#直接使用命令来挂载 -v
docker run -it -v 主机目录:容器目录
#测试
root@fanwd-virtual-machine:/home# docker run -it -v /home/ceshi:/home centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
7a0437f04f83: Already exists
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
#启动起来时候我们可以通过docker inspect 容器id
"Mounts": [
{
"Type": "bind",
"Source": "/home/ceshi",
"Destination": "/home",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
#文件测试同步
[root@93ef583c1c73 home]# touch a.java #docker容器指令
[root@93ef583c1c73 home]#
root@fanwd-virtual-machine:/home# cd ceshi/ #linux指令
root@fanwd-virtual-machine:/home/ceshi# ls
root@fanwd-virtual-machine:/home/ceshi# ls
a.java
#再次测试
root@fanwd-virtual-machine:/home/ceshi# vim a.java #linux 同步
root@fanwd-virtual-machine:/home/ceshi# ls
a.java
root@fanwd-virtual-machine:/home/ceshi# cat a.java
nihaoya!!!
root@fanwd-virtual-machine:/home# docker start 93ef583c1c73
93ef583c1c73
root@fanwd-virtual-machine:/home# docker attach 93ef583c1c73 #同步
[root@93ef583c1c73 /]# cd /home
[root@93ef583c1c73 home]# ls
a.java
[root@93ef583c1c73 home]# cat a.java
nihaoya!!!
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 mysql mysql:5.7
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag #docker 官方安装mysql
#下载mysql
docker pull mysql
#运行mysql
# root@fanwd-virtual-machine:/home# 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=1234 --name mysql1 mysql
b8663433f9bb0b9c78b0df93cab868fc7d40104d2974bfb7bf914b21c638220a
#基本指令
-d 后台运行
-p 端口
-v 挂载
-e 环境配置
--name 容器名字
#测试
root@fanwd-virtual-machine:/home# ls
ceshi fanw fanwd mysql '--restart=always' test.java xiaohei.java
root@fanwd-virtual-machine:/home# cd mysql/
root@fanwd-virtual-machine:/home/mysql# ls
conf data
root@fanwd-virtual-machine:/home/mysql# cd data/
root@fanwd-virtual-machine:/home/mysql/data# ls
auto.cnf binlog.index client-cert.pem '#ib_16384_1.dblwr' ib_logfile0 '#innodb_temp' performance_schema server-cert.pem undo_001
binlog.000001 ca-key.pem client-key.pem ib_buffer_pool ib_logfile1 mysql private_key.pem server-key.pem undo_002
binlog.000002 ca.pem '#ib_16384_0.dblwr' ibdata1 ibtmp1 mysql.ibd public_key.pem sys
docker cimmit -m="#提交的描述信息" -a="作者" 容器id 目标镜像名:【标签】
提交镜像
-v 容器内路径
#测试
root@fanwd-virtual-machine:/home# docker run -d -P --name nginzx01 -v /etc/nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
ac2522cc7269: Already exists
09de04de3c75: Pull complete
b0c8a51e6628: Pull complete
08b11a3d692c: Pull complete
a0e0e6bcfd2c: Pull complete
4fcb23e29ba1: Pull complete
Digest: sha256:b0ea179ab61c789ce759dbe491cc534e293428ad232d00df83ce44bf86261179
Status: Downloaded newer image for nginx:latest
cd6f8b86e429cc743e1d6ae7cecda36f8a2572bc90a5bb1948266684cfb182e3
#查看volume的情况
root@fanwd-virtual-machine:/home# docker volume ls
DRIVER VOLUME NAME
local a961f0778e3b0bd96cfa621bead9137504de535a43a2649ee907e2c32e593ed6
具名挂载
#测试
root@fanwd-virtual-machine:/home# docker run -d -P --name xiaohei-nginx -v xiaohei:/etc/nginx nginx
4e498795c0733e8612594f1a4b1e3b2c99904d3fc6200a00102dd2f965dae491
#查看volume的情况
root@fanwd-virtual-machine:/home# docker volume ls
DRIVER VOLUME NAME
local a961f0778e3b0bd96cfa621bead9137504de535a43a2649ee907e2c32e593ed6
local xiaohei
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v 、宿主机路径:容器内路径 #指定路径挂载
#通过 -v 容器内路径:ro rw 改变读写权限
ro readonly #只读
rw readwrite #可读可写
docker run -d -p --name xxxx - v xxx:/etc/nginx:ro nginx
docker run -d -p --name xxxx - v xxx:/etc/nginx:rw nginx
ro这个路径只能通过宿主机来操作 容器内部是无法操作的!
#创建一个镜像文件
root@fanwd-virtual-machine:/home/text1# vim dockerfile1
#文件中的内容
FROM centos
VOLUME ["volume01","volume02"]
CMD echo"---end-----"
CMD /bin/bash
root@fanwd-virtual-machine:/home/text1# docker build -f dockerfile1 -t fanwd/centos .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 300e315adb2f
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 0c496b2192ab
Removing intermediate container 0c496b2192ab
---> 8e28c2fa8f78
Step 3/4 : CMD echo "--------end-------"
---> Running in 7cecfc786724
Removing intermediate container 7cecfc786724
---> d600781d0da1
Step 4/4 : CMD /bin/bash
---> Running in 14fa4b48cb72
Removing intermediate container 14fa4b48cb72
---> 346ac60d6f42
Successfully built 346ac60d6f42
Successfully tagged fanwd/centos:latest
#启动一下自己的镜像
root@fanwd-virtual-machine:/home/text1# docker run -it 346ac60d6f42 /bin/bash
[root@30a2ca9be379 /]# ls -l
total 56
lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin
drwxr-xr-x 5 root root 360 Mar 28 08:11 dev
drwxr-xr-x 1 root root 4096 Mar 28 08:11 etc
drwxr-xr-x 2 root root 4096 Nov 3 15:22 home
lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
drwx------ 2 root root 4096 Dec 4 17:37 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 15:22 media
drwxr-xr-x 2 root root 4096 Nov 3 15:22 mnt
drwxr-xr-x 2 root root 4096 Nov 3 15:22 opt
dr-xr-xr-x 300 root root 0 Mar 28 08:11 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 run
lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 15:22 srv
dr-xr-xr-x 13 root root 0 Mar 28 08:11 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
drwxr-xr-x 2 root root 4096 Mar 28 08:11 volume01
drwxr-xr-x 2 root root 4096 Mar 28 08:11 volume02 #生成镜像的时候自动挂载的数据卷目录