Docker概述
真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署,而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可,Docker容器使用沙箱機制,通過隔離機制,使每個發布的應用之間互相隔離,相互之間不會有任何介面,
優點:隔離,鏡像輕巧,秒級啟動,
檔案地址:https://docs.docker.com/
倉庫地址:https://hub.docker.com/
- Docker基本組成:
1.鏡像(image):類似于一個模板,通過這個模板來創建容器服務,如tomcat鏡像=》run命令運行鏡像=》創建tomcat容器
2.容器(container):Docker利用容器技術,可獨立運行一個或一組應用,是通過鏡像來創建的,可以執行啟動,停止,洗掉等命令,暫時可將容器理解為一個簡易的linux系統,
3.倉庫(repository):存放鏡像的地方,分為共有倉庫(如Docker hub)和私有倉庫,
Docker安裝
根據幫助檔案:
#1.卸載舊版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
#2.安裝包
sudo yum install -y yum-utils
#3.設定鏡像倉庫
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo #默認國外的,非常慢
sudo yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #推薦阿里云
#4.更新軟體包索引
yum makecache fast
#4.安裝Docker引擎 (docker-ce:社區版)
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
#5.啟動Docker
sudo systemctl start docker
#docker version查看是否安裝成功
#6.測驗helloword
sudo docker run hello-world
#7.查看下載的hello-world鏡像
docker images
#8.卸載docker
#移除Docker引擎
sudo yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
#移除鏡像
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
阿里云鏡像加速
-
進入阿里云官網的容器鏡像服務,找到鏡像加速地址
-
配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://1kc9p0un.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker run的流程
Docker如何作業的?
Docker是一個CS結構的系統,Docker的守護行程運行在主機上,DockerServer接收到DockerClient的指令執行,
Docker命令
官方檔案:https://docs.docker.com/engine/reference/commandline/build/
docker version #顯示版本資訊
docker info #docker更加詳細的資訊
docker 命令 --help #幫助命令
- 鏡像命令
docker images #查看所有鏡像
解釋
- Repostory #鏡像倉庫源
- TAG #鏡像標簽
- IMAGE_ID #鏡像id
可選項(可通過docker images --help查看)
-a, --all Show all images (default hides intermediate images)
-q, --quiet Only show image IDs
docker search搜索鏡像
可選項
- 通過收藏數過濾
docker pull下載鏡像 鏡像名[:tag]
[root@cchuang cchuang]# docker pull mysql
Using default tag: latest #如果不寫tag,默認最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分層下載
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真實地址
#指定版本下載
[root@cchuang cchuang]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
洗掉鏡像
-
通過image_id洗掉
-
洗掉所有鏡像
-
容器命令
說明:有了鏡像才能創建容器,先下載一個centos鏡像學習,
docker pull centos
新建容器并啟動:
docker run [引數] image
#引數說明:
--name="Name" #容器名稱,tomcat1,Tomcat2
-d #后臺運行
-it #互動方式運行,進入容器查看內容
-p #指定容器埠
-p ip:主機埠:容器埠(常用)
-p 主機埠:容器埠(常用)
-p 容器埠
-P #隨機指定埠
#測驗
[root@cchuang ~]# docker run -it centos /bin/bash #啟動并進入容器
[root@a25b788abecd /]#
#查看容器內的centos
[root@a25b788abecd /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退出容器(exit:退出容器并停止;ctrl+P+Q:容器退出不停止)
[root@a25b788abecd /]# exit
exit
#列出所有運行容器
[root@cchuang /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@cchuang /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a25b788abecd centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago agitated_khorana
8e4b0bac5001 feb5d9fea6a5 "/hello" 3 hours ago Exited (0) 3 hours ago epic_hypatia
#洗掉容器
docker rm 容器id #洗掉指定容器,不能洗掉正在運行的容器,若強制洗掉,-f
docker rm -f $(docker ps -aq) #洗掉所有容器
docker ps -a -q|xargs docker rm #洗掉所有容器
#啟動和停止容器
docker start 容器id #啟動容器
docker restart 容器id #重啟容器
docker stop 容器id #停止容器
docker kill 容器id #
常用的其它命令
后臺啟動容器
#docker run -d 鏡像名
[root@cchuang /]# docker run -d centos
#docker ps 發現centos停止了
#docker容器使用后臺運行時,必須要有個前臺行程,docker發現沒有應用,就會自動停止,
#nginx,容器啟動后,發現沒有提供服務,就會自動停止,
查看日志
docker logs
#顯示指定條數的日志
-ft #顯示格式時間戳
--tail 10 #顯示條數
[root@cchuang /]# docker logs -ft --tail 10 ddeeded713ed
查看容器中的行程資訊
#docker top 容器id
[root@cchuang /]# docker top ddeeded713ed
UID PID PPID C STIME TTY TIME CMD
root 17484 17465 0 11:15 pts/0 00:00:00 /bin/bash
查看容器的元資料
[root@cchuang /]# docker inspect ddeeded713ed
進入容器的命令
通常容器都是以后臺方式運行的,需要進入容器,修改一些配置,
docker exec -it 容器id /bin/bash
docker attach 容器id
從容器內拷貝到主機上
#docker cp 容器id:容器內路勁 目標主機路勁
[root@cchuang /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ddeeded713ed centos "/bin/bash" 6 hours ago Up 6 hours cool_wozniak
#查看主機路徑
[root@cchuang /]# cd /home
[root@cchuang home]# mkdir test
[root@cchuang home]# ls
cchuang test
#進入容器內部
[root@cchuang home]# docker attach ddeeded713ed
[root@ddeeded713ed /]# cd home
[root@ddeeded713ed home]# ls
[root@ddeeded713ed home]# touch test.txt
[root@ddeeded713ed home]# ls
test.txt
[root@ddeeded713ed home]# exit
exit
#拷貝檔案到主機上
[root@cchuang home]# docker cp ddeeded713ed:/home/test.txt /home/test
Successfully copied 1.536kB to /home/test
[root@cchuang home]# ls
cchuang test
[root@cchuang home]# cd test
[root@cchuang test]# ls
test.txt
1.部署nginx
#1.搜索鏡像
[root@cchuang test]# docker search nginx
#2.pull鏡像
[root@cchuang test]# docker pull nginx
#3.啟動容器并開放埠
[root@cchuang test]# docker run -d --name nginx01 -p 3344:80 nginx
埠暴露:
補充:(阿里云開放埠步驟)
1.新增安全組訪問規則配置
2.開放埠
開放埠:firewall-cmd --zone=public --add-port=3344/tcp --permanent
查看已開啟的埠資訊:
firewall-cmd --list-ports
3.重啟防火墻
systemctl restart firewalld.service
利用iptable開啟埠
命令:iptables -A INPUT -p tcp --dport 3344 -j ACCEPT
4.啟動docker,映射埠
docker run -d --name nginx0 -p 3344:80 nginx
5.如果報錯,重啟docker服務
systemctl restart docker
6.測驗
外網輸入地址 ip:3344 可進入docker容器的nginx
若要修改nginx組態檔,每次都要進入nginx內部修改,非常麻煩,我們可以在容器外部提供一個映射的路徑,達到在容器外部修改檔案,容器內部可以自動修改(-v 資料卷的方式)
2.部署es+kibana
#1.es暴露的埠很多
#2.耗記憶體
#3.es資料一般要放到安全目錄,掛載
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
#啟動之后linux卡住,耗記憶體
#查看cpu狀態
docker stats
#關閉docker,增加es的記憶體限制,通過 -e 環境變數配置修改
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
de3ab3cae3fb elasticsearch01 97.21% 318MiB / 1.715GiB 18.11% 656B / 0B 108MB / 246kB 26
kibana連接es,如何才能連接過去?見下文docker網路原理
可視化
- portainer
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
測驗:
打開阿里云安全組,外網測驗 ip:8088
- Rancher(CI/CD)
Docker鏡像加載原理
-
UnionFS(聯合檔案系統)
UnionFS(聯合檔案系統)是一種分層,輕量級且高性能的檔案系統,他支持對檔案系統的修改作為一次提交來一層一層疊加,同時可將不同的目錄掛載到同一個虛擬檔案系統下,Union檔案系統是Docker鏡像的基礎,鏡像可以通過分層來繼承,基于基礎鏡像可以制作各種具體的應用鏡像, -
Docker鏡像加載原理
docker鏡像實際上是由一層一層的檔案系統組成,這種層級的檔案系統即UnionFS,
bootfs(boot file system)主要包含bootloader和kernel,bootloader主要引導加載kernel,Linux剛啟動時就會加載bootfs檔案系統,Docker最底層就是bootfs,這一層與典型的Linux/Unix是一樣的,包含boot加載器和內核,當boot加載完整個內核就在記憶體中了,此時記憶體的使用權已經由bootfs轉交給內核了,此時系統就會卸載bootfs,
rootfs(root file system),在bootfs之上,包含的就是典型的 /dev,/proc,/bin,/etc等標準目錄和檔案,rootfs就是各種不同作業系統的發行版,如centos,Ubuntu等等,
為何docker的centos只有幾百MB,而安裝進虛擬機的有是幾個G
對于一個精簡的centos,rootfs可以很小,只需要包含最基本的命令即可,底層的kernel用的還是host的,自己只需要提供rootfs即可,不同的發行版本可以共用bootfs,
Docker分層理解
所有的Docker鏡像都起始于一個基礎鏡像,當進行修改或者新增時,會在當前鏡像層之上,創建新的鏡像層,
在額外添加鏡像層的同時,鏡像是始終保持當前鏡像的所有組合,如圖,每個鏡像層包含三個檔案,整個鏡像包含來自兩個鏡像層的6個檔案,
下圖展示了三層鏡像,在外界看來只有6個檔案,因為最上層檔案7是檔案5的更新版本,
最終對外展示的鏡像層視圖:
特點
Docker鏡像都是只讀的,當容器啟動時,一個新的可寫層被加載到鏡像的頂部,這一層通常是所說的容器層,容器層之下的都叫鏡像層,
如何提交一個自己的鏡像(commit)
如果想要保存當前容器的狀態,就可以通過commit來提交,獲得一個鏡像,
docker commit 提交容器成為一個新副本
#命令和git原理近似
docker commit -m="提交的描述資訊" -a="作者" 容器id 目標鏡像名:[TAG]
#原tomcat鏡像沒有webapp下的應用,添加應用后提交鏡像
[root@cchuang ~]# docker commit -a="cchuang" -m="添加應用" 1274e9395923 tomcat1.0
[root@cchuang ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat1.0 latest ad4efd5a4e82 24 seconds ago 684MB
Docker資料卷
- 什么是容器資料卷?
docker將應用和環境打包成鏡像,但是如果像mysql等將資料也放在容器中,那么容器洗掉,資料也會丟失,我們可以將資料存在一個容器可以共享的地方,Docker容器中產生的資料,可以同步到本地,這就是卷技術,可以將Docker容器內的目錄,掛載到Linux上面,
- 使用方式:
1.使用命令來掛載 -v
docker run -it -v 主機目錄:容器內目錄
docker run -it -v /home/test:/home tomcat /bin/bash
#啟動后通過docker inspect查看掛載資訊
對本地檔案的修改會使掛載的容器內的目錄同步修改!
實戰:安裝mysql,MySQL的資料持久化問題,
mysql的資料再data目錄下,將data目錄掛載出來.
#1.拉取mysql鏡像
[root@cchuang /]# docker pull mysql:5.7
-d 后臺運行
-p 埠映射
-v 掛載
-e 環境配置
--name 容器名稱
#2.運行容器,需要做資料掛載,安裝啟動mysql時,需要設定密碼
[root@cchuang /]# docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
#3.啟動成功,從本地通過sqlyog連接服務器的3306,映射到容器內的3306
#4.查看本地掛載目錄
[root@cchuang home]# ls
cchuang mysql test
[root@cchuang home]# cd mysql
[root@cchuang mysql]# ls
conf data
[root@cchuang mysql]# cd data
[root@cchuang data]# ls
auto.cnf ib_buffer_pool mysql server-key.pem
ca-key.pem ibdata1 performance_schema sys
ca.pem ib_logfile0 private_key.pem
client-cert.pem ib_logfile1 public_key.pem
client-key.pem ibtmp1 server-cert.pem
#5.通過本地sqlyog創建一個test資料庫,即docker容器內也會創建一個檔案,查看本地掛載的data目錄,發現多一個檔案,掛載成功,
[root@cchuang data]# ls
auto.cnf ib_buffer_pool mysql server-key.pem
ca-key.pem ibdata1 performance_schema sys
ca.pem ib_logfile0 private_key.pem test
client-cert.pem ib_logfile1 public_key.pem
client-key.pem ibtmp1 server-cert.pem
#6.洗掉docker容器,linux本地掛載的data目錄下的檔案依舊存在,資料持久化,
[root@cchuang data]# docker rm -f 60afb0e127e9
[root@cchuang data]# ls
auto.cnf ib_buffer_pool mysql server-key.pem
ca-key.pem ibdata1 performance_schema sys
ca.pem ib_logfile0 private_key.pem test
client-cert.pem ib_logfile1 public_key.pem
client-key.pem ibtmp1 server-cert.pem
具名掛載和匿名掛載
#匿名掛載(沒有指定容器外的路徑)
[root@cchuang /]# docker run -d -p 80:80 --name nginx01 -v /etc/nginx nginx
#查看所有volume情況
[root@cchuang /]# docker volume ls
DRIVER VOLUME NAME
local 1ef0259786a48805e8816ea03f5dafc92477a449d64117c4c3a4cda027906ba3
local 52eeb7ad21d8cff59fdeab57ed8e3c9ca868620a542b9c058a6db8ba8504f1f3
#具名掛載(通過 -v 卷名:容器內路徑)
[root@cchuang /]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
a4e76f8aecbd7889f85f7955eb1839924c4bf273fc807ef6cc0b2dab308c5f83
[root@cchuang /]# docker volume ls
DRIVER VOLUME NAME
local juming-nginx
#查看一下掛載的卷
[root@cchuang ~]# docker volume inspect juming-nginx
[
{
"CreatedAt": "2023-04-17T17:33:31+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
所有docker下的卷,沒有指定目錄的情況下都是在/var/lib/docker/volumes/XX下的,通過具名掛載可以很方便的找到卷,多數情況下都是具名掛載,
#掛載方式
-v 容器內路徑 #匿名掛載
-v 卷名:容器外路徑 #具名掛載
-v /宿主機路徑:容器內路徑 #指定路徑掛載
拓展:
#通過 -v 容器內路徑:ro rw改變讀寫權限(對于容器來說,在容器內操作掛載出來的目錄的權限)
ro readonly #只讀 (只能從容器外部宿主機來操作,容器內部無法操作)
rw readwrite #可讀寫
[root@cchuang ~]# docker run -d -P --name nginx01 -v juming-nginx:/etc/nginx:ro nginx
初識DockerFile
Dockfile就是用來構建Docker鏡像的,一段命令腳本,通過這個腳本可以生成鏡像,腳本里的每個命令就是一層,
#構建dockerfile檔案
[root@cchuang docker-file-volume]# vim dockerfile1
[root@cchuang docker-file-volume]# cat dockerfile1
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end---"
CMD /bin/bash
#構建鏡像
[root@cchuang docker-file-volume]# docker build -f dockerfile1 -t cchuang/centos .
[+] Building 0.1s (5/5) FINISHED
=> [internal] load build definition from dockerfile1 0.0s
=> => transferring dockerfile: 121B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 0.0s
=> [1/1] FROM docker.io/library/centos 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:da9e95cba3f789425b8d7437106995c796f02b5bb5c0b05aa2c43df4399ef0f0 0.0s
=> => naming to docker.io/cchuang/centos 0.0s
#啟動容器運行新建的鏡像
[root@cchuang docker-file-volume]# docker run -it cchuang/centos /bin/bash
[root@f5826eedf0d5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
#查看掛載的路徑
[root@cchuang docker-file-volume]# docker inspect da9e95cba3f7
資料卷容器
多個容器之間共享資料(通過 --volume-from)
#啟動三個容器,通過上面構建的鏡像啟動
[root@cchuang ~]# docker run -it --name docker01 da9e95cba3f7
[root@6e21966e1eaf /]# ls -l
#創建docker02,通過--volume-from 繼承docker01容器
[root@cchuang ~]# docker run -it --name docker02 --volumes-from docker01 da9e95cba3f7
#進入docker01的容器,在掛載的volume01目錄下創建一個檔案,在docker02里也會新增,
[root@cchuang ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c430673f042d da9e95cba3f7 "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker02
6e21966e1eaf da9e95cba3f7 "/bin/sh -c /bin/bash" 6 minutes ago Up 6 minutes docker01
#進入docker01
[root@cchuang ~]# docker attach 6e21966e1eaf
[root@6e21966e1eaf /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 360 Apr 17 12:58 dev
drwxr-xr-x 1 root root 4096 Apr 17 12:58 etc
drwxr-xr-x 2 root root 4096 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 4096 Sep 15 2021 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 2020 media
drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
dr-xr-xr-x 99 root root 0 Apr 17 12:58 proc
dr-xr-x--- 2 root root 4096 Sep 15 2021 root
drwxr-xr-x 11 root root 4096 Sep 15 2021 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Apr 17 12:58 sys
drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
drwxr-xr-x 20 root root 4096 Sep 15 2021 var
drwxr-xr-x 2 root root 4096 Apr 17 12:58 volume01
drwxr-xr-x 2 root root 4096 Apr 17 12:58 volume02
[root@6e21966e1eaf /]# cd volume01
#新增檔案
[root@6e21966e1eaf volume01]# ls
[root@6e21966e1eaf volume01]# touch docker01
[root@6e21966e1eaf volume01]# ls
docker01
#進入docker02容器,查看volume01目錄發現docker01創建的檔案同步到的docker02
[root@cchuang ~]# docker attach c430673f042d
[root@c430673f042d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@c430673f042d /]# cd volume01
[root@c430673f042d volume01]# ls
docker01
可以用來實作多個mysql之間的資料共享
DockerFile介紹
DockerFile就是用來構建Docker鏡像的,是一個命令腳本,
構建步驟:
- 撰寫一個Dockefile檔案
- docker build構建成為一個鏡像
- docker run運行鏡像
- docker push發布鏡像(docker hub或者阿里云鏡像倉庫)
官網centos鏡像:
很多官方鏡像都是基礎包,很多功能都沒有,我們通常會制作自己的鏡像,
DockerFile構建程序
- 注意:
1.每個保留關鍵字都是大寫字母,
2.每個指令都會創建提交一個新的鏡像層,并提交,
- dockerfile指令說明:
FORM #基礎鏡像,一切從這里構建
MAINTAINER #鏡像作者,姓名+郵箱
RUN #鏡像構建的時候運行的命令
ADD #需要添加的其他鏡像,如tomcat壓縮包
WORKDIR #鏡像作業目錄
VOLUME #掛載卷位置
EXPOSE #暴露埠,不指定需要在run的時候 -p 指定
CMD #指定這個容器啟動時要運行的命令 (只有最后一個會生效,可被替代)
ENTRYPOINT #指定這個容器啟動時要運行的命令 (可以追加命令)
ONBUILD #當構建一個被繼承的Dockerfile時,會運行ONBUILD的指令
COPY #類似ADD,將檔案拷貝到鏡像里
ENV #構建的時候設定環境變數
- 構建自己的centos鏡像(原始centos鏡像沒有vim和ipconfig命令)
#撰寫dockerfile檔案
FROM centos:7
MAINTAINER cchuang<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end---"
CMD /bin/bash
#通過這個檔案構建鏡像
[root@cchuang dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
[root@cchuang dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 ab91446c9412 32 seconds ago 640MB
#運行測驗vim和ipconfig命令
[root@cchuang dockerfile]# docker run -it ab91446c9412 /bin/bash
[root@f1a5718cc952 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
#docker history命令查看鏡像的構建歷史
[root@cchuang dockerfile]# docker history mysql:5.7
IMAGE CREATED CREATED BY SIZE COMMENT
c20987f18b13 16 months ago /bin/sh -c #(nop) CMD ["mysqld"] 0B
<missing> 16 months ago /bin/sh -c #(nop) EXPOSE 3306 33060 0B
<missing> 16 months ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 16 months ago /bin/sh -c ln -s usr/local/bin/docker-entryp… 34B
<missing> 16 months ago /bin/sh -c #(nop) COPY file:345a22fe55d3e678… 14.5kB
<missing> 16 months ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0B
<missing> 16 months ago /bin/sh -c { echo mysql-community-server m… 313MB
<missing> 16 months ago /bin/sh -c echo 'deb http://repo.mysql.com/a… 55B
<missing> 16 months ago /bin/sh -c #(nop) ENV MYSQL_VERSION=5.7.36-… 0B
<missing> 16 months ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=5.7 0B
<missing> 16 months ago /bin/sh -c set -ex; key='A4A9406876FCBD3C45… 1.84kB
<missing> 16 months ago /bin/sh -c apt-get update && apt-get install… 52.2MB
<missing> 16 months ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0B
<missing> 16 months ago /bin/sh -c set -eux; savedAptMark="$(apt-ma… 4.17MB
<missing> 16 months ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0B
<missing> 16 months ago /bin/sh -c apt-get update && apt-get install… 9.34MB
<missing> 16 months ago /bin/sh -c groupadd -r mysql && useradd -r -… 329kB
<missing> 16 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 16 months ago /bin/sh -c #(nop) ADD file:bd5c9e0e0145fe33b… 69.3MB
- CMD和ENTRYPOINT的區別
ENTRYPOINT 中的引數始侄訓被使用,而CMD中的額外引數可以在容器啟動時動態替換掉,
如果Dockerfile檔案既有CMD,又有ENTRYPOINT ,那么CMD將會被當做引數傳遞給ENTRYPOINT
Dockerfile制作Tomcat鏡像
1.準備鏡像檔案,tomcat壓縮包,jdk壓縮包
#下載jdk
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz
[root@cchuang cchuang]# ls
apache-tomcat-9.0.73.tar.gz jdk-8u131-linux-x64.tar.gz
2.撰寫dockerfile檔案
[root@cchuang cchuang]# vim Dockerfile
FROM centos:7
MAINTAINER cchuang<[email protected]>
ADD jdk-8u131-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.73.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_131
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.73
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.73
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.73/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.73/logs/catalina.out
#構建鏡像
[root@cchuang cchuang]# docker build -t diytomcat .
#啟動容器并掛載webapps,logs目錄
[root@cchuang cchuang]# docker run -d --name ccuhangTomcat -p 8080:8080 -v /home/cchuang/tomcat/webapps:/usr/local/apache-tomcat-9.0.73/webapps -v /home/cchuang/tomcat/logs:/usr/local/apache-tomcat-9.0.73/logs diytomcat
948bd5fbbcb271ca048c72a1d605693701e3cfbde1472d6e900411ff9a1367f2
#檔案掛載成功,發布專案只需發布在本地掛載的webapps目錄,會同步到容器內的webapps目錄
[root@cchuang tomcat]# ls
logs webapps
[root@cchuang tomcat]# pwd
/home/cchuang/tomcat
發布自己的鏡像
發布dockerhub
#登陸dockerhub
[root@cchuang /]# docker login -u huangchengcong
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
#提交鏡像
#docker push dockerhub用戶名/鏡像名:tag
[root@cchuang /]# docker push huangchengcong/diytomcat:1.0
#若報錯,修改tag,帶上版本號
[root@cchuang /]# docker tag afda1a6423f6 huangchengcong/diytomcat:1.0
#再次提交push成功
[root@cchuang /]# docker push huangchengcong/diytomcat:1.0
The push refers to repository [docker.io/huangchengcong/diytomcat]
5f70bf18a086: Pushed
a42a41fd6008: Pushing 12.5MB/259.3MB
8cf305978f90: Pushing 5.036MB/16.17MB
發布到阿里云
1.找到阿里云容器鏡像服務
2.創建命名空間
3.創建鏡像倉庫
4.操作參考官方檔案
小結圖
Docker網路原理
理解Docker0
[root@cchuang /]# ip addr
- docker 是如何處理網路訪問的?
1.我們每啟動一個容器,docker就會為docker容器分配一個ip,只要安裝了docker,就會有一個網卡docker0,橋接模式,使用veth-pair技術,
再啟動一個容器,又多了一對網卡:
# 啟動容器帶來的網卡都是一對一對的,veth-pair技術提供一對虛擬的設備介面,充當一個橋梁,容器和容器之間是可以通信的,
所有的容器不指定網路的情況下,都是通過Docker0路由的,docker會為容器分配一個可用ip,docker中的所有網路介面都是虛擬的,
容器互聯 --link ;,
容器之間可以通過容器名ping通,如:
[root@cchuang ~]# docker exec -it tomcat02 ping tomcat01
#實作,通過 --link,就是在容器的host組態檔新增一條配置
[root@cchuang ~]# docker run -d -P --name tomcat03 --link tomcat02 tomcat
發現tomcat03的host組態檔和tomcat02的聯系
[root@cchuang ~]# docker exec -it tomcat03 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 tomcat02 09abfc1d9356
172.17.0.4 6c3e62552585
自定義網路互聯
#查看所有的docker網路
[root@cchuang ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3368f7e41d43 bridge bridge local
b705923b3209 host host local
e220dcd80b94 none null local
#前面不指定網路的情況下默認引數為 --net bridge 為docker0
[root@cchuang /]# docker run -d -P --name tomcat01 --net bridge tomcat
#docker0默認不支持域名訪問,需要用--link實作
#自定義網路
#--driver bridge 橋接
#--subnet 子網
#--gateway 網關
[root@cchuang /]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
38260ff41b7ad9078a2b765464638c0242dae84aac0fa756646d07bd96de881b
[root@cchuang /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3368f7e41d43 bridge bridge local
b705923b3209 host host local
38260ff41b7a mynet bridge local
e220dcd80b94 none null local
#查看自己配置的mynet網路資訊
[root@cchuang /]# docker inspect mynet net
[
{
"Name": "mynet",
"Id": "38260ff41b7ad9078a2b765464638c0242dae84aac0fa756646d07bd96de881b",
"Created": "2023-04-18T15:09:03.295634613+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
#測驗,啟動兩個容器,指定網路為自定義網路
[root@cchuang /]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
[root@cchuang /]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
#查看mynet網路下的資訊
[root@cchuang /]# docker inspect mynet net
[
{
"Name": "mynet",
"Id": "38260ff41b7ad9078a2b765464638c0242dae84aac0fa756646d07bd96de881b",
"Created": "2023-04-18T15:09:03.295634613+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"8e3125446425fe4c250749bd8f10c048606d3f647af747eb578add88fb27d41b": {
"Name": "tomcat-net-01",
"EndpointID": "df034fd89f6ededfd6032cc552f1d6d993b277fb0afea17174bd2c439f424ac8",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"ee47cd02a96c0f080f9634ec9f6b3c0d3bb01a1c5a502590d857e779e9c31169": {
"Name": "tomcat-net-02",
"EndpointID": "2495488fd7c9656c5b697e37428c9779691be02225082c969d5f856d05569061",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
#自定義網路的好處,可以直接通過容器名ping通,而docker0不指定 -- link是無法通過容器名ping通,而且搭建集群時不同的集群使用不同的網路,保證集群的安全和健康,
網路連通,即如何讓docker0和自定義網路之間實作通訊(本質是容器和網卡實作通訊)
#通過network 的connect引數
[root@cchuang /]# docker network connect --help
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
Connect a container to a network
Options:
--alias strings Add network-scoped alias for the container
--driver-opt strings driver options for the network
--ip string IPv4 address (e.g., "172.30.100.104")
--ip6 string IPv6 address (e.g., "2001:db8::33")
--link list Add link to another container
--link-local-ip strings Add a link-local address for the container
#將mynet網卡和tomcat01連通
[root@cchuang /]# docker network connect mynet tomcat01
#查看mynet網路資訊,發現將tomcat01容器加到mynet中來,即一個容器,兩個ip
部署Redis集群,建立自己的redis網卡,
#1.建立redis網路
[root@cchuang /]# docker network create redis --subnet 172.38.0.0/16
[root@cchuang /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
3368f7e41d43 bridge bridge local
b705923b3209 host host local
38260ff41b7a mynet bridge local
e220dcd80b94 none null local
5db97860681f redis bridge local
#2.通過腳本年創建6個redis組態檔
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
#查看節點
root@cchuang mydata]# ls
redis
[root@cchuang mydata]# cd redis/
[root@cchuang redis]# ls
node-1 node-2 node-3 node-4 node-5 node-6
#啟動redis-1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#啟動redis-2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#啟動redis-3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#啟動redis-4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#啟動redis-5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#啟動redis-6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#查看所有啟動redis
[root@cchuang /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d41d6bc058a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
700afe5dc35f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 38 seconds ago Up 37 seconds 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
94ad1e4dfdb1 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
657ae5c6bb50 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
60ef474566c8 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
427e4900481f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
[root@cchuang /]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
#創建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --
cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 1759e80401711e6eb4d544d69c8ea621460223dc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 17a9936229cacb5416dc66101627aec511c48ae3 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: bf2c752b2119fabae55c3fd83c05f8d240c6b11a 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: f5eccd3cc18d12d8e9fe247deed229a3bb284c33 172.38.0.14:6379
replicates bf2c752b2119fabae55c3fd83c05f8d240c6b11a
S: 40feaf9141203d5753974ebe22463db86ee8a59e 172.38.0.15:6379
replicates 1759e80401711e6eb4d544d69c8ea621460223dc
S: 5428e54dce5556fbe3f588d0a42726d97056ffb7 172.38.0.16:6379
replicates 17a9936229cacb5416dc66101627aec511c48ae3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: 1759e80401711e6eb4d544d69c8ea621460223dc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 40feaf9141203d5753974ebe22463db86ee8a59e 172.38.0.15:6379
slots: (0 slots) slave
replicates 1759e80401711e6eb4d544d69c8ea621460223dc
M: bf2c752b2119fabae55c3fd83c05f8d240c6b11a 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5428e54dce5556fbe3f588d0a42726d97056ffb7 172.38.0.16:6379
slots: (0 slots) slave
replicates 17a9936229cacb5416dc66101627aec511c48ae3
M: 17a9936229cacb5416dc66101627aec511c48ae3 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f5eccd3cc18d12d8e9fe247deed229a3bb284c33 172.38.0.14:6379
slots: (0 slots) slave
replicates bf2c752b2119fabae55c3fd83c05f8d240c6b11a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#連接集群
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:189
cluster_stats_messages_pong_sent:194
cluster_stats_messages_sent:383
cluster_stats_messages_ping_received:189
cluster_stats_messages_pong_received:189
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:383
#查看集群節點資訊
127.0.0.1:6379> cluster nodes
40feaf9141203d5753974ebe22463db86ee8a59e 172.38.0.15:6379@16379 slave 1759e80401711e6eb4d544d69c8ea621460223dc 0 1681806643621 5 connected
bf2c752b2119fabae55c3fd83c05f8d240c6b11a 172.38.0.13:6379@16379 master - 0 1681806644523 3 connected 10923-16383
5428e54dce5556fbe3f588d0a42726d97056ffb7 172.38.0.16:6379@16379 slave 17a9936229cacb5416dc66101627aec511c48ae3 0 1681806643000 6 connected
1759e80401711e6eb4d544d69c8ea621460223dc 172.38.0.11:6379@16379 myself,master - 0 1681806644000 1 connected 0-5460
17a9936229cacb5416dc66101627aec511c48ae3 172.38.0.12:6379@16379 master - 0 1681806644623 2 connected 5461-10922
f5eccd3cc18d12d8e9fe247deed229a3bb284c33 172.38.0.14:6379@16379 slave bf2c752b2119fabae55c3fd83c05f8d240c6b11a 0 1681806644000 4 connected
#測驗,set一個kv
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
#停止172.38.0.13的容器
[root@cchuang ~]# docker stop redis-3
#查看集群節點資訊
127.0.0.1:6379> cluster nodes
40feaf9141203d5753974ebe22463db86ee8a59e 172.38.0.15:6379@16379 slave 1759e80401711e6eb4d544d69c8ea621460223dc 0 1681806643621 5 connected
bf2c752b2119fabae55c3fd83c05f8d240c6b11a 172.38.0.13:6379@16379 master - 0 1681806644523 3 connected 10923-16383
5428e54dce5556fbe3f588d0a42726d97056ffb7 172.38.0.16:6379@16379 slave 17a9936229cacb5416dc66101627aec511c48ae3 0 1681806643000 6 connected
1759e80401711e6eb4d544d69c8ea621460223dc 172.38.0.11:6379@16379 myself,master - 0 1681806644000 1 connected 0-5460
17a9936229cacb5416dc66101627aec511c48ae3 172.38.0.12:6379@16379 master - 0 1681806644623 2 connected 5461-10922
f5eccd3cc18d12d8e9fe247deed229a3bb284c33 172.38.0.14:6379@16379 slave bf2c752b2119fabae55c3fd83c05f8d240c6b11a 0 1681806644000 4 connected
#再去get a,發現依然可以得到,此處的值是從從機得到的,
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
IDEA整合Docker
- 將springboot應用打成jar包,撰寫Dockefile檔案
- 將Dockerfile和jar包上傳到服務器
[root@cchuang home]# cd idea
[root@cchuang idea]# ls
demo-0.0.1-SNAPSHOT.jar Dockerfile
- 通過Dockerfile生成鏡像
[root@cchuang idea]# docker build -t springbootimage .
[+] Building 45.4s (7/7) FINISHED
- 啟動容器
[root@cchuang idea]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springbootimage latest 926a61407aa4 38 seconds ago 661MB
[root@cchuang idea]# docker run -d -P --name springbootimage springbootimage
a628e2ef78ba764bbf908774f71b7a9e0e3113bc309899101c48525278f82940
[root@cchuang idea]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a628e2ef78ba springbootimage "java -jar /app.jar …" 45 seconds ago Up 44 seconds 0.0.0.0:32775->8080/tcp, :::32775->8080/tcp springbootimage
- 訪問測驗,成功
[root@cchuang idea]# curl localhost:32775/hello
hello!!!
Docker Compose容器編排
Docker Swarm集群部署
Jenkins(CI/CD)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/550507.html
標籤:Linux
上一篇:Linux學習筆記