創作不易,來了的客官點點關注,收藏,訂閱一鍵三連?😜

前言
運維之基礎——Linux,我是一個即將畢業的大學生,超超,如果你也在學習Linux,不妨跟著萌新超超一起學習Linux,拿下Linux,一起加油,共同努力,拿到理想offer!
系列文章
Linux進階 | Docker部署nginx的web服務,VOLUME的使用詳解,實作資料持久化!
Linux進階 | 2萬字總結最詳細的Docker的安裝、底層隔離機制和簡單使用!建議收藏,持續更新?
Linux | 詳解系統監控和常用命令(top free dstat)
Linux | 超超講解SSH的原理與SSH的實作!建議收藏?
概述
Docker 屬于 Linux 容器的一種封裝,提供簡單易用的容器使用介面,它是目前最流行的 Linux 容器解決方案,本期內容為Docker第三期,通過本期內容將會掌握docker的鏡像制作,
目錄
前言
概述
超超Docker學習思維導圖
鏡像的概念
Base鏡像
Bootfs與rootfs
鏡像的分層結構
鏡像的制作
制作前的疑問
Dockerfile
制作步驟
以制作nginx鏡像為例啟動web服務
自己練習制作nginx鏡像
練習
練習一:以python:3.9鏡像為基礎創建鏡像,并以此啟動容器并連接到redis
練習二:實作docker官網的鏡像制作
超超Docker學習思維導圖

Docker思維導圖將持續更新,歡迎大家訂閱Linux欄目!
鏡像的概念
Base鏡像
Base 鏡像有兩層含義:
1. 不依賴其他鏡像,從 scratch 構建,
2. 其他鏡像可以之為基礎進行擴展,
Base 鏡像的通常都是各種 Linux 發行版的 Docker 鏡像,比如 Ubuntu, Debian, CentOS 等,以 CentOS 為例學習 base 鏡像包含哪些內容,
[root@docker ~]# docker images centos
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 8652b9f0cb4c 9 months ago 204MB
使用docker pull centos下載最新版本的Centos鏡像也就207M左右,而我們平時下載一個原生的centos鏡像都是4G,
容器只能使用 Host 的 kernel,并且不能修改,所有容器都共用 host 的 kernel,在容器中沒辦法對 kernel 升級,如果容器對 kernel 版本有要求(比如應用只能在某個 kernel 版本下運行),則不建議用容器,這種場景虛擬機可能更合適,
Bootfs與rootfs
Bootfs:內核空間是 kernel,Linux 剛啟動時會加載 bootfs 檔案系統,之后 bootfs 會被卸載掉,
rootfs:容器內部的作業系統,用戶空間的檔案系統是 rootfs,包含我們熟悉的 /dev, /proc, /bin 等目錄,
鏡像的分層結構
Docker 支持通過擴展現有鏡像,創建新的鏡像,實際上,Docker Hub 中 99% 的鏡像都是通過在 base 鏡像中安裝和配置需要的軟體構建出來的,
鏡像的制作
制作前的疑問
鏡像是什么?
答案:鏡像由程式代碼、基礎作業系統、基礎軟體組成,
鏡像是一個檔案,里面包含了程式代碼、基礎作業系統、基礎軟體,
為什么要制作鏡像,docker hub上不是有很多鏡像嗎?
答案:
1.不能滿足我們的需要
2.不夠安全,有安全隱患
誰去制作鏡像?
答案: 一般都是開發人員去制作
也可以是運維人員去制作、權利非常大的作業人員,
鏡像里有什么?
1.base 鏡像 --》基礎鏡像--》提供作業系統,其他軟體最基本的功能
基礎鏡像里的作業系統:Ubuntu、debian、centos等
Dockerfile
如果你想要從一個基礎鏡像開始建立一個自定義鏡像,可以選擇一步一步進行構建,也可以選擇寫一個組態檔,然后一條命令(docker build)完成構建,顯然組態檔的方式可以更好地應對需求的變更,這個組態檔就是Dockerfile,
那DockerFile是什么?
Dockerfile: 用于描述鏡像的生成規則(組態檔),Dockerfile中的每一條命令,都在Docker鏡像中以一個獨立鏡像層的形式存在
DockerFile就類似與Linux的shell腳本,只不過DockerFile是用來構建鏡像的
制作步驟
以制作nginx鏡像為例啟動web服務
1.新建一個空目錄
[root@docker ~]# cd /mydocker/
[root@docker mydocker]# pwd
/mydocker
2.新建Dockerfile
[root@docker mydocker]# vim Dockerfile
[root@docker mydocker]# ls
Dockerfile
[root@docker mydocker]# cat Dockerfile
# Use an official Python runtime as a parent image 下載一個python2.7的鏡像模板,已經有作業系統和安裝好python2.7
FROM python:2.7-slim
# Set the working directory to /app #在docker容器里的作業目錄-->進入docker容器的時候,所在的目錄
WORKDIR /app
# Copy the current directory contents into the container at /app 復制當前目錄下的所有的內容到容器里的/app目錄下
ADD . /app
VOLUME ["/data_flask"]
# Install any needed packages specified in requirements.txt 制作鏡像的時候運行
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable 設定環境變數,app.py運行時可以呼叫
ENV NAME World
# Run app.py when the container launches 容器啟動時運行
CMD ["python", "app.py"]
3.新建requirements.txt檔案
[root@docker mydocker]# vim requirements.txt
[root@docker mydocker]# ls
Dockerfile requirements.txt
[root@docker mydocker]# cat requirements.txt
Flask
Redis
4.新建app.py檔案
[root@docker mydocker]# vim app.py
[root@docker mydocker]# ls
app.py Dockerfile requirements.txt
[root@docker mydocker]# cat app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
[root@docker mydocker]#
5.生成鏡像,需要一點時間,因為下載安裝很多東西
[root@docker mydocker]# docker build -t hellochao .
-t :指定要創建的目標鏡像名
. :Dockerfile 檔案所在目錄,可以指定Dockerfile 的絕對路徑
注:此時可能會有錯誤爆出,是由于網路連接問題所導致,
解決方法:重啟docker服務,

6.查看生成的鏡像
[root@docker mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hellochao latest b9cdb67a8db4 14 seconds ago 159MB
<none> <none> fbea61ffb278 32 minutes ago 148MB
ubuntu-chaochao 2.0 67596afe27b5 4 days ago 72.8MB
ubuntu latest 1318b700e415 3 weeks ago 72.8MB
redis latest aa4d65e670d6 3 weeks ago 105MB
nginx latest 08b152afcfae 3 weeks ago 133MB
mysql 5.7.35 8cf625070931 3 weeks ago 448MB
daocloud.io/nginx latest 6084105296a9 5 months ago 133MB
hello-world latest d1165f221234 5 months ago 13.3kB
centos 7 8652b9f0cb4c 9 months ago 204MB
python 2.7-slim eeb27ee6b893 16 months ago 148MB
centos/python-35-centos7 latest 2db34dda8fd8 2 years ago 645MB
7.以剛剛創建的鏡像為基礎創建容器
[root@docker mydocker]# docker run -d --name hello-chao-1 -p 8013:80 hellochao
b571dc07bf393737fe820cf5a7b6c5c61359b9979054d3484aeb1d573a788d71
[root@docker mydocker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b571dc07bf39 hellochao "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:8013->80/tcp, :::8013->80/tcp hello-chao-1
訪問方式1:
用瀏覽器訪問:網站-->宿主機IP+埠號

訪問方式2:curl IP地址:埠號
Hello World!
Hostname: f4aeb5d5305a
Visits: cannot connect to Redis, counter disabled
因為redis資料庫容器沒有啟動,flask web服務不能連接到redis資料庫
8.啟動redis容器
[root@docker mydocker]# docker run -d -p 6379:6379 --name hello-redis-1 redis
4ae649e2c9cf52e19e5cab4e4a6136d493390ff2e089838a4d65221e8e5b534a
9.再次啟動一個自己制作鏡像的容器,連接到redis容器
[root@docker mydocker]# docker run -d --name hello-chao-2 -p 8014:80 --link hello-redis-1:redis hellochao
1967d1eae722bd34e01f5bc575127e041a6caea15f48bd5f3f137ae1f2c744a9
10.訪問web服務,效果如下:
方式一:用瀏覽器

方式二:curl
[root@docker ~]# curl 192.168.232.132:8014
<h3>Hello World!</h3><b>Hostname:</b> 1967d1eae722<br/><b>Visits:</b> 3[root@docker ~]#
Dockerfile里面的RUN和CMD是什么時候運行的?
RUN:構建鏡像時候運行的,在臨時啟動的中間測驗的容器里運行,會產生中間的鏡像層
CMD:鏡像制作好后,容器啟動的時候運行
ADD和COPY
Dockerfile中的COPY指令和ADD指令都可以將主機上的資源復制或加入到容器鏡像中,都是在構建鏡像的程序中完成的,
COPY指令和ADD指令的唯一區別在于是否支持從遠程URL獲取資源,
COPY指令只能從執行docker build所在的主機上讀取資源并復制到鏡像中,
而ADD指令還支持通過URL從遠程服務器讀取資源并復制到鏡像中,(推薦)
自己練習制作nginx鏡像
1.部署環境
[root@docker ~]# mkdir/mydocker/nginx
2.創建Dockerfile
[root@docker nginx]# ls
Dockerfile install_nginx.sh nginx-1.21.1.tar.gz
[root@docker nginx]# vim Dockerfile
[root@docker nginx]# cat Dockerfile
FROM centos:7
ENV NGINX_VERSION 1.21.1
ENV AUTHOR ChaoChao
LABEL maintainer="lzc<2569138892@qq.com>"
RUN mkdir /nginx
WORKDIR /nginx
COPY . /nginx
RUN set -ex;\
bash install_nginx.sh ; \
yum install vim iputils net-tools iproute -y
EXPOSE 80
ENV PATH=/usr/local/nginx1/sbin:$PATH
STOPSIGNAL SIGQUIT
CMD ["nginx","-g","daemon off;"]
[root@docker nginx]#
3.構建nginx鏡像
[root@docker nginx]#docker build -t chao-ngixn-1 .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
chao-nginx-1 latest f668613a9de5 6 seconds ago 543MB
getting-started-1 latest 78bc6143bbce 7 hours ago 383MB
getting-started latest 8224b1c2dc86 19 hours ago 374MB
hello-chao-3 latest a9c7a3290464 20 hours ago 923MB
hellochao latest b9cdb67a8db4 24 hours ago 159MB
<none> <none> fbea61ffb278 24 hours ago 148MB
python 3.9 4c7220cee541 31 hours ago 912MB
node 12-alpine dc1848067319 5 days ago 88.9MB
ubuntu-chaochao 2.0 67596afe27b5 5 days ago 72.8MB
ubuntu latest 1318b700e415 3 weeks ago 72.8MB
redis latest aa4d65e670d6 3 weeks ago 105MB
nginx latest 08b152afcfae 3 weeks ago 133MB
mysql 5.7.35 8cf625070931 3 weeks ago 448MB
daocloud.io/nginx latest 6084105296a9 5 months ago 133MB
hello-world latest d1165f221234 5 months ago 13.3kB
centos 7 8652b9f0cb4c 9 months ago 204MB
python 2.7-slim eeb27ee6b893 16 months ago 148MB
centos/python-35-centos7 latest 2db34dda8fd8 2 years ago 645MB
4.創建以新建鏡像為基礎的容器并測驗是否實作功能
[root@docker nginx]# docker run -d --name chaochao-nginx-1 -p 8018:80 chao-nginx-1
fad41f8b6ee1da4034c8fd89a1a56c3588a28100850e7c5a9cfdb8f089eeef4f
[root@docker nginx]# curl 192.168.232.132:8018
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@docker nginx]# docker exec -it chaochao-nginx-1 /bin/bash
[root@fad41f8b6ee1 nginx]# ls
Dockerfile install_nginx.sh nginx-1.21.1 nginx-1.21.1.tar.gz
[root@fad41f8b6ee1 nginx]# vim chaochao.txt
[root@fad41f8b6ee1 nginx]# cat chaochao.txt
echo I am chaochao!
[root@fad41f8b6ee1 nginx]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
135: eth0@if136: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:0a brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.10/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@fad41f8b6ee1 nginx]# exit
exit
[root@docker nginx]# ls
Dockerfile install_nginx.sh nginx-1.21.1.tar.gz
5.更改index.html
方法一:利用掛載實作
[root@docker nginx]# docker run -dp 8019:80 -v /web:/usr/local/nginx1/html --name chaochao-nginx-2 chao-nginx-1
6e3f9a849ce179ad184e2c314651c86a4db8c3c7ab5f264f0af27baa6aa1a047
[root@docker nginx]#

方法二:使用卷
[root@docker nginx]# docker volume create chao-nginx-volume-1
chao-nginx-volume-1
[root@docker nginx]# docker volume inspect chao-nginx-volume-1
[
{
"CreatedAt": "2021-08-18T17:20:27+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/chao-nginx-volume-1/_data",
"Name": "chao-nginx-volume-1",
"Options": {},
"Scope": "local"
}
]
[root@docker nginx]# cd /var/lib/docker/volumes/chao-nginx-volume-1/_data
[root@docker _data]# cp /web/* .
[root@docker _data]# ls
1.jpg index.html rep.html
[root@docker _data]# docker run -dp 8020:80 --name chao-nginx-3 --mount source=chao-nginx-volume-1,target=/usr/local/nginx1/html/ chao-nginx-1
e0f8f8b0bfc40b8ad6d2e74a536b6e831dde444fbb8c536b37ead81e31c5d64c

練習
練習一:以python:3.9鏡像為基礎創建鏡像,并以此啟動容器并連接到redis
1.升級 hello-chao-1的鏡像里的基礎鏡像為python:3.9,并將自己的鏡像更名
2.啟動容器和redis容器,測驗訪問
[root@docker ~]# cd mydocker2/
[root@docker mydocker2]# ls
[root@docker mydocker2]# cp /mydocker/* .
[root@docker mydocker2]# ls
app.py Dockerfile requirements.txt
[root@docker mydocker2]# vim Dockerfile
[root@docker mydocker2]# docker pull python:3.9
[root@docker mydocker2]# docker build -t hello-chao-3 .
[root@docker mydocker2]# curl 192.168.232.132:8015
<h3>Hello,I am Lizhichao!</h3><b>Hostname:</b> 232af106a331<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>[root@docker mydocker2]#
[root@docker mydocker2]# docker run -d --name hello-redis-2 -p 8011:6379 redis
181d9b51863a6ad888e7e53ac27070343396fab16c911851dfa2c4e7579b7f22
[root@docker mydocker2]# docker run -d --name hello-chao-5 -p 8016:80 --link hello-redis-2:redis hello-chao-3
080bdc9dcd9304597c27db1afd6e266fd6d8c37ae78fcf02bedf81809a085470
[root@docker mydocker2]# curl 192.168.232.132:8016
<h3>Hello,I am Lizhichao!</h3><b>Hostname:</b> 080bdc9dcd93<br/><b>Visits:</b> 1[root@docker mydocker2]# curl 192.168.232.132:8016
<h3>Hello,I am Lizhichao!</h3><b>Hostname:</b> 080bdc9dcd93<br/><b>Visits:</b> 2[root@docker mydocker2]#
練習二:實作docker官網的鏡像制作
實作 https://docs.docker.com/get-started/02_our_app/ 官網的鏡像制作
[root@docker ~]# ls
anaconda-ks.cfg getting-started-master.zip mydocker2 ubuntu-chao2.tar ubuntu-chao.tar
[root@docker ~]# unzip getting-started-master.zip
[root@docker ~]# ls
anaconda-ks.cfg getting-started-master getting-started-master.zip mydocker2 ubuntu-chao2.tar ubuntu-chao.tar
[root@docker ~]# cd getting-started-master
[root@docker getting-started-master]# ls
app docker-compose.yml docs LICENSE README.md yarn.lock
build.sh Dockerfile Jenkinsfile mkdocs.yml requirements.txt
[root@docker getting-started-master]# cd app
[root@docker app]#
[root@docker app]# ls
package.json spec src yarn.lock
[root@docker app]# vim Dockerfile
[root@docker app]# ls
Dockerfile package.json spec src yarn.lock
[root@docker app]# cat Dockerfile
#syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
[root@docker app]#
[root@docker app]# docker run -dp 3000:3000 getting-started-1
588fd20a79b9d8082507dc209e73fa8a51a1e6abb56bc019256afc0df43ac73a
[root@docker app]#
[root@docker app]# docker run -dp 3001:3000 --name chao-node-2 getting-started-1
b5c8d74e7be4426fb22e1c524e26bead8b160cd938ae6bebe2433a52c65ab33a
[root@docker app]#


創作不易,客官點個贊,評論一下吧!超超和你一起加油?😜
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/296544.html
標籤:其他
