有时候需对已有的容器的docker环境进行操作,但因非自己搭建的,或创建容器时未保留容器的创建命令,这时候就很难查到容器生成时的docker run命令。本文探讨在国产openeuler22.03容器环境下用runlike来实现容器生成时的docker run命令重构。
runlike依赖click,因此需要同时下载此两个包,本文发文时对应的版本为click-8.1.3和runlike-1.4.9,下载命令如下:
[root@localhost opt]# pip3 download click==8.1.3 -d /opt
Collecting click==8.1.3
Using cached click-8.1.3-py3-none-any.whl (96 kB)
Saved ./click-8.1.3-py3-none-any.whl
Successfully downloaded click
[root@localhost opt]# pip3 download runlike==1.4.9 -d /opt
Collecting runlike==1.4.9
File was already downloaded /opt/runlike-1.4.9-py3-none-any.whl
Collecting click<9.0.0,>=8.0.1
File was already downloaded /opt/click-8.1.3-py3-none-any.whl
Successfully downloaded runlike click
[root@localhost opt]# ls -al *.whl
-rw-r--r--. 1 root root 96588 6月 13 16:57 click-8.1.3-py3-none-any.whl
-rw-r--r--. 1 root root 5733 4月 11 08:11 runlike-1.4.9-py3-none-any.whl
将软件包拷贝分发到内网服务器,安装软件包
[root@localhost opt]# pip3 install click-8.1.3-py3-none-any.whl runlike-1.4.9-py3-none-any.whl
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Processing ./click-8.1.3-py3-none-any.whl
Processing ./runlike-1.4.9-py3-none-any.whl
Installing collected packages: click, runlike
Successfully installed click-8.1.3 runlike-1.4.9
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@localhost opt]#
命令格式:runlike -p 容器id
上样例(测试主机上运行了3个容器实例):
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a889fab9c8e4 atmoz/sftp:debian-stretch "/entrypoint sftp:11…" 31 hours ago Up 28 minutes 0.0.0.0:4436->22/tcp sftpsrv3
94795b86366a atmoz/sftp:debian-stretch "/entrypoint sftp:11…" 33 hours ago Up 28 minutes 0.0.0.0:4435->22/tcp sftpsrv2
4b5b4a2c06f2 atmoz/sftp:debian-stretch "/entrypoint sftp:11…" 33 hours ago Up 28 minutes 0.0.0.0:4434->22/tcp sftpsrv
[root@localhost opt]# runlike -p 4b5b4a2c06f2
docker run --name=sftpsrv \
--hostname=4b5b4a2c06f2 \
--network=snet \
-p 4434:22 \
--restart=always \
--runtime=runc \
--detach=true \
atmoz/sftp:debian-stretch \
sftp:1111:1111
[root@localhost opt]# runlike -p 94795b86366a
docker run --name=sftpsrv2 \
--hostname=94795b86366a \
--network=snet \
-p 4435:22 \
--restart=always \
--runtime=runc \
--detach=true \
atmoz/sftp:debian-stretch \
sftp:1111:1111
[root@localhost opt]# runlike -p a889fab9c8e4
docker run --name=sftpsrv3 \
--hostname=a889fab9c8e4 \
--mac-address=02:42:ac:11:00:02 \
-p 4436:22 \
--restart=always \
--runtime=runc \
--detach=true \
atmoz/sftp:debian-stretch \
sftp:1111:1111
其中hostname、mac-address、runtime是可以不需要的,--detach=true一般写为-d。
对比这批测试主机的原始docker run指令(注意sftpsrv3使用容器缺省bridge网络,sftpsrv、sftpsrv2使用自定义网络snet并配置了固定IP):
docker run --name sftpsrv --ip=172.18.0.2 --network=snet --restart=always -p 4434:22 -d atmoz/sftp:debian-stretch sftp:1111:1111
docker run --name sftpsrv2 --ip=172.18.0.3 --network=snet --restart=always -p 4435:22 -d atmoz/sftp:debian-stretch sftp:1111:1111
docker run --name sftpsrv3 --restart=always -p 4436:22 -d atmoz/sftp:debian-stretch sftp:1111:1111
可知runlike生成的docker run指令配置反推出了除容器的固定IP地址外的其它所有信息,如果在自定义网络环境中,需要在docker run 中用“--ip=”补充指定出静态IP,以免容器业务有影响。
至于容器的固定IP, 简单的话可以用iptables -nL来根据Chain DOCKER策略端口来反推(本例 3个测试容器目标端口一样,生产系统不大可能出现这种情况,不同容器的目标端口是不同的):
[root@localhost opt]# iptables -nL
...
Chain DOCKER (2 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 172.18.0.2 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 172.17.0.2 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 172.18.0.3 tcp dpt:22
...
当然也可以进入容器中进行查看:
[root@localhost opt]# docker exec -it sftpsrv /bin/bash
root@4b5b4a2c06f2:/# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
5: eth0@if6: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.18.0.2/24 brd 172.18.0.255 scope global eth0
valid_lft forever preferred_lft forever
root@4b5b4a2c06f2:/# exit
exit
[root@localhost opt]# docker exec -it sftpsrv3 /bin/bash
root@a889fab9c8e4:/# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
7: eth0@if8: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@a889fab9c8e4:/# exit
exit
[root@localhost opt]#
希望本文对大家有用。