本篇文章继续给大家介绍Docker,主要介绍容器管理的内容,包括容器管理命令,数据持久化,端口映射底层原理,手动制作容器镜像及一个容器运行多个服务的实战。
目录
二、实战1:手动制作镜像实现本地yum仓库,要求可以在本地yum仓库部署docker
三、实战2:一个容器运行多个服务案例,给yum仓库镜像添加sshd的功能
docker部署一般使用包管理工具(yum、apt)还有在任何Linux系统都通用的二进制部署
docker image pull | docker pull
docker image save | docker save
docker image load | docker load
docker image tag | docker tag
docker image rm | docker rmi
docker image ls | docker images
docker image build | docker build
docker image push | docker push
docker image history | docker history
docker container ls/ps/list | docker ps
docker container run | docker run
-i 分配一个交互式的标准输入
-t 分配终端
-d 后台运行
-e 向容器传递环境变量
--name 指定容器的名称
-p 指定端口映射
docker container exec | docker exec
docker container rm | docker rm
docker container inspect
有些镜像在运行的时候需要传递特定的参数
参考链接:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docker.html
#部署单点ES,指定"discovery.type=single-node"表示ES为单点
docker run -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.5
#部署mysql,MYSQL_ROOT_PASSWORD表示指定root用户,密码为123
docker run -e MYSQL_ROOT_PASSWORD=123 -d --name=db01 mysql:8.0
#部署mysql,MYSQL_ALLOW_EMPTY_PASSWORD=yes表示root用户,密码为空
docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d --name=db02 mysql:8.0
#部署mysql,MYSQL_RANDOM_ROOT_PASSWORD=yes表示配置mysql的root用户,密码随机
docker run -e MYSQL_RANDOM_ROOT_PASSWORD=yes -d --name=db03 mysql:8.0
查看nginx对外提供服务的IP,后面跟容器名称
[root@Docker01 ~]# docker run -d nginx:1.14.2
[root@Docker01 ~]# docker container inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' hungry_jemison
172.17.0.2
遍历的方式查找IP
[root@Docker01 ~]# docker container inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' hungry_jemison
172.17.0.2
遍历方式查看Mac地址
[root@Docker01 ~]# docker container inspect -f '{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' hungry_jemison
02:42:ac:11:00:02
查看最近的一个容器对外提供的IP
[root@Docker01 ~]# docker container inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" `docker container ps -lq`
172.17.0.2
--since查看两分钟以内的数据,--until查看两分钟以后的容器的日志,后面可以跟名字也可以跟容器ID
[root@Docker01 ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
......
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 80/tcp clever_sammet
[root@Docker01 ~]# docker container logs -f -t --since 2m a82a20da88e7
2023-06-07T13:10:22.183407269Z 172.17.0.1 - - [07/Jun/2023:13:10:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@Docker01 ~]# docker container logs -f -t --until 2m clever_sammet
2023-06-07T13:10:22.183407269Z 172.17.0.1 - - [07/Jun/2023:13:10:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
容器停止后将不再对外提供服务,开启后又重新提供,比如我们运行nginx,修改了配置文件,在nginx的容器中不方便重启服务,我们就可以重启nginx容器也可以实现启用配置文件的效果,当然也可以nginx -s reload
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 80/tcp clever_sammet
[root@Docker01 ~]# docker stop clever_sammet #停止,默认10秒停,可以用stop -t 1,1秒停止
clever_sammet
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Docker01 ~]# docker start clever_sammet #开启
clever_sammet
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 8 minutes ago Up 1 second 80/tcp clever_samme
[root@Docker01 ~]# docker restart clever_sammet #重启
clever_sammet
容器暂停,相当于虚拟机的挂起
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 9 minutes ago Up About a minute 80/tcp clever_sammet
[root@Docker01 ~]# docker pause clever_sammet #容器暂停
clever_sammet
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 9 minutes ago Up About a minute (Paused) 80/tcp clever_sammet
[root@Docker01 ~]# docker unpause clever_sammet #容器恢复
clever_sammet
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 10 minutes ago Up 2 minutes 80/tcp clever_sammet
将宿主机的koten目录拷贝到clever_sammet容器的根路径下
[root@Docker01 ~]# mkdir koten
[root@Docker01 ~]# docker cp koten clever_sammet:/
Successfully copied 1.54kB to clever_sammet:/
[root@Docker01 ~]# docker exec -it clever_sammet bash
root@a82a20da88e7:/# cd /koten/
root@a82a20da88e7:/koten# exit
exit
将宿主机的/etc/hosts文件拷贝到clever_sammet容器的根路径下
[root@Docker01 ~]# docker cp /etc/hosts clever_sammet:/
Successfully copied 2.05kB to clever_sammet:/
[root@Docker01 ~]# docker exec -it clever_sammet cat /hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
将clever_sammet容器的/6.txt拷贝到宿主机
[root@Docker01 ~]# docker exec -it clever_sammet bash
root@a82a20da88e7:/# echo 666 > 6.txt
root@a82a20da88e7:/# exit
exit
[root@Docker01 ~]# docker cp clever_sammet:/6.txt ./
Successfully copied 2.05kB to /root/./
[root@Docker01 ~]# cat 6.txt
666
docker stats clever_sammet是持续输出状态,类似于实时监控,--no-stream只输出一个状态信息
[root@Docker01 ~]# docker stats clever_sammet --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a82a20da88e7 clever_sammet 0.00% 1.355MiB / 3.84GiB 0.03% 656B / 0B 0B / 0B 2
查看容器有哪些进程
[root@Docker01 ~]# docker top clever_sammet
UID PID PPID C STIME TTY TIME CMD
root 100531 100511 0 21:17 ? 00:00:00 nginx: master process nginx -g daemon off;
101 100552 100531 0 21:17 ? 00:00:00 nginx: worker process
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 23 minutes ago Up 15 minutes 80/tcp clever_sammet
[root@Docker01 ~]# docker kill clever_sammet
clever_sammet
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 24 minutes ago Exited (137) 12 seconds ago clever_sammet
[root@Docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 25 minutes ago Exited (137) 56 seconds ago clever_sammet
[root@Docker01 ~]# docker rename clever_sammet nginx
[root@Docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a82a20da88e7 nginx:1.14.2 "nginx -g 'daemon of…" 25 minutes ago Exited (137) About a minute ago nginx
1、查看现有存储卷
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
2、创建随机(匿名)的存储卷
[root@Docker01 ~]# docker volume create
cecbc406a5cea75be4618daacfca486cf42718103f455eabbe631801a00fc3b4
3、创建自定义名称的存储卷
[root@Docker01 ~]# docker volume create linux
linux
4、清除未使用的存储卷
应该是清除所有未使用的存储卷,但是我测试docker24.0.2版本只能删除随机的未使用的存储卷,自定义名字的存储卷无法被删除,20.10.24版本会都删除
[root@Docker01 ~]# docker volume create
4b98c692381d1660ee85ad132edf6f8bdb1595c3a4e6d4d0139ed42d91035270
[root@Docker01 ~]# docker volume create koten
koten
[root@Docker01 ~]# docker volume prune -f
Deleted Volumes:
4b98c692381d1660ee85ad132edf6f8bdb1595c3a4e6d4d0139ed42d91035270
Total reclaimed space: 0B
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local koten
5、删除指定的存储卷
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local koten
[root@Docker01 ~]# docker volume rm koten
koten
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
6、查看存储卷的详细信息
[root@Docker01 ~]# docker volume create koten
koten
[root@Docker01 ~]# docker volume inspect koten
[
{
"CreatedAt": "2023-06-07T21:44:11+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/koten/_data", #存储卷在宿主主机的目录
"Name": "koten",
"Options": null,
"Scope": "local"
}
]
7、容器使用存储卷进行数据持久化
将存储卷koten挂载到容器,若不存在则直接创建,默认权限为rw,此时修改容器的nginx主页,当容器停止再重启的时候,数据也不会消失,主机要被绑定在挂载点上的目录是/var/lib/docker/volumes/koten/_data,数据都存储在了存储卷里,同时也存储在了这个目录下,两边任意一边修改,另一边都会发生变化
[root@Docker01 ~]# docker container run -v koten:/usr/share/nginx/html -d --name web01 nginx:1.14.2
2ceb94d0b90764a640e1cac942d811b8a9e94237da114261b67b098b6e736109
#该命令等同于docker container run -v koten:/usr/share/nginx/html:rw -d --name web01 nginx:1.14.2
#默认就是rw权限
将koten存储卷以只读的方式挂载,此时进入web02的容器中,无法修改挂载点的文件
[root@Docker01 ~]# docker container run -v koten:/usr/share/nginx/html:ro -d --name web02 nginx:1.14.2
1df2dd3e26854ccc2cb8b95fbdcb2a144d791cec303836a1594bcfbe03cb7ad2
不指定存储卷,将容器的/usr/share/nginx/html路径作为挂载点,和本地随机的存储卷关联
[root@Docker01 ~]# docker container run -v /usr/share/nginx/html -d --name web03 nginx:1.14.2
a707dc435e79a6a4cbc6c9e7e9fb0dc8c374a9f362272b1ff33f9bf2e73c6dbe
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a707dc435e79 nginx:1.14.2 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 80/tcp web03
1df2dd3e2685 nginx:1.14.2 "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 80/tcp web02
2ceb94d0b907 nginx:1.14.2 "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 80/tcp web01
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local f92dfafb134cdb5652c56b2723fbfddf1eefaabed0cd7b6b8a3815873386cb14
local koten
8、创建宿主机路径存储卷
删除所有的存储卷,可以将未使用的所有存储卷删除
docker volume rm `docker volume ls -q`
删除所有的容器及匿名存储卷,-v有删除匿名存储卷的作用,还有使用过的存储卷,我们可以指定名字删除?
[root@Docker01 ~]# docker container rm -fv `docker container ps -qa`
a707dc435e79
1df2dd3e2685
2ceb94d0b907
a82a20da88e7
7f3b67fade82
343ab6c3c4bc
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
local koten
[root@Docker01 ~]# docker volume rm koten
koten
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
此时如果不想被删除可以使用宿主机的路径作为存储卷
将宿主机的路径以只读方式挂载到容器的挂载点,如果宿主机的路径不存在会自动创建
[root@Docker01 ~]# docker run -d -v /koten/data:/usr/share/nginx/html:ro --name web01 nginx:1.20.1
3f4a568696f3f96a33c18ed9cc87633eca2b89100b77c6235994a978b1d867c7
此时查存储卷查不出来,但是可以查看存储卷的详细信息,可以通过挂载的目录找到删除,所以遇见懂行的还是不行
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
[root@Docker01 ~]# docker inspect web01
......
"Mounts": [
{
"Type": "bind",
"Source": "/koten/data",
"Destination": "/usr/share/nginx/html",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
}
],
......
[root@Docker01 ~]# rm -rf /koten/data/
9、与其他容器使用相同的存储卷
由于web01存储卷使用的是路径,所以web02也通过docker volume ls查不到,也是用的/koten/data的路径存储卷
[root@Docker01 ~]# docker container run --volumes-from web01 -d --name web02 nginx:1.20.1
ea3c1bfe997f12487912e526a43c6356a44d714820a0ed054384aea2a723d6bd
[root@Docker01 ~]# docker volume ls
DRIVER VOLUME NAME
[root@Docker01 ~]# docker inspect web02
......
"Mounts": [
{
"Type": "bind",
"Source": "/koten/data",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": false,
"Propagation": "rprivate"
}
],
......
1、共享文件:多个容器需要访问相同的文件。例如,多个 Web 应用容器需要访问相同的静态文件夹。
2、数据库集群:多个数据库容器需要在同一目录下共享数据卷。例如,MySQL 数据库容器需要写入和读取共享的数据卷。
3、日志收集:多个容器需要将日志输出到同一目录。例如,多个应用程序容器需要将日志写入共享的目录中,以便可以统一处理和分析日志数据。
需要注意的是,在使用多个容器共享同一个挂载点时,必须确保所有容器都是只读的或只写的,并且它们不应该同时写入同一个文件或目录,否则可能会导致文件损坏或数据冲突。另外,为了避免权限问题,请确保所有容器都使用相同的用户和组(UID / GID)
端口映射其实是做了两件事,一件是编写iptables规则,另一件是监听了映射的端口
1、将宿主机的所有IP地址的81端口映射到容器的80端口,默认使用tcp协议
[root@Docker01 ~]# docker run -d -p 81:80 --name web01 nginx:1.20.1
edc0949c0d75dc0d9c9d4ab2fc920b04e400c71655c9565420f95088b003faaf
2、指定IP地址进行端口映射
[root@Docker01 ~]# docker run -d -p 10.0.0.201:82:80 --name web02 nginx:1.20.1
326a04c6c7e5048c529d7bc9239df3429649f77835077e3233d886829b71c1e4
3、指定端口范围进行映射
[root@Docker01 ~]# docker run -d -p 10.0.0.201:20-21:20-21 --name web03 nginx:1.20.1
3c2e7cec434b8af8d838feee9d525c1a1119ed8ec58d904c87f68e2497f028fe
4、指定多个不连续的端口映射
比如在部署ES容器的时候就会用到
[root@Docker01 ~]# docker run -d -p 10.0.0.201:9200:9200 -p 10.0.0.201:9300:9300 --name web04 nginx:1.20.1
f18bc1b8ff90f491d2ae6cbb6e13fb8b242a6a9e96cdd3e90b8e10ed86421553
5、指定协议,若不指定协议,默认是tcp协议
[root@Docker01 ~]# docker run -d -p 10.0.0.201:53:53/udp -p 10.0.0.201:53:53/tcp --name web05 nginx:1.20.153c62e049428b7e22634424bc45514c3dde16d2b82e8e33d22701686e7fa17dc
6、宿主机随机端口对容器80端口进行映射
[root@Docker01 ~]# docker run -d -p 10.0.0.201::80/tcp --name web06 nginx:1.20.1
07f354785edf8fc632e9c6aceb146d01db1a9a76405b39bacc854dd937566bf4
7、宿主机用随机端口对容器暴露的端口进行映射
[root@Docker01 ~]# docker run -d -P --name web07 nginx:1.20.1
f2373e6f934c68a1394c2baf82e06d5ca10623deaf5ad4ad1d7ab8c0097321c8
[root@Docker01 ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2373e6f934c nginx:1.20.1 "/docker-entrypoint.…" 16 seconds ago Up 15 seconds 0.0.0.0:32769->80/tcp, :::32768->80/tcp
1、启动容器
启动容器并保持容器为一致开启状态,俗话说就是让容器一直夯着
tail -f只有夯住的作用,nginx -g既有夯住的作用,又有启动nginx服务的作用,若是把nginx -g写进脚本,扩展性就更强了
[root@Docker01 ~]# docker run --name base -d centos:7 tail -f /etc/hosts
2、安装服务
[root@Docker01 ~]# docker exec -it base bash
[root@8d7bdcdf1096 /]# curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@8d7bdcdf1096 /]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@8d7bdcdf1096 /]# yum -y install nginx
......
[root@8d7bdcdf1096 /]# ls /usr/share/nginx/html/
404.html icons/ nginx-logo.png
50x.html img poweredby.png
en-US index.html
[root@8d7bdcdf1096 /]# rm -rf /usr/share/nginx/html/*[root@8d7bdcdf1096 /]# echo www.koten.vip > /usr/share/nginx/html/index.html
[root@8d7bdcdf1096 /]# nginx -g 'daemon off;'
[root@8d7bdcdf1096 /]# exit
exit
[root@Docker01 ~]#
3、将容器提交为镜像
[root@Docker01 ~]# docker container commit -a koten -m 'koten-nginx' base koten-centos-nginx:v0.1
sha256:d98fa4dc0aa3a2a41cafdbf47d8585ce593542196eea4ccf76bd10dce2a68ecc
4、查看镜像的详细信息
[root@Docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
koten-centos-nginx v0.1 d98fa4dc0aa3 25 seconds ago 486MB
[root@Docker01 ~]# docker inspect koten-centos-nginx:v0.1
......
"RepoTags": [
"koten-centos-nginx:v0.1"
],
......
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"tail",
"-f",
"/etc/hosts"
],
"Image": "centos:7",
......
[root@Docker01 ~]#
5、手动运行镜像测试
#运行镜像
[root@Docker01 ~]# docker run -d --name myweb01 koten-centos-nginx:v0.1
e05e4e8e490e7d3af8a92eb7d71fd4fe8b096a48904b611fcc6a93f6e0fcfe91
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e05e4e8e490e koten-centos-nginx:v0.1 "tail -f /etc/hosts" 15 seconds ago Up 15 seconds myweb01
#运行镜像并在容器中执行将nginx在前台服务的命令
[root@Docker01 ~]# docker run -d --name myweb02 koten-centos-nginx:v0.1 nginx -g 'daemon off;'
71c2add6ae7137029466a7d1d52c11873f54c2fe8f64f5cc5d7b7404465efa91
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71c2add6ae71 koten-centos-nginx:v0.1 "nginx -g 'daemon of…" 18 seconds ago Up 17 seconds myweb02
e05e4e8e490e koten-centos-nginx:v0.1 "tail -f /etc/hosts" About a minute ago Up 59 seconds myweb01
#可以将容器再次打包成镜像
[root@Docker01 ~]# docker container commit myweb02 koten-centos-nginx:v0.2
sha256:1867b7ccba272395ed92d35a3f0f913b89562a8efc17bd0395dae28fd54e4ec5
[root@Docker01 ~]# docker run -d --name myweb03 koten-centos-nginx:v0.2
a9a5813fe3ea5fcccb55d9b3d340be9dce2d15da3e5cbc505c14e5054be05560
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9a5813fe3ea koten-centos-nginx:v0.2 "nginx -g 'daemon of…" 25 seconds ago Up 24 seconds myweb03
71c2add6ae71 koten-centos-nginx:v0.1 "nginx -g 'daemon of…" About a minute ago Up About a minute myweb02
e05e4e8e490e koten-centos-nginx:v0.1 "tail -f /etc/hosts" 2 minutes ago Up 2 minutes myweb01
#测试nginx容器的主页内容
[root@Docker01 ~]# docker container inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" `docker container ps -lq`
172.17.0.4
[root@Docker01 ~]# curl 172.17.0.4
www.koten.vip
1、启用容器服务
[root@Docker01 ~]# docker run --name docker-base -d centos:7 tail -f /etc/hosts
26d354802f586db09074c6b173a50387d7a45f1fcc5e9e659e610b7217485cc6
[root@Docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26d354802f58 centos:7 "tail -f /etc/hosts" 6 minutes ago Up 6 minutes docker-base
2、复制rpm包和yum仓库部署脚本到容器中
[root@Docker01 ~]# docker cp deploy_docker.sh docker-base:/
Successfully copied 2.56kB to docker-base:/
[root@Docker01 ~]# docker cp /Packages docker-base:/
Successfully copied 5.41GB to docker-base:/
3、运行脚本实现自动部署yum仓库
[root@Docker01 ~]# docker exec -it docker-base bash
[root@26d354802f58 /]# sh /deploy_docker.sh
......
Complete!
Spawning worker 0 with 130 pkgs
Spawning worker 1 with 129 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
Failed to get D-Bus connection: Operation not permitted
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service, pointing to /usr/lib/systemd/system/nginx.service.
4、编写个nginx -g脚本,运行容器的时候执行脚本即启动了服务
想不被夯住启动nginx,直接写nginx即可,可以在systemctl cat nginx查看nginx启动命令
[root@26d354802f58 /]# cat start.sh
#!/bin/bash
nginx -g 'daemon off;'
5、提交镜像并启动yum容器
[root@Docker01 ~]# docker container commit docker-base docker-local-yum:v0.1
[root@Docker01 ~]# docker run -dp 80:80 docker-local-yum:v0.1 bash -x start.sh
dbbdd20dba951f0278edf84b832fadcd54086d756d5602ae15f7c96146569757
6、客户端配置yum源
[root@Docker04 ~]# cat /etc/yum.repos.d/local-docker.repo
[local]
name=docker_local
baseurl=http://10.0.0.201/packages
enabled=1
7、客户端安装测试
[root@Docker04 ~]# yum -y install docker-ce-20.10.24 docker-ce-cli-20.10.24 containerd.io
......
========================================================================================================
Package Arch Version Repository Size
========================================================================================================
Installing:
containerd.io x86_64 1.6.21-3.1.el7 local 34 M
docker-ce x86_64 3:20.10.24-3.el7 local 22 M
docker-ce-cli x86_64 1:20.10.24-3.el7 local 30 M
Installing for dependencies:
docker-ce-rootless-extras x86_64 24.0.2-1.el7 local 9.1 M
Transaction Summary
========================================================================================================
......
1、基于上面的yum镜像为基础镜像
[root@Docker01 ~]# docker run -d docker-local-yum:v0.1 #运行容器
9c758bf1a0564c70984c58eb677a301a4d16a935903a73530134d848e773fb7f
[root@Docker01 ~]# docker ps #查看当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c758bf1a056 docker-local-yum:v0.1 "tail -f /etc/hosts" 9 seconds ago Up 8 seconds elegant_gauss
[root@Docker01 ~]# docker exec -it elegant_gauss bash #进入容器
[root@9c758bf1a056 /]# yum provides sshd #查找sshd服务在哪个安装包下
......
openssh-server-7.4p1-22.el7_9.x86_64 : An open source
...: SSH server daemon
Repo : updates
Matched from:
Filename : /usr/sbin/sshd
[root@9c758bf1a056 /]# yum -y install openssh-server initscripts
......
[root@9c758bf1a056 /]# cat /start.sh
#!/bin/bash
# Inint root password.
if [ -n "$ADMIN_PASSWD" ];then
echo $ADMIN_PASSWD | passwd root --stdin
elif [ -n "$1" ]; then
echo $1 | passwd root --stdin
else
echo 123 | passwd root --stdin
fi
# start nginx server
# nginx -g "daemon off;"
nginx
# get key
/usr/sbin/sshd-keygen
# start sshd service
/usr/sbin/sshd -D
[root@9c758bf1a056 /]# exit
exit
[root@Docker01 ~]#
#initscripts是CentOS 或者 RHEL 系统的初始化脚本包,用于启动或停止系统服务、设置主机名等
不安装这个会影响ssh的使用
2、提交镜像并启动yum容器
[root@Docker01 ~]# docker commit elegant_gauss docker-local-yum:v0.2
sha256:abb8580cd9959fbb92b71bc51f68f00445ec59edaefd7165b9d2e09c80565ae5
[root@Docker01 ~]# docker run -dp 1180:80 -p 1122:22 docker-local-yum:v0.2 bash -x /start.sh 666
6df7b592ce07bedf0090a19255483e300700a29c9ddb1b6e24e22a05651f721c
[root@Docker01 ~]# docker container inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" `docker container ps -lq`
172.17.0.2
[root@Docker01 ~]# ssh 172.17.0.2
......
[root@6df7b592ce07 ~]# exit
logout
Connection to 172.17.0.2 closed.
[root@Docker01 ~]#
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!