將應用和環境打包乘一個鏡像nginx
[root@docker ~]# docker run -it -v 主機目錄:容器目錄
[root@docker ceshi]# docker run -it -v /home/ceshi/:/home centos /bin/bash
複製代碼
例:sql
雙向過程,在容器內修改東西,宿主機內數據是同步的,反之docker
實戰:Mysql安裝vim
問題:Mysql的數據持久化問題centos
官方啓動方法bash
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tagmarkdown
啓動mysqloop
[root@docker ~]# docker run -d -p 3310: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測試
c751aa1f8c74ff58c5551e8bb62f2c90b58bbab90f7044b296a47fbef8569638
-d 後臺運行
-p 端口映射
-v 卷掛載
-e 環境配置
--name 容器的名字
[root@docker data]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Run 'docker volume COMMAND --help' for more information on a command.
匿名掛載
[root@docker data]# docker run -d -P --name nginx02 -v /etc/nginx nginx
2a51a0e9b09a4358d94784ba13ba6176d14e57672affee96c2f517930a1b351f
[root@docker data]# docker volume ls
DRIVER VOLUME NAME
local 9666e6bffa6452578375314c5e7be127d7819661019c2ad45338ac2be5f0155e
具名掛載
[root@docker data]# docker run -d -P --name nginx01 -v xiaoming-nginx:/etc/nginx nginx
050eb7bf1fe98d95056e3892a3fdba21e86a277f041ce150cd5fac74205c363c
[root@docker data]# docker volume ls
DRIVER VOLUME NAME
local xiaoming-nginx
docker volume inspect volumename
``` 真實位置 [root@docker data]# docker volume inspect xiaoming-nginx [ { "CreatedAt": "2020-11-21T21:07:10-05:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/xiaoming-nginx/_data", "Name": "xiaoming-nginx", "Options": null, "Scope": "local" } ] 查看容器元數據 docker inspect 容器ID 全部docker容器內的卷,沒有指定目錄的狀況下都是在/var/lib/docker/volumes/xxx/_data 經過具名掛載,能夠方便找到卷 [root@docker ~]# mkdir ./docker-test-file [root@docker ~]# cd docker-test-file/ [root@docker docker-test-file]# vim dockerfile01 FROM centos VOLUME ["volume01","volume02"] CMD echo "---HHHHHHHHHHAAAAAAAHHHHHHHHH---" CMD /bin/bash [root@docker docker-test-file]# docker build -t caq/centos:1.0 -f ./dockerfile01 ./ Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos ---> 0d120b6ccaa8 Step 2/4 : VOLUME ["volume01","volume02"] ---> Running in a73113d7e8a3 Removing intermediate container a73113d7e8a3 ---> c711b3f50a7d Step 3/4 : CMD echo "---HHHHHHHHHHAAAAAAAHHHHHHHHH---" ---> Running in 1fb3a0ddd9e4 Removing intermediate container 1fb3a0ddd9e4 ---> 3af9104bbb1a Step 4/4 : CMD /bin/bash ---> Running in 2e403b7bdf30 Removing intermediate container 2e403b7bdf30 ---> d31239b293f0 Successfully built d31239b293f0 Successfully tagged caq/centos:1.0 [root@docker docker-test-file]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE caq/centos 1.0 d31239b293f0 About a minute ago 215MB 進入容器會發現建造dockerfiles時掛載的卷,這種掛載屬於匿名掛載 ![]() ![]() 測試卷同步 [root@docker docker-test-file]# docker run -it d31239b293f0 [root@76da58168b21 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02 [root@76da58168b21 /]# cd volume01 [root@76da58168b21 volume01]# mkdir 11.22 若是不指定宿主機文件,那麼docker會自動分配給宿主機一個位置,經過docker inspect 容器id 可查看到 [root@docker volumes]# cd /var/lib/docker/volumes/3a29756c59c1f5755802283e024f1fec3fa32ed1f3986f663c0326df48950380/_data [root@docker _data]# ls 11.22 實現容器間卷同步 [root@docker ]# docker run -it --name docker01 caq/centos:1.0 /bin/bash [root@docker _data]# docker run -it --name docker02 --volumes-from docker01 caq/centos:1.0 [root@docker _data]# docker exec -it docker01 /bin/bash [root@b2bf96149e27 /]# cd volume01 [root@b2bf96149e27 volume01]# ls docker01 [root@docker _data]# docker exec -it docker02 /bin/bash [root@5a79420489d3 /]# cd volume01 [root@5a79420489d3 volume01]# ls docker01 複製代碼