一、CentOS7安装docker
1、安装
Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10, CentOS 7 满足最低内核的要求,所以我们在CentOS 7安装Docker。
-
卸载旧docker
如果之前安装过旧版本的Docker,可以使用下面命令卸载:
yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-selinux \docker-engine-selinux \docker-engine \docker-ce
-
安装yum工具
yum install -y yum-utils \# device-mapper-persistent-data \# lvm2 --skip-broken
-
然后更新本地镜像源:
# 设置docker镜像源 yum-config-manager \--add-repo \https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-
输入命令:
yum install -y docker-ce
docker-ce为社区免费版本。稍等片刻,docker即可安装成功。
2、启动
- 输入docker -v有显示并不意味着安装成功;输入docker images如下显示,因为没有启动docker
完毕!
[root@localhost home]# docker -v
Docker version 24.0.7, build afdd53b
[root@localhost home]#
[root@localhost home]#
[root@localhost home]# docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
- Docker应用需要用到各种端口,逐一去修改防火墙设置。非常麻烦,因此直接关闭防火墙。启动docker前,一定要关闭防火墙!!
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
- 通过命令启动docker:
systemctl start docker # 启动docker服务systemctl stop docker # 停止docker服务systemctl restart docker # 重启docker服务
控制台
[root@localhost home]# systemctl stop firewalld
[root@localhost home]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost home]# systemctl start docker
[root@localhost home]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:docker.socket
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost home]# systemctl restart docker
3、配置镜像加速
ocker官方镜像仓库网速较差,我们需要设置国内镜像服务:
参考阿里云的镜像加速文档:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
配置镜像加速器的地址
---命令1
sudo mkdir -p /etc/docker
---命令2
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://xxxxxywk.mirror.aliyuncs.com"]
}
EOF
---命令3
sudo systemctl daemon-reload
---命令4
sudo systemctl restart docker
二、部署MySQL
只需一行命令就能安装mysql
[root@localhost docker]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysqldocker run -d --name nacos \-p 8848:8848 \-e MODE=standalone \-e SPRING_DATASOURCE_PLATFORM=mysql \-e MYSQL_SERVICE_HOST=192.168.200.131 \-e MYSQL_SERVICE_DB_NAME=nacos_config \-e MYSQL_SERVICE_USER=root \-e MYSQL_SERVICE_PASSWORD=root \nacos/nacos-server:v2.1.0
运行命令,设置mysql密码为root
[root@localhost docker]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysql
Unable to find image 'mysql:latest' locally
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
e3aaa62878d1a89635b7f39ec6e1c9b258a4d2a63ee90155473114d03dca0cc9
[root@localhost docker]#
用navicat连接虚拟机地址的mysql,连接成功
三、docker命令
1、常用命令
-
docker run :创建并运行一个新的容器
-
-d 让容器在后台运行
-
–name mysql 给容器起个名字叫mysql,必须唯一
-
-p 3306:3306 端口映射,宿主机端口和容器内端口,容器内端口取决于容器,不改动;左边宿主机端口可以改动。可以通过访问宿主机的3306端口访问到容器内部的3306端口
-
-e KEY=VALUE 设置环境变量,由镜像决定,需要查看镜像的文档说明
-
mysql 指定运行的镜像的名称,即是什么镜像;正确写法mysql:5.7
- [repository]:[tag] 标准写法,镜像名:版本号,tag不写默认latest
-
-
docker stop:停止容器内部的进程,并非停止整个容器,容器还存在
-
docker start:启动容器内的进程(不会创建新的容器)
-
docker ps:查看容器的运行状态,默认只显示运行中的容器
- -a:所有的包括已停止的容器
-
docker inspect:查看容器详情,ip,端口,镜像,数据卷挂载等信息
-
docker rm:删掉容器
-
docekr exec :进入到容器内部,后续可以在容器内部做一些处理
docker exec -it [容器名] /bin/bash
-
docker logs:查看日志
-
docker pull:从docker仓库里拉取一个镜像
-
docekr push:将镜像推到仓库
-
docker images:查看本地已经存在了哪些镜像
-
docker rmi:本地删除镜像
2、自定义镜像相关命令
- docker build:基于dockerfile构建自定义镜像
- docker save:将镜像保存为压缩文件
- docker load:将压缩文件加载为镜像
- docker push:将镜像推到仓库
三、部署Nginx
- 拉取镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]#
- 查看本地存在的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 22 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
[root@localhost ~]#
- 将镜像保存为压缩文件。ll命令为 ls -l的缩写
[root@localhost ~]# docker save --helpUsage: docker save [OPTIONS] IMAGE [IMAGE...]Save one or more images to a tar archive (streamed to STDOUT by default)Aliases:docker image save, docker saveOptions:-o, --output string Write to a file, instead of STDOUT
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1261 11月 16 16:42 anaconda-ks.cfg
drwxr-xr-x. 2 root root 20 11月 17 08:58 test_order
[root@localhost ~]# docker save -o nginx.tar nginx:latest
[root@localhost ~]# ll
总用量 142492
-rw-------. 1 root root 1261 11月 16 16:42 anaconda-ks.cfg
-rw-------. 1 root root 145905152 11月 17 15:28 nginx.tar
drwxr-xr-x. 2 root root 20 11月 17 08:58 test_order
[root@localhost ~]#
- 删除nginx:latest镜像
[root@localhost ~]# docker rmi nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 23 months ago 516MB
[root@localhost ~]#
- 加载镜像压缩文件
[root@localhost ~]# docker load --helpUsage: docker load [OPTIONS]Load an image from a tar archive or STDINAliases:docker image load, docker loadOptions:-i, --input string Read from tar archive file, instead of STDIN-q, --quiet Suppress the load output
[root@localhost ~]# docker load -i nginx.tar
2edcec3590a4: Loading layer [==================================================>] 83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>] 62MB/62MB
b8d6e692a25e: Loading layer [==================================================>] 3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>] 4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>] 3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>] 7.168kB/7.168kB
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 22 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
[root@localhost ~]#
- 创建并运行一个容器
[root@localhost ~]# docker run -d --name nginx -p 80:80 nginx
d23e74f2b63699635312a58dbd2cd1a1dbf95c8816e8a3a91e44dbf4f082a4de
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23e74f2b636 nginx "/docker-entrypoint.…" 13 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
e3aaa62878d1 mysql "docker-entrypoint.s…" 4 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]#
- 使docker ps的以规定格式输出内容
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID IMAGE PORTS STATUS NAMES
d23e74f2b636 nginx 0.0.0.0:80->80/tcp, :::80->80/tcp Up 4 minutes nginx
e3aaa62878d1 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up 4 hours mysql
[root@localhost ~]#
- 停掉容器
[root@localhost ~]# docker stop nginx
nginx
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}"
CONTAINER ID IMAGE PORTS STATUS NAMES
e3aaa62878d1 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up 4 hours mysql
[root@localhost ~]# docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}\t{{.Names}}" -a
CONTAINER ID IMAGE PORTS STATUS NAMES
d23e74f2b636 nginx Exited (0) 44 seconds ago nginx
e3aaa62878d1 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up 4 hours mysql
[root@localhost ~]#
- 启动容器
[root@localhost ~]# docker start nginx
nginx
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23e74f2b636 nginx "/docker-entrypoint.…" 11 minutes ago Up 6 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
e3aaa62878d1 mysql "docker-entrypoint.s…" 4 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ~]#
- 查看日志;docker logs -f 会持续跟踪日志–follow
[root@localhost ~]# docker logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/11/17 07:33:50 [notice] 1#1: using the "epoll" event method
2023/11/17 07:33:50 [notice] 1#1: nginx/1.21.5
2023/11/17 07:33:50 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/11/17 07:33:50 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2023/11/17 07:33:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2023/11/17 07:33:50 [notice] 1#1: start worker processes
2023/11/17 07:33:50 [notice] 1#1: start worker process 31
2023/11/17 07:43:37 [notice] 1#1: signal 3 (SIGQUIT) received, shutting down
2023/11/17 07:43:37 [notice] 31#31: gracefully shutting down
2023/11/17 07:43:37 [notice] 31#31: exiting
2023/11/17 07:43:37 [notice] 31#31: exit
2023/11/17 07:43:37 [notice] 1#1: signal 17 (SIGCHLD) received from 31
2023/11/17 07:43:37 [notice] 1#1: worker process 31 exited with code 0
2023/11/17 07:43:37 [notice] 1#1: exit
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/11/17 07:45:28 [notice] 1#1: using the "epoll" event method
2023/11/17 07:45:28 [notice] 1#1: nginx/1.21.5
2023/11/17 07:45:28 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/11/17 07:45:28 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64
2023/11/17 07:45:28 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2023/11/17 07:45:28 [notice] 1#1: start worker processes
2023/11/17 07:45:28 [notice] 1#1: start worker process 24
[root@localhost ~]#
- 进入容器内部 docker exec -it nginx bash(-it是添加一个可输入的终端,使用bash进行交互)
进入nginx容器
[root@localhost ~]# docker exec -it nginx bash
root@d23e74f2b636:/#
root@d23e74f2b636:/# ls -l
total 12
drwxr-xr-x. 2 root root 4096 Dec 20 2021 bin
drwxr-xr-x. 2 root root 6 Dec 11 2021 boot
drwxr-xr-x. 5 root root 340 Nov 17 07:45 dev
drwxr-xr-x. 1 root root 41 Dec 29 2021 docker-entrypoint.d
-rwxrwxr-x. 1 root root 1202 Dec 29 2021 docker-entrypoint.sh
drwxr-xr-x. 1 root root 19 Nov 17 07:33 etc
drwxr-xr-x. 2 root root 6 Dec 11 2021 home
drwxr-xr-x. 1 root root 45 Dec 20 2021 lib
drwxr-xr-x. 2 root root 34 Dec 20 2021 lib64
drwxr-xr-x. 2 root root 6 Dec 20 2021 media
drwxr-xr-x. 2 root root 6 Dec 20 2021 mnt
drwxr-xr-x. 2 root root 6 Dec 20 2021 opt
dr-xr-xr-x. 148 root root 0 Nov 17 07:45 proc
drwx------. 2 root root 37 Dec 20 2021 root
drwxr-xr-x. 1 root root 23 Nov 17 07:45 run
drwxr-xr-x. 2 root root 4096 Dec 20 2021 sbin
drwxr-xr-x. 2 root root 6 Dec 20 2021 srv
dr-xr-xr-x. 13 root root 0 Nov 17 00:42 sys
drwxrwxrwt. 1 root root 6 Dec 29 2021 tmp
drwxr-xr-x. 1 root root 66 Dec 20 2021 usr
drwxr-xr-x. 1 root root 19 Dec 20 2021 var
root@d23e74f2b636:/# exit
exit
[root@localhost ~]#
进入mysql容器
[root@localhost ~]# docker exec -it mysql bash
root@e3aaa62878d1:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.27 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.86 sec)mysql> exit
Bye
root@e3aaa62878d1:/# exit
exit
[root@localhost ~]#
- 删除容器。默认不能删运行中的容器。可以先stop后rm或者rm -f。
[root@localhost ~]# docker rm mysql
Error response from daemon: You cannot remove a running container e3aaa62878d1a89635b7f39ec6e1c9b258a4d2a63ee90155473114d03dca0cc9. Stop the container before attempting removal or force remove
[root@localhost ~]# docker rm mysql -f
mysql
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d23e74f2b636 nginx "/docker-entrypoint.…" 24 minutes ago Up 13 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@localhost ~]#
四、命令别名
~表示root目录
[root@localhost ~]# vi ~/.bashrc
[root@localhost ~]#
[root@localhost ~]# source ~/.bashrc //让命令生效
[root@localhost ~]#
[root@localhost ~]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
d23e74f2b636 nginx 0.0.0.0:80->80/tcp, :::80->80/tcp Up 21 minutes nginx
[root@localhost ~]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
d23e74f2b636 nginx 0.0.0.0:80->80/tcp, :::80->80/tcp Up 21 minutes nginx
[root@localhost ~]#
五、数据卷挂载
1、修改Nginx静态html文件
目标:1、修改nginx容器内的html目录下的index.html文件内容;2、将静态资源部署到nginx的html目录
- 启动服务
[root@localhost test_order]# systemctl start docker
[root@localhost test_order]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
[root@localhost test_order]#
- 启动nginx容器
[root@localhost test_order]# docker start nginx
nginx
[root@localhost test_order]#
- 进入到容器内部
[root@localhost test_order]# docker exec -it nginx bash
root@d23e74f2b636:/#
- 进入到nginx的静态资源目录/usr/share/nginx/html。发现不能使用vi命令。
root@d23e74f2b636:/# cd /usr/share/nginx/html
root@d23e74f2b636:/usr/share/nginx/html# ll
bash: ll: command not found
root@d23e74f2b636:/usr/share/nginx/html# ls
50x.html index.html
root@d23e74f2b636:/usr/share/nginx/html# vi index.html
bash: vi: command not found
root@d23e74f2b636:/usr/share/nginx/html#
2、数据卷
数据卷:是一个虚拟目录,是容器内目录与宿主机目录之间映射的桥梁。一旦数据卷产生,会实现两个目录的双向绑定。
因此,可以直接通过在宿主机上修改文件,容器相对应的容器也会被修改。
docker volume --help
数据卷挂载:
在执行docker run命令是,使用 -v 数据卷:容器内目录 可以完成数据卷挂载(只能在这一步挂载,不能在容器创建之后);当创建容器时,如果挂载了数据卷且数据卷不存在,会自动创建数据卷
数据卷挂载后的命令:
命令 | 说明 |
---|---|
docker colume create | 创建数据卷 |
docker volume ls | 查看所有数据卷 |
docker volume rm | 删除指定数据卷 |
docker volume inspect [name] | 查看某个数据卷详情 |
docker volume prune | 清除未使用的数据卷 |
3、重新创建容器并挂载数据卷
- 删除之前的nginx容器并重新创建,同时挂载一个html数据卷,挂载的容器内目录为/usr/share/nginx/html
[root@localhost test_order]# docker rm -f nginx
nginx
[root@localhost test_order]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
[root@localhost test_order]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost test_order]# docker run -d --name nginx -p 80:80 -v html:/usr/share/nginx/html nginx
243562e523d1f52211539fa8dd2908eedf6586b8b11896ccbd216140d9898252
[root@localhost test_order]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
243562e523d1 nginx "/docker-entrypoint.…" 13 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@localhost test_order]#
- 查看数据卷详情,数据卷被挂载到/var/lib/docker/volumes/下,且/var/lib/docker/volumes/html/_data下的文件与nginx容器内/usr/share/nginx/html/下人文件相同
[root@localhost test_order]# docker volume ls
DRIVER VOLUME NAME
local d5a198ffeee2c28589adb04619336b1236035473033c11c37055c4a1c7d3c792
local html
[root@localhost test_order]# docker volume inspect html
[{"CreatedAt": "2023-11-20T10:02:41+08:00","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/html/_data","Name": "html","Options": null,"Scope": "local"}
]
[root@localhost test_order]# cd /var/lib/docker/volumes/html/_data
[root@localhost _data]# ll
总用量 8
-rw-r--r--. 1 root root 497 12月 28 2021 50x.html
-rw-r--r--. 1 root root 615 12月 28 2021 index.html
[root@localhost _data]#
-
直接在/var/lib/docker/volumes/html/_data目录下修改index.html文件
-
成功修改
-
此外,可以将资源放到/var/lib/docker/volumes/html/_data目录下实现数据迁移。
六、本地目录挂载
数据卷挂载的弊端:1、匿名数据卷不方便使用;2、宿主机目录固定为/var/lib/docker/volumes/
本地目录挂载:在执行docker run命令时,使用 -v 本地目录:容器内目录 可以完成本地目录挂载(只能在这一步挂载,不能在容器创建之后);本地目录必须以"/“或”./"开头,否则会被识别为数据卷而非本地目录
七、自定义镜像
1、DockerFile语法
部署一个Java应用的步骤:
1、准备一个linux服务器
2、安装JRE并配置环境变量
3、拷贝JAR包
4、运行JAR包
指令 | 说明 | 示例 |
---|---|---|
FROM | 指定基础镜像 | FROM centos:6 |
ENV | 设置环境变量,可在后面指令使用 | ENV key value |
COPY | 拷贝本地文件到镜像的指定目录 | COPY ./mysql-5.7.rpm /tmp |
RUN | 执行Linux的shell命令,一般是安装过程的命令 | RUN yum install gcc |
EXPOSE | 指定容器运行时监听的端口,是给镜像使用者看的 | EXPOSE 8080 |
ENTRYPOINT | 镜像中应用的启动命令,容器运行时调用 | ENTRYPOINT java -jar xx.jar |
2、自定义docker-demo镜像
-
Dockerfile文件
-
文件结构
[root@localhost demo]# ll
总用量 25024
-rw-r--r--. 1 root root 25620395 11月 20 13:59 docker-demo.jar
-rw-r--r--. 1 root root 250 11月 20 13:58 Dockerfile
[root@localhost demo]# tree
.
├── docker-demo.jar
└── Dockerfile0 directories, 2 files
[root@localhost demo]#
- 下载openjdk:11.0-jre-buster基础镜像
[root@localhost demo]# docker pull openjdk:11.0-jre-buster
11.0-jre-buster: Pulling from library/openjdk
c4cc477c22ba: Pull complete
077c54d048f1: Pull complete
0368544993b2: Pull complete
d2b3c389e55f: Pull complete
7fde22603cbd: Pull complete
e540594d7c47: Pull complete
Digest: sha256:3546a17e6fb4ff4fa681c38f3f6644efd393f9bb7ed6ebbd85f06065f5d570ed
Status: Downloaded newer image for openjdk:11.0-jre-buster
docker.io/library/openjdk:11.0-jre-buster
[root@localhost demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
openjdk 11.0-jre-buster 57925f2e4cff 23 months ago 301MB
[root@localhost demo]#
- 构建自定义镜像
[root@localhost demo]# docker build -t docker-demo .
[+] Building 4.8s (8/8) FINISHED docker:default=> [internal] load build definition from Dockerfile 0.0s=> => transferring dockerfile: 349B 0.0s=> [internal] load .dockerignore 0.0s=> => transferring context: 2B 0.0s=> [internal] load metadata for docker.io/library/openjdk:11.0-jre-buster 0.0s=> [1/3] FROM docker.io/library/openjdk:11.0-jre-buster 0.1s=> [internal] load build context 4.1s=> => transferring context: 25.63MB 4.0s=> [2/3] RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone 4.4s=> [3/3] COPY docker-demo.jar /app.jar 0.1s=> exporting to image 0.1s=> => exporting layers 0.1s=> => writing image sha256:b659fb9f1b714016b9a0a7d15346046e7fa710204aa3c301d02e35a3f37ca2f0 0.0s=> => naming to docker.io/library/docker-demo 0.0s
[root@localhost demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-demo latest b659fb9f1b71 46 seconds ago 327MB
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
openjdk 11.0-jre-buster 57925f2e4cff 23 months ago 301MB
[root@localhost demo]#
- 创建容器
[root@localhost demo]# docker run -d --name dd -p 8090:8090 docker-demo
f2fbba619278e83b7510ed17a5bed4d26abc447cf5a7923fa8fc8b812b1c2394
[root@localhost demo]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
f2fbba619278 docker-demo 0.0.0.0:8090->8090/tcp, :::8090->8090/tcp Up 8 seconds dd
- 浏览器测试
- 查看容器日志
以上说明自定义镜像构建成功
八、容器网络互联
命令 | 说明 |
---|---|
docker network create | 创建一个网络 |
docker network ls | 查看指定网络 |
docker network rm | 删除指定网络 |
docker network prune | 清除未使用的网络 |
docker network connect | 使指定容器连接加入某网路 |
docker network disconnect | 使指定容器连接离开某网路 |
docker network inspect | 查看网络详细信息 |
目标:使dd容器能够连接mysql容器
- 创建新的网络testNet。其中bridge(即docker0)是docker自带的默认网桥,容器创建时如果没有指定网络,则默认处于该连接下。
[root@localhost demo]# docker network ls
NETWORK ID NAME DRIVER SCOPE
76d6c5c9df88 bridge bridge local
5072bd55e284 host host local
74ff2fed89b9 none null local
[root@localhost demo]# docker network create testNet
9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91
[root@localhost demo]# docker network ls
NETWORK ID NAME DRIVER SCOPE
76d6c5c9df88 bridge bridge local
5072bd55e284 host host local
74ff2fed89b9 none null local
9e0f10bb1bf2 testNet bridge local
[root@localhost demo]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:a0:84:74 brd ff:ff:ff:ff:ff:ffinet 192.168.179.128/24 brd 192.168.179.255 scope global noprefixroute dynamic ens33valid_lft 1212sec preferred_lft 1212secinet6 fe80::e391:3f36:2ff2:da5c/64 scope link noprefixroute valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:2b:a2:a2:50 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft foreverinet6 fe80::42:2bff:fea2:a250/64 scope link valid_lft forever preferred_lft forever
13: veth160830c@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether da:ea:f1:b8:42:80 brd ff:ff:ff:ff:ff:ff link-netnsid 0inet6 fe80::d8ea:f1ff:feb8:4280/64 scope link valid_lft forever preferred_lft forever
14: br-9e0f10bb1bf2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:69:ec:d6:61 brd ff:ff:ff:ff:ff:ffinet 172.18.0.1/16 brd 172.18.255.255 scope global br-9e0f10bb1bf2valid_lft forever preferred_lft forever
[root@localhost demo]#
- mysql容器连接到testNet。发现mysql处于两个网络。
[root@localhost demo]# docker run -d --name mysql -p 3306:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=root mysql
0186ddeb4c108d3accd8068ffee6bd86be3e5d2336b6ae3bd8ffff64c7595f71
[root@localhost demo]# dps
CONTAINER ID IMAGE PORTS STATUS NAMES
0186ddeb4c10 mysql 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp Up 2 seconds mysql
f2fbba619278 docker-demo 0.0.0.0:8090->8090/tcp, :::8090->8090/tcp Up 35 minutes dd
[root@localhost demo]# docker network connect testNet mysql
[root@localhost demo]# docker inspect mysql
......"Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "76d6c5c9df88a644a949b060ff4f188dde5db61ecc42efc9f7358a36d4f164b7","EndpointID": "806490529ffde5e5860f416fa0df12361927ae67fcbd8b3364bf91c6a96a8c51","Gateway": "172.17.0.1","IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:11:00:03","DriverOpts": null},"testNet": {"IPAMConfig": {},"Links": null,"Aliases": ["0186ddeb4c10"],"NetworkID": "9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91","EndpointID": "35f2059a5331e5221a4d6346762c74e1f213376a47b19ad5962797abc29ebb4d","Gateway": "172.18.0.1","IPAddress": "172.18.0.2","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:12:00:02","DriverOpts": {}}}
......
- 删除dd容器,重新创建,且在创建时就指定网络,发现dd容器没有默认的bridge网络了。
[root@localhost demo]# docker rm -f dd
dd
[root@localhost demo]# docker run -d --name dd -p 8090:8090 --network testNet docker-demo
266c61091e6fd535e83824c32eb0280a12f8004880eee23d5d2a6b4ef784c36d
[root@localhost demo]# docker inspect dd
......"Networks": {"testNet": {"IPAMConfig": null,"Links": null,"Aliases": ["266c61091e6f"],"NetworkID": "9e0f10bb1bf25b5e97e87af223e5a061d11a9a17d125cd508144fb7d223f1b91","EndpointID": "ada42e9ad84f8b4307ebfe32fa1d08416b651dbe7528af76bffaf152783b97e6","Gateway": "172.18.0.1","IPAddress": "172.18.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "02:42:ac:12:00:03","DriverOpts": null}}
......
- 这时容器可以通过容器名进行网络互联(因为处于同一个网桥里),但是默认的bridge不可以,只能通过ip地址连接(但是没luan用,因为每次启动ip地址会变化)。
[root@localhost demo]# docker exec -it dd bash
root@266c61091e6f:/# ping mysql
PING mysql (172.18.0.2) 56(84) bytes of data.
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=3 ttl=64 time=0.039 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=4 ttl=64 time=0.085 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=5 ttl=64 time=0.086 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=6 ttl=64 time=0.081 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=7 ttl=64 time=0.067 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=8 ttl=64 time=0.105 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=9 ttl=64 time=0.060 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=10 ttl=64 time=0.080 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=11 ttl=64 time=0.052 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=12 ttl=64 time=0.061 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=13 ttl=64 time=0.108 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=14 ttl=64 time=0.044 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=15 ttl=64 time=0.049 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=16 ttl=64 time=0.110 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=17 ttl=64 time=0.084 ms
64 bytes from mysql.testNet (172.18.0.2): icmp_seq=18 ttl=64 time=0.057 ms
^C
--- mysql ping statistics ---
18 packets transmitted, 18 received, 0% packet loss, time 21ms
rtt min/avg/max/mdev = 0.039/0.073/0.110/0.021 ms
root@266c61091e6f:/#
九、DockerCompose
语法:docker compose [options] [command]
-f 指定compose文件的路径和名称
-p指定project名称
指令 | 说明 |
---|---|
up | 创建并启动所有service容器 |
down | 停止并移除所有容器、网络 |
ps | 列出所有启动的容器 |
logs | 查看指定容器的日志 |
stop | 停止容器 |
start | 启动容器 |
restart | 重启容器 |
top | 查看运行中的进程 |
exec | 在指定的运行中容器中执行命令 |