如何从一个新的docker集装箱移动一个卷?
我正在努力实现一个机制来升级容器的基础映像。 为了做到这一点,我需要:
- 从当前容器中获取卷的列表;
- 使用旧容器(卷,networking等)的configuration创build一个新容器
创build新的容器
我试图这样做:
docker_api.create_container( image=creation_data.get('image'), hostname=creation_data.get('hostname'), volumes=creation_data.get('volumes'), host_config=docker_api.create_host_config( binds=creation_data.get('volume_bindings'), port_bindings={80: 80}, restart_policy={"MaximumRetryCount": 0, "Name": "always"} ))
创build数据
其中creation_data
从旧容器中收集,如下所示:
{ 'image': 'docker.akema.fr:5000/coaxis/coaxisopt_daemon:latest', 'hostname': "test-01", 'volumes': [ "/home/mast/.ssh", "/etc/mast" ], 'volumes_bindings': { "841d6a1709b365763c85fb4b7400c87f264d468eb1691a660fe81761da6e374f": { 'bind': "/home/mast/.ssh", 'mode': 'rw' }, "002730cbb4dd9b37ad808915a60081508885d533fe003b529b8d0ab4fa46e92e": { 'bind': "/etc/mast", 'mode': 'rw' } }, 'networking_config': { 'EndpointsConfig': {'opt_network_508be7': {'IPAMConfig': {'IPv4Address': '10.0.0.1'}}} } }
题
在检查新的容器时, Mounts
部分似乎没有正确的体积, Source
字段是不同的path 。
如何根据旧容器信息将卷装载到新容器?
我不知道你对第一个容器的启动有多less控制,但是如果你这样做:
docker run --name container1 --volume vol1:/my/vol1/dir --volume vol2:/my/vol2/dir image1
要运行container1,您只需要执行此操作以重复使用container1的container1卷:
docker run --name container2 --volume vol1:/my/vol1/dir --volume vol2:/my/vol2/dir image2
然后移除不会移除卷的container1(即使它们还没有被其他容器重用)。
vol1和vol2数据将存储在主机上的/var/lib/docker/volumes/vol1/_data/
和/var/lib/docker/volumes/vol2/_data/
。
所以,答案是:不要使用匿名卷来满足这个需求,使用命名卷。 如果你有一个具有匿名卷的遗留容器,你需要重用,我想你可以把它们手工复制到这个新容器的命名卷中。
特里斯坦答案帮助我得到一个工作设置,然后我拒绝它docker-py
。 解决scheme是使用卷Name
值(例如a87bdc07881fcf139a29…
)作为装载点的源。
NB:也有一个相当恼人的错字: volumes_bindings
vs volume_bindings
。
检查
$ docker inspect my_container --format "{{ json .Mounts }}" | python -m json.tool [ { "Destination": "/etc/mast", "Driver": "local", "Mode": "", "Name": "a87bdc07881fcf139a29…", "Propagation": "", "RW": true, "Source": "/var/lib/docker/volumes/a87bdc07881fcf139a29…/_data", "Type": "volume" } ]
装入新的容器
docker create \ --name container \ --volume a87bdc07881fcf139a29…:/etc/mast \ docker.site.org:5000/coaxis/coaxisopt_daemon:latest
docker-py
new_container = docker_api.create_container( image=creation_data.get('image'), hostname=creation_data.get('hostname'), volumes=creation_data.get('volumes'), host_config=docker_api.create_host_config( binds=creation_data.get('volumes_bindings'), port_bindings={80: 80}, restart_policy={"MaximumRetryCount": 0, "Name": "always"} ))
与数据:
{ 'image': 'docker.akema.fr:5000/coaxis/coaxisopt_daemon:latest', 'hostname': "test-01", 'volumes': ["/etc/mast"], 'volumes_bindings': { "a87bdc07881fcf139a29…": { 'bind': "/etc/mast", 'mode': 'rw' } }, 'networking_config': {} }