Docker从入门到入土
作者:互联网
Docker从入门到入土
Docker官方文档 <!--more-->
Docker安装
# 1.卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2.需要的安装包
yum install -y yum-utils
# 3.配置阿里云镜像仓库地址
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 4.更新yum的索引
# 5.安装最新的docker ce->社区版 ee->企业版
yum install docker-ce docker-ce-cli containerd.io
# 6.判断docker版本
docker version
# 7.开启docker服务
systemctl start docker
# 8.测试Hello World
docker run hello-world
# 9.查看hello-world镜像
docker images
图1 测试运行HelloWork
阿里云加速
登录阿里云,搜索容器镜像服务
,找到镜像加速器,配置
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://3fshcuok.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
回顾HelloWorld
run的流程和docker原理
图2 run的流程
底层原理
Docker是如何工作的?
Docker是一个CS结构系统,Docker的守护进程运行在主机上,通过Socket从客户端访问
Docker-Server接收到Docker-Client的指令,就会执行这个命令
图3 docker原理
Docker为什么比VM Ware快?
Docker的常用命令
帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker --help # 帮助命令
镜像命令
docker images 查看所有本地的主机上的镜像
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 months ago 13.3kB
[root@localhost /]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 months ago 13.3kB
[root@localhost /]# docker images -q 只显示镜像ID
d1165f221234
docker search 搜索镜像
[root@localhost /]# docker search mysql # 到阿里云镜像中寻找mysql镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11435 [OK]
mariadb MariaDB Server is a high performing open sou… 4341 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 848 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 325 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
[root@localhost /]# docker search mysql --filter=STARS=5000 START数大于5000的
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11435 [OK]
docker pull 下载镜像
# 下载镜像 docker pull 镜像名称:版本号,如果不填写版本号默认下载最新
[root@localhost /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
a330b6cecb98: Pull complete # 分层下载,docker image的核心 联合文件系统
9c8f656c32b8: Pull complete
88e473c3f553: Pull complete
50e0789bacad: Pull complete
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 下面两者等价,只是将真实地址省略
docker pull mysql
docker pull docker.io/library/mysql:latest
[root@localhost /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a330b6cecb98: Already exists # 得益于分层文件系统,有些不需要下载
9c8f656c32b8: Already exists
88e473c3f553: Already exists
062463ea5d2f: Already exists
7063da9569eb: Pull complete
51a9a9b4ef36: Pull complete
Digest: sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 1d7aba917169 2 weeks ago 448MB
mysql latest 0716d6ebcc1a 2 weeks ago 514MB
hello-world latest d1165f221234 6 months ago 13.3kB
docker rmi 删除镜像
docker rmi -f 容器ID
docker rmi -f 容器ID 容器ID 容器ID
docker rmi -f $(docker images -aq) # 删除全部镜像
容器命令
说明:我们有了镜像才可以创建容器,下载一个centos镜像来测试学习
docker pull centos
新建容器并启动
# docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
-P 随机指定端口
#测试,启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@fdf7265a8a41 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 注意进入容器后'localhost'变成了centos的ID
# 退出容器
[root@fdf7265a8a41 /]# exit
exit
列出所有的运行的容器
# docker ps 命令
# docker ps 列出当前正在运行的容器
# docker ps -a 列出当前正在运行的容器+历史运行过的容器
# docker ps -n=? 列出最近创建的容器
# docker ps -q 只显示容器编号
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdf7265a8a41 centos "/bin/bash" 2 minutes ago Exited (0) 52 seconds ago crazy_jennings
2e547c587069 d1165f221234 "/hello" 6 hours ago Exited (0) 6 hours ago vigorous_ganguly
ca61591d1ba9 d1165f221234 "/hello" 16 hours ago Exited (0) 16 hours ago inspiring_hertz
56517e8bd551 d1165f221234 "/hello" 16 hours ago Exited (0) 16 hours ago great_ellis
[root@localhost /]# docker ps -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fdf7265a8a41 centos "/bin/bash" 9 minutes ago Exited (0) 8 minutes ago crazy_jennings
[root@localhost /]# docker ps -aq
fdf7265a8a41
2e547c587069
ca61591d1ba9
56517e8bd551
退出容器
exit # 容器停止并退出
Ctrl + P + Q # 容器不停止退出
删除容器
docker rm 容器ID # 删除指定容器
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 镜像名
# 问题 docker ps 发现容器停止了
# 常见的坑 docker 容器使用后台运行 就必须要有一个前台进程,docker发现没有应用就会自动停止
# nginx 容器启动后,发现自己没有提供服务,就会立即停止,就没有程序了
查看日志
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo ccy;sleep 1;done"
d2417531fc728a400a97bac53725f1a5e93458e40da95b1240676dab5e41e9c9
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2417531fc72 centos "/bin/sh -c 'while t…" 5 seconds ago Up 5 seconds nostalgic_panin
# docker logs -ft --tail 查看日志条数 容器ID
[root@localhost /]# docker logs -ft --tail 20 d2417531fc72
图4 查看日志输出
查看容器中进程信息
# docker top 容器ID
[root@localhost /]# docker top 4730f22951bb
图5 查看容器进程信息
查看镜像的元数据
# docker inspect 容器ID
[root@localhost /]# docker inspect 4730f22951bb
进入当前正在运行的容器
# docker exec -it 容器ID /bin/bash 进入容器后开启一个新的终端,可以在里面操作
[root@localhost ~]# docker exec -it f97583fcbddf /bin/bash
# docker attach 容器ID 进入容器正在执行的终端,不会启动新的进程
[root@localhost /]# docker attach 4730f22951bb
拷贝容器内的文件到主机
# docker cp 容器ID:需要复制的文件绝对路径 复制到主机绝对路径
[root@localhost ~]# docker exec -it f97583fcbddf /bin/bash
[root@f97583fcbddf /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@f97583fcbddf /]# touch test.txt
[root@f97583fcbddf /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test.txt tmp usr var
[root@f97583fcbddf /]# exit # 退出容器
exit
[root@localhost ~]# cd /home
[root@localhost home]# ls
ccyccy zfs
[root@localhost home]# docker cp f97583fcbddf:/test.txt /home
[root@localhost home]# ls
ccyccy test.txt zfs
图6 拷贝容器内的文件到主机
基础实战
部署Nginx
图7 nginx容器
[root@localhost home]# docker search nginx # docker hub上查找nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15504 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2066 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 818 [OK]
[root@localhost home]# docker pull nginx # 下载nginx
[root@localhost home]# docker images # 查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ad4c705f24d3 10 days ago 133MB
[root@localhost home]# docker run -d --name nginx01 -p 3344:80 nginx # 启动nginx
1c6c5f3639a5291baa28685b3dbb8f311276b5df09b8e1c22d1c8f9505189961
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c6c5f3639a5 nginx "/docker-entrypoint.…" 18 seconds ago Up 13 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@localhost home]# curl localhost:3344 # 查看3344端口
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
图8 本地访问容器
我们每次改动nginx配置文件,都需要进入容器内部,十分的麻烦,要是可以在容器外部提供一个映射路径,达到在外部修改文件名,容器内部就可以自动修改,这就是后面要讲的容器数据卷挂载
部署Tomcat
# 方法一 You can test it by visiting http://container-ip:8080 in a browser or, if you need access outside the host, on port 8888: 这是一种前台启动
docker run -it --rm tomcat:9.0
# 我们之前的启动都是后台,停止了容器之后,容器还可以查到
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 4 days ago 231MB
tomcat 9.0 62146f8bda84 5 days ago 680MB
nginx latest ad4c705f24d3 10 days ago 133MB
# 方法二
docker pull tomcat # 下载tomcat
docker run -d -p 3355:8080 --name tomcat01 tomcat # 后台运行tomcat
docker exec -it tomcat01 /bin/bash
# 发现问题:1.linux命令变少了 2.没有webapps,阿里云镜像的原因,默认是最小的镜像
图9 本地访问阉割版的tomcat
# 解决访问404问题,将webapps.dist的文件复制到webapps中
root@431d992fab0f:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@431d992fab0f:/usr/local/tomcat# cp webapps.dist/* webapps
cp: -r not specified; omitting directory 'webapps.dist/ROOT'
cp: -r not specified; omitting directory 'webapps.dist/docs'
cp: -r not specified; omitting directory 'webapps.dist/examples'
cp: -r not specified; omitting directory 'webapps.dist/host-manager'
cp: -r not specified; omitting directory 'webapps.dist/manager'
root@431d992fab0f:/usr/local/tomcat# cp -r webapps.dist/* webapps
图10 本地访问补充版的tomcat
思考问题:我们以后要部署项目,如果每次都要进入容器是不是很麻烦?要是可以在容器外部提供一个映射路径,我们在外部防止项目,就自动同步到内部
部署ES和Kibana
# es暴露的端口很多
# es十分消耗内存
# es的数据一般都需要放置到安全目录,挂载
# --net somenetwork
# 不限制内存启动ES
[root@localhost ~]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
Unable to find image 'elasticsearch:7.6.2' locally
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
045da33338e661ac16eb225898ccc63292dc3420f48ae128730192aeb512947f
# 启动之后整体很卡
# docker stats 容器ID 查看容器占用内存
[root@localhost ~]# docker stats 045da33338e6
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
045da33338e6 elasticsearch 0.43% 884.6MiB / 1.781GiB 48.51% 648B / 0B 1.32GB / 156MB 23
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
045da33338e6 elasticsearch 0.43% 884.6MiB / 1.781GiB 48.51% 648B / 0B 1.32GB / 156MB 23
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
045da33338e6 elasticsearch 178.64% 877.5MiB / 1.781GiB 48.12% 648B / 0B 1.36GB / 156MB 23
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
045da33338e6 elasticsearch 0.72% 898.5MiB / 1.781GiB 49.27% 648B / 0B 1.81GB / 158MB 45
^C
# 测试是否能够连接
[root@localhost ~]# curl localhost:9200
{
"name" : "045da33338e6",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "jUZT3mabSQOnW5GRafTcEw",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
# 限制内存的启动ES
[root@localhost ~]# docker run -d --name elasticsearch04 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
7a54988b44026facd9438e1cdafbfb8eae1cbee889799e3e50de49cea2d7c002
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7a54988b4402 elasticsearch:7.6.2 "/usr/local/bin/dock…" 4 seconds ago Up 2 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch04
# 可以看到ES占用内存明显下降
[root@localhost ~]# docker stats 7a54988b4402
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
7a54988b4402 elasticsearch04 0.15% 218MiB / 1.781GiB 11.95% 648B / 0B 81.4MB / 0B 21
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
7a54988b4402 elasticsearch04 0.15% 218MiB / 1.781GiB 11.95% 648B / 0B 81.4MB / 0B 21
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
7a54988b4402 elasticsearch04 322.91% 239.7MiB / 1.781GiB 13.14% 648B / 0B 83.2MB / 0B 21
^C
[root@localhost ~]# curl localhost:9200
{
"name" : "7a54988b4402",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "5oMHX-VITRO8qQnj_wmPHA",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
可视化
portainer
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
图11 访问portainer
图12 查看docker容器可视化界面
commit镜像
实战测试
启动一个默认的tomcat
将webapps.list下面的文件复制到webapps
将修改过的镜像提交为一个镜像
# 命令和git原理相似
# docker commit -a="作者" -m="描述内容" 容器ID 镜像名:版本号
[root@localhost ~]# docker commit -a="ccy" -m="add webapps application" dc8b8c0c381f ccytomcat:1.0
sha256:410a652a4828d0b900b27cfcb62636bac328159cbba2b1f690c793ebe552153f
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ccytomcat 1.0 410a652a4828 13 seconds ago 684MB
图13 commit镜像
容器数据卷
什么是容器数据卷?
将应用和环境打包成一个镜像!
数据?如果数据都在容器中,那么我们容器删除,数据就会丢失!需求:数据可以持久化
MySQL,容器删乐,删库跑路!需求:MySQL数据可以存储到本地
荣光其之间可以有一个数据共享的技术,Docker容器中产生的数据,同步到本地
这就是卷技术!目录的挂在,将我们容器内的目录,挂载到Linux上面个
图14 容器数据卷
使用卷技术
方式一:直接使用命令来挂载
第一台服务器命令挂载启动centos
[root@localhost ~]# docker run -it -v /home/ceshi:/home centos /bin/bash # 挂载命令
[root@4a82deeaf247 /]# cd /home
[root@4a82deeaf247 home]# ls
[root@4a82deeaf247 home]# touch test.java # 创建test.java文件
[root@4a82deeaf247 home]# ls
test.java
第二胎服务器查看挂载点同步出现了文件
[root@localhost ceshi]# ls
test.java
图15 docker inspect 容器ID
测试关闭容器后,在宿主机上改变文件内容,数据是否同步到容器中
[root@4a82deeaf247 home]# exit # 关闭容器
exit
[root@localhost ~]# docker ps # 确认容器关闭
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
在宿主机上改变文件内容
[root@localhost ceshi]# vim test.java
图16 改变文件内容
重新启动容器查看数据是否同步
[root@localhost ~]# docker start 4a82deeaf247 # 重新启动容器
4a82deeaf247
[root@localhost ~]# docker attach 4a82deeaf247 # 进入容器
[root@4a82deeaf247 /]# cd home
[root@4a82deeaf247 home]# ls
test.java
[root@4a82deeaf247 home]# cat test.java # 输出文件内容
Hello Linux
实战测试
# 运行容器,需要做数据挂在,安装启动mysql,需要配置密码,这是要注意的
# 参数说明:
# -d 后台运行
# -p 端口映射
# -v 卷挂载
# -e 环境配置
# --name 容器名字
[root@localhost ~]# 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
3deb393ddb4c4f80c2ccd5cefe0b66e530324b04a33f9436c4657dd281ca16ae
[root@localhost ~]# cd /home
[root@localhost home]# ls
ccyccy ceshi mysql test.txt zfs
本地是可以连接到容器中的mysql的
图17 连接mysql
测试新建数据库以后数据是否同步显示
[root@localhost mysql]# ls data
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem test
结果表示数据实时同步,并且即使删除了容器数据仍然不会丢失,做到了数据持久化
图18 数据持久化实现
具名挂载和匿名挂载
[root@localhost mysql]# docker run -d -P --name nginx01 -v /etc/nginx nginx # 匿名挂载
8b69296ea35d8c87fd2235c9657f3b3b16ebe6eb49347a85f98761fd3c870436
# -v 中只写容器内的路径,没有写容器外的路径
[root@localhost mysql]# docker volume ls # 查看所有卷的情况
DRIVER VOLUME NAME
local 9fb68fde313f775a1dac0fce0f17622a99248435dcdbe48dbeca459fc18ab1bd
local 607c38318150dda7c742a836f689a2387bde63e49a0f52fad11f8d7363d4f077
local 2419963dcf664596011c210d8ce3abc4a64bfb88ab9a8f04bdc9ba35824c30f0
[root@localhost mysql]# docker run -d -P --name nginx02 -v juming:/etc/nginx nginx # 具名挂载
5f2b8085ff48798b7969334b3622c6a0dff923d3a7ff167e33cc5be9a0e91334
[root@localhost mysql]# docker volume ls
DRIVER VOLUME NAME
local 9fb68fde313f775a1dac0fce0f17622a99248435dcdbe48dbeca459fc18ab1bd
local 607c38318150dda7c742a836f689a2387bde63e49a0f52fad11f8d7363d4f077
local 2419963dcf664596011c210d8ce3abc4a64bfb88ab9a8f04bdc9ba35824c30f0
local juming
# 查看卷的具体情况
[root@localhost mysql]# docker volume inspect juming
[
{
"CreatedAt": "2021-09-22T14:53:43+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming/_data",
"Name": "juming",
"Options": null,
"Scope": "local"
}
]
# 通过具名挂载可以方便的找到我们的卷,大多数情况下都是在使用具名挂载
拓展
# ro readonly 只读
# rw readwrite 可读可写
[root@localhost mysql]# docker run -d -P --name nginx01 -v /etc/nginx:ro nginx
[root@localhost mysql]# docker run -d -P --name nginx01 -v /etc/nginx:rw nginx
初始Dockerfile
Dockerfile就是用来构建docker镜像的构建文件
方式二:Dockerfile
第一台服务器
[root@localhost home]# mkdir docker-test-volume # 创建测试文件夹
[root@localhost home]# cd docker-test-volume
[root@localhost docker-test-volume]# vim dockerfile1 # 编写脚本,用以生成镜像,挂载
[root@localhost docker-test-volume]# cat dockerfile1
FORM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
# docker build -f dockerfile的绝对路径 -t 名称:版本 .(这个点很重要不要忘记)
[root@localhost docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t kuangshen/centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 40417b2b9c8c
Removing intermediate container 40417b2b9c8c
---> 946914c53113
Step 3/4 : CMD echo "----end----"
---> Running in 1b4f54c4fa06
Removing intermediate container 1b4f54c4fa06
---> 782c87429aa3
Step 4/4 : CMD /bin/bash
---> Running in f11246ac40a3
Removing intermediate container f11246ac40a3
---> c7a9ef231036
Successfully built c7a9ef231036
Successfully tagged kuangshen/centos:1.0
[root@localhost docker-test-volume]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kuangshen/centos 1.0 c7a9ef231036 28 seconds ago 231MB # 可以看到镜像已经生成了
ccytomcat 1.0 410a652a4828 2 hours ago 684MB
centos latest 5d0da3dc9764 6 days ago 231MB
tomcat 9.0 62146f8bda84 7 days ago 680MB
[root@localhost ~]# docker run -it c7a9ef231036 /bin/bash # 运行
[root@2d021f05aba6 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@2d021f05aba6 /]# cd volume01
[root@2d021f05aba6 volume01]# ls
[root@2d021f05aba6 volume01]# touch container.txt # 创建文件测试是否挂载成功
[root@2d021f05aba6 volume01]# ls
container.txt
第二台服务器
图19 挂载情况
[root@localhost ~]# cd /var/lib/docker/volumes/8888b62687819088cf399880b7e559ec5bb20cc880eb8f4f33734d0b78646f0e/_data
[root@localhost _data]# ls
container.txt
数据卷容器
多个容器之间的数据同步
测试
第一个容器
[root@localhost _data]# docker run -it --name docker01 kuangshen/centos:1.0 # 启动第一个容器
第二个容器
# --volumes-from 容器名称
[root@localhost _data]# docker run -it --name docker02 --volumes-from docker01 kuangshen/centos:1.0 # 启动第二个容器
第一个容器
[root@50427c01508a /]# cd volume01
[root@50427c01508a volume01]# ls
[root@50427c01508a volume01]# touch test.txt # 创建测试文件
[root@50427c01508a volume01]# ls
test.txt
第二个容器
[root@b6e7933381c9 /]# cd volume01 # 查看数据是否同步
[root@b6e7933381c9 volume01]# ls
test.txt
上面这种情况是,两个容器同时运行以后,第一个容器创建文件,第二个容器数据实时同步
如果此时有第三个容器连接第一个容器数据是否会同步?
第三个容器
[root@localhost ~]# docker run -it --name docker03 --volumes-from docker01 kuangshen/centos:1.0
[root@bcc15b9dd695 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@bcc15b9dd695 /]# ls volume01
test.txt
证明在第三个程序创建并运行时,数据就实时同步了
第四个容器连接非第一个主机数据也会实时同步
第四个容器
[root@localhost ~]# docker run -it --name docker04 --volumes-from docker03 kuangshen/centos:1.0
[root@38cff41d9a5a /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02
[root@38cff41d9a5a /]# cd volume01
[root@38cff41d9a5a volume01]# ls
test.txt
即使第一个容器关闭(即使删除),数据仍然保存
DockerFile
dockerfile是用来构建docker镜像的文件!命令参数脚本
构建步骤:
-
编写一个dockerfile文件
-
docker build构建成一个镜像
-
docker run 运行镜像
-
docker push 发布镜像(DockerHub、阿里云镜像仓库)
指令
FROM # 基础镜像,一切从这里开始构建
MAINTAINER # 镜像是谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:tomcat镜像,这个tomcat压缩包,添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 保留端口配置
CMD # 指定容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承的Dockerfile这个时候会运行ONBUILD的命令
COPY # 类似ADD,将文件拷贝到镜像中
ENY # 构建的时候设置环境变量
实战测试
创建一个增强版的centos
原版的centos vim ifconfig等命令不能使用,自己创建的centos功能需要增强
[root@localhost dockerfile]# vim mydockerfile-centos # 编写dockerfile文件
[root@localhost dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER ccy<1391403481@qq.com>
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@localhost dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
...
Successfully built 2ea4ac329e88
Successfully tagged mycentos:0.1
[root@localhost dockerfile]# docker run -it mycentos:0.1 # 运行增强版centos
[root@bf5566adc835 local]# pwd # dockerfile中指定的工作路径
/usr/local
[root@bf5566adc835 local]# vim test # 测试功能
[root@bf5566adc835 local]# exit
exit
[root@localhost dockerfile]# docker history 2ea4ac329e88 # 查看镜像的创建过程 docker history 镜像ID
IMAGE CREATED CREATED BY SIZE COMMENT
2ea4ac329e88 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
34d50469620b 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
f3b719fb3b3c 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
9ea686da736c 8 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
e3bd5893df79 8 minutes ago /bin/sh -c yum -y install net-tools 14.7MB
e0300580cb35 8 minutes ago /bin/sh -c yum -y install vim 71.5MB
9edc5e5dc9d0 14 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
4aab098875cf 14 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
ba0eec4a69bf 14 minutes ago /bin/sh -c #(nop) MAINTAINER ccy<1391403481… 0B
5d0da3dc9764 6 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 6 days ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
图20 增强版centos
CMD和ENTRYPOINT的区别
CMD # 指定容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT # 指定容器启动的时候要运行的命令,可以追加命令
CMD
[root@localhost dockerfile]# vim dockerfile-cmd-test
[root@localhost dockerfile]# cat dockerfile-cmd-test
FROM centos
CMD ["ls","-a"]
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
[root@localhost dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Successfully built c281d114e792
Successfully tagged cmdtest:latest
[root@localhost dockerfile]# docker run -it c281d114e792
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
[root@localhost dockerfile]# docker run -it c281d114e792 -l # CMD指令不能追加指令,如果想要实现-al则需要运行
# docker run -it c281d114e792 ls -al
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ERRO[0000] error waiting for container: context canceled
ENTRYPOINT
[root@localhost dockerfile]# vim dockerfile-entrypoint-test # 编写dockerfile文件,内容与上面文件唯一不同的就在于CMD指令替换为ENTRYPOINT
[root@localhost dockerfile]# docker build -f dockerfile-entrypoint-test -t entrypointtest:0.1 .
Successfully built c4d9e947ed9e
Successfully tagged entrypointtest:0.1
[root@localhost dockerfile]# docker run -it c4d9e947ed9e
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
[root@localhost dockerfile]# docker run -it c4d9e947ed9e -l # ENTRYPOINT可以追加指令
total 0
drwxr-xr-x. 1 root root 6 Sep 22 12:54 .
drwxr-xr-x. 1 root root 6 Sep 22 12:54 ..
-rwxr-xr-x. 1 root root 0 Sep 22 12:54 .dockerenv
lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Sep 22 12:54 dev
drwxr-xr-x. 1 root root 66 Sep 22 12:54 etc
drwxr-xr-x. 2 root root 6 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 6 Sep 15 14:17 lost+found
drwxr-xr-x. 2 root root 6 Nov 3 2020 media
drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt
drwxr-xr-x. 2 root root 6 Nov 3 2020 opt
dr-xr-xr-x. 211 root root 0 Sep 22 12:54 proc
dr-xr-x---. 2 root root 162 Sep 15 14:17 root
drwxr-xr-x. 11 root root 163 Sep 15 14:17 run
lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Nov 3 2020 srv
dr-xr-xr-x. 13 root root 0 Sep 22 04:01 sys
drwxrwxrwt. 7 root root 171 Sep 15 14:17 tmp
drwxr-xr-x. 12 root root 144 Sep 15 14:17 usr
drwxr-xr-x. 20 root root 262 Sep 15 14:17 var
实战Dockerfile制作tomcat镜像
-
准备镜像文件,tomcat压缩包,jdk压缩包
图21 tomcat和jdk版本
-
编写dockerfile文件,官方命名:Dockerfile,不需要添加-f指令
[root@localhost ~]# cd /home/ceshi
[root@localhost ceshi]# cat Dockerfile # 编写dockerfile文件
FROM centos
MAINTAINER ccy<1391403481@qq.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u11-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.22.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh && tail -F /url/local/apache-tomcat-9.0.22/bin/logs/catalina.out
-
搭建镜像
[root@localhost ceshi]# docker build -t diytomcat . # 创建dyitomcat,dockerfile文件命名为Dockerfile时不要参数 -f dockerfile文件的绝对路径
...
Successfully built 56be47a9b145
Successfully tagged diytomcat:latest
-
运行镜像并测试访问测试端口
[root@localhost tomcat]# docker run -d -p 9090:8080 --name ccytomcat -v /home/shuang/tomcat/test:/usr/local/apache-tomcat-9.0.22/webapps/test -v /home/shuang/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.22/logs diytomcat
d59fa67ce0a6143e04c3cc162b95bf62b6e4c2ef8b3cb27369981cc85671bf53
[root@localhost tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d59fa67ce0a6 diytomcat "/bin/sh -c '/usr/lo…" 26 seconds ago Up 25 seconds 0.0.0.0:9090->8080/tcp, :::9090->8080/tcp ccytomcat
图22 访问tomcat主页
5.发布项目
在挂载的/home/shuang/tomcat/test
目录下创建index.jsp
,WEB-INF
并在WEB-INF
目录下创建web.xml
图23 index.jsp和web.xml内容
图24 访问测试页面
发布镜像
-
登录自己的DockerHub
[root@localhost tomcatlogs]# docker login -u 1913041150
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
-
发布
给镜像标记tag的时候有一个小坑,docker tag DockerHub上你登陆的用户名/自定义名:版本号
,如果命名不规范发布会失败
# 将镜像标记tag不然会发布失败
[root@localhost ceshi]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
diytomcat latest 3898302ff13c 53 seconds ago 643MB
[root@localhost ceshi]# docker tag 3898302ff13c 1913041150/tomcat:1.0 # 给发布的镜像标记tag
[root@localhost ceshi]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
1913041150/tomcat 1.0 3898302ff13c About a minute ago 643MB
diytomcat latest 3898302ff13c About a minute ago 643MB
# docker push 镜像名:版本号
[root@localhost ceshi]# docker push 1913041150/tomcat:1.0
The push refers to repository [docker.io/1913041150/tomcat]
d5adb62c1a6a: Pushed
635aab98b6b4: Pushed
fc22e7e29fc1: Pushed
202730b45a70: Pushed
74ddd0ec08fa: Pushed
1.0: digest: sha256:bbca3244dbcae2b3a2c09670f8b7502f64c7609d50d89ec055eba394bcf1fed7 size: 1373
图25 镜像上传成功
SpringBoot微服务打包Docker镜像
-
创建springboot项目,编写测试类
@RestController
public class DockerController {
@GetMapping("/test")
public String test() {
return "Hello Ccy!!!";
}
}
-
测试运行,package项目打包插件
图26 package打包
图27 测试运行jar包
-
下载Docker插件(作用是:Dockerfile文件高亮显示以及代码提示)
图28 docker插件
-
编写Dockerfile文件
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
-
将jar包和Dockerfile文件上传至Linux虚拟机中,创建,运行,测试
[root@localhost idea]# ls # 将将jar包和Dockerfile文件上传至目标目录中
Docker-0.0.1-SNAPSHOT.jar Dockerfile
[root@localhost idea]# docker build -t ccy . # 创建镜像
...
Successfully built 75c4da69d31a
Successfully tagged ccy:latest
[root@localhost idea]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ccy latest 75c4da69d31a 12 seconds ago 661MB
java 8 d23bdf5b1b1b 4 years ago 643MB
[root@localhost idea]# docker run -d -P --name ccy-web ccy # 后台运行镜像
4e3fbd7622dd39544a26b1f604ee710ea841d9420d013175d4a508e32cfcedca
[root@localhost idea]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e3fbd7622dd ccy "java -jar /app.jar …" About a minute ago Up 58 seconds 0.0.0.0:49153->8080/tcp, :::49153->8080/tcp ccy-web
[root@localhost idea]# curl localhost:49153/test # 测试运行
Hello Ccy!!![root@localhost idea]#
标签:容器,入门,ID,入土,镜像,docker,root,Docker,localhost 来源: https://www.cnblogs.com/ccywmbc/p/16410840.html