將應用和環境打包成一個鏡像
需求:數據能夠持久化和同步mysql
docker run -it -v 主機目錄:容器內目錄 # 測試 [root@hwh1 ~]# docker run -it -v /home/test:/home tomcat10.0.0:1.0 /bin/bash [root@hwh1 ~]# docker inspect 4446060c16ca ....... "Mounts": [ { "Type": "bind", "Source": "/home/test", "Destination": "/home", "Mode": "", "RW": true, "Propagation": "rprivate" } ], ....... [root@hwh1 home]# ls hwh test # 能夠看到已經有文件夾了 # 測試容器內新增文件是否會添加到 主機 root@3e7209654d79:/home# touch test1.txt root@3e7209654d79:/home# ls test1.txt # 主機內同步添加 [root@hwh1 test]# pwd /home/test [root@hwh1 test]# ls test1.txt # 測試當容器中止時,在主機上添加文件 從新啓動後 會發現容器中也會增長 # 新增文件 [root@hwh1 test]# touch test2.txt [root@hwh1 test]# ls test1.txt test2.txt # 啓動容器 [root@hwh1 ~]# docker start 3e7209654d79 3e7209654d79 [root@hwh1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3e7209654d79 tomcat10.0.0:1.0 "/bin/bash" 4 hours ago Up 3 seconds 8080/tcp practical_archimedes # 進入容器查看 [root@hwh1 ~]# docker attach 3e7209654d79 root@3e7209654d79:/usr/local/tomcat# cd /home/ root@3e7209654d79:/home# ls test1.txt test2.txt
# 拉取鏡像 [root@hwh1 ~]# docker pull mysql:5.7 [root@hwh1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.7 9cfcce23593a 2 weeks ago 448MB # 運行容器 掛載數據,設置密碼 [root@hwh1 ~]# docker run -d -p 3306:3306 -v /home/mysql/conf/:/etc/mysql/conf.d -v /home/mysql/data/:/var/lib/mysql --name mysql1.0 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 # -d 後臺運行 # -p 暴露端口 # -v 卷掛載 # --name 設置名字 # -e 環境配置 # MYSQL_ROOT_PASSWORD=123456 mysql密碼設置 [root@hwh1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3954a6cf0d8a mysql:5.7 "docker-entrypoint.s…" 10 seconds ago Up 8 seconds 33060/tcp, 0.0.0.0:3306->3306/tcp mysql1.0 # 用 mysql 鏈接工具測試 # 查看 data [root@hwh1 data]# pwd /home/mysql/data [root@hwh1 data]# ls auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem
# 查看全部卷 的狀況 [root@hwh1 ~]# docker volume --help Usage: docker volume COMMAND Manage volumes Commands: create Create a volume 建立一個 volume inspect Display detailed information on one or more volumes 顯示一個或多個卷的詳細信息 ls List volumes 列表查看 volume prune Remove all unused local volumes 移除全部不用的 volumes rm Remove one or more volumes 移除一個或者多個 volumes # 匿名掛載 # -v 容器內路徑 [root@hwh1 ~]# docker run -d --name nginx02 -P -v /etc/nginx nginx:1.19.0 1baf9eb9dc56967b790999942bdb346a8ade8eff8636f1776ef7996eb63ec2ae # 由於在掛載的時候,只寫了容器內路徑,而沒有寫 主機路徑,這就是匿名掛載 [root@hwh1 ~]# docker volume ls DRIVER VOLUME NAME local 0586a6dd5c3052d18e9baff561bc0c2fb310428b3791efa80d424646e0a6912a # 具名掛載 # -v 卷名:容器內目錄 [root@hwh1 ~]# docker run -d -P -v obviousNginx:/etc/nginx nginx:1.19.0 cb7f8b00b8b5ff926696e48f63fe7ce71db8de5332d41cbd3ce1d5efbd780bf8 [root@hwh1 ~]# docker volume ls DRIVER VOLUME NAME local obviousNginx [root@hwh1 ~]# docker volume inspect obviousNginx [ { "CreatedAt": "2020-06-24T20:50:01+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/obviousNginx/_data", "Name": "obviousNginx", "Options": null, "Scope": "local" } ]
全部的 docker容器內的卷,沒有指定目錄的狀況下都是在 「/var/lib/docker/volumes/卷名/_data」 下
咱們經過具名掛載能夠方便的找到咱們的一個卷,大多數狀況都是在使用 具名掛載
nginx
-v 容器內路徑 # 一 匿名掛載 -v 卷名:容器內目錄 # 二 具名掛載 -v 主機目錄:容器內目錄 # 三 指定路徑掛載
# 設置權限 ro rw 改變讀寫權限 # -v 容器內路徑:ro/rw ro readonly # 只讀,只能經過主機操做,容器內部沒法操做 rw readwrite # 可讀可寫
Dockerfile就是用來構建 docker鏡像的構建文件sql
# 簡單構建測試 [root@hwh1 home]# mkdir dockerfile_test [root@hwh1 home]# cd dockerfile_test/ [root@hwh1 dockerfile_test]# pwd /home/dockerfile_test [root@hwh1 dockerfile_test]# vim dockerfile1 [root@hwh1 dockerfile_test]# cat dockerfile1 FROM centos VOLUME ["volume01","volume02"] # 掛載目錄,匿名掛載 CMD echo"success" # 輸出成功證實腳本運行成功 CMD /bin/bash # 默認 bash 控制檯 # -f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') dockerfile 路徑 # --tag list Name and optionally a tag in the 'name:tag' format 版本 [root@hwh1 dockerfile_test]# docker build -f dockerfile1 -t centos:1.0 . # 注意這裏有個點 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos # 分佈設置 latest: Pulling from library/centos 6910e5a164f7: Pull complete Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b Status: Downloaded newer image for centos:latest ---> 831691599b88 Step 2/4 : VOLUME ["volume01","volume02"] # 掛載目錄 ---> Running in 982ae0c60dd5 Removing intermediate container 982ae0c60dd5 ---> 8c5dc67775f3 Step 3/4 : CMD echo"success" ---> Running in 5c3d0ee1ba60 Removing intermediate container 5c3d0ee1ba60 ---> e391fd4d40e6 Step 4/4 : CMD /bin/bash ---> Running in 2ec7cb4632dc Removing intermediate container 2ec7cb4632dc ---> 4f7354d57ff3 Successfully built 4f7354d57ff3 Successfully tagged centos:1.0 [root@hwh1 dockerfile_test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 1.0 4f7354d57ff3 27 seconds ago 215MB
# 啓動本身寫的鏡像測試 [root@hwh1 ~]# docker run -it 4f7354d57ff3 /bin/bash [root@1d2a9a575139 /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin drwxr-xr-x. 5 root root 360 Jun 28 13:48 dev drwxr-xr-x. 1 root root 66 Jun 28 13:48 etc drwxr-xr-x. 2 root root 6 May 11 2019 home lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwx------. 2 root root 6 Jun 11 02:35 lost+found drwxr-xr-x. 2 root root 6 May 11 2019 media drwxr-xr-x. 2 root root 6 May 11 2019 mnt drwxr-xr-x. 2 root root 6 May 11 2019 opt dr-xr-xr-x. 350 root root 0 Jun 28 13:48 proc dr-xr-x---. 2 root root 162 Jun 11 02:35 root drwxr-xr-x. 11 root root 163 Jun 11 02:35 run lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 May 11 2019 srv dr-xr-xr-x. 13 root root 0 Jun 28 08:21 sys drwxrwxrwt. 7 root root 145 Jun 11 02:35 tmp drwxr-xr-x. 12 root root 144 Jun 11 02:35 usr drwxr-xr-x. 20 root root 262 Jun 11 02:35 var drwxr-xr-x. 2 root root 6 Jun 28 13:48 volume02 drwxr-xr-x. 2 root root 6 Jun 28 13:48 volume01
# 只有容器內路徑掛載,匿名掛載 # 外部查看 [root@hwh1 ~]# docker volume ls DRIVER VOLUME NAME local 3147ec56df4ec370dadee55edc42c5f8a10d2e352b5eb0d74969b7ff64b06670 local 351272828a359dd2095d9f9caced693a1bf6272924637e3edfdead2945dd0782 # 查看容器的外部路徑 [root@hwh1 ~]# docker inspect 1d2a9a575139 ....... "Mounts": [ { "Type": "volume", "Name": "351272828a359dd2095d9f9caced693a1bf6272924637e3edfdead2945dd0782", "Source": "/var/lib/docker/volumes/351272828a359dd2095d9f9caced693a1bf6272924637e3edfdead2945dd0782/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "3147ec56df4ec370dadee55edc42c5f8a10d2e352b5eb0d74969b7ff64b06670", "Source": "/var/lib/docker/volumes/3147ec56df4ec370dadee55edc42c5f8a10d2e352b5eb0d74969b7ff64b06670/_data", "Destination": "volum02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], .......
若是你有一些要在容器之間共享的持久性數據,或者但願在非持久容器中使用,那麼最好建立一個命名的數據卷容器,而後從其掛載數據。
實現兩個或者多個容器之間實現數據同步和共享docker
# 啓動測試 用剛剛建立的鏡像測試,啓動三個容器 [root@hwh1 ~]# docker run -it --name docker01 centos:1.0 [root@6815ba3aebfa /]# # Ctrl + P +Q 不中止容器退出 # --volumes-from 指定容器載入卷 [root@hwh1 ~]# docker run -it --name docker02 --volumes-from docker01 centos:1.0 # 到 docker01 中的 volume01 或者 volume02 進行修改測試,查看是否同步到 docker02 [root@hwh1 ~]# docker exec -it cc6698c7e1ae /bin/bash [root@cc6698c7e1ae volume01]# pwd /volume01 [root@cc6698c7e1ae volume01]# mkdir test # 新增測試 [root@cc6698c7e1ae volume01]# ls test # 回到 docker02 查看,已經實現同步數據 [root@f71dd97f742f /]# cd volume01 [root@f71dd97f742f volume01]# ls test # 建立第三個容器 測試同步 [root@hwh1 ~]# docker run -it --name docker03 --volumes-from docker01 centos:1.0 [root@06b74c606532 /]# cd volume01 [root@06b74c606532 volume01]# ls test # 一樣能夠同步數據 # 測試刪除 docker01 ,查看文件是否還存在 [root@hwh1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06b74c606532 centos:1.0 "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker03 f71dd97f742f centos:1.0 "/bin/sh -c /bin/bash" 9 minutes ago Up 9 minutes docker02 cc6698c7e1ae centos:1.0 "/bin/sh -c /bin/bash" 30 minutes ago Up 30 minutes docker01 [root@hwh1 ~]# docker rm -f cc6698c7e1ae cc6698c7e1ae # docker02 還存在原來的文件 [root@f71dd97f742f /]# cd volume01 [root@f71dd97f742f volume01]# ls test # 原理:至關於雙向複製進行備份的形式,進行同步數據 # 在這種狀況下,若是移除其中已經掛載的容器,不管是移除哪一個,都不會被移除。 # 要將卷從硬盤上刪除,必須使用 docker rm -v 刪除全部引用該卷的容器