简体   繁体   中英

Docker SDK/API How to assign IP to container in python?

How do I write the ip parameter of the following command using docker sdk in python? I need the --ip parameter to be written in python in this run command

docker run -p 2035:2035 -p 5026:5026/udp -p 5307:5307 -p 6233:6233 -p 6343:6343 -p 3140:3140 -p 8181:8181 -p 221:22 --privileged=true --restart=always --net=mynetwork --ip=172.22.5.361 --hostname mycont --name mycont /root/Applications/script/start.sh

This is the part I wrote. But IP parameter is not found in docker sdk manual. That's why I'm stuck here.

client = docker.from_env()
imageTag = input("image tag: ")
client.containers.run('193.163.23.29:5001/myway/myways:'+imageTag, detach=True, ports={2035:2035,'5026/udp':5026,5307:5307,6233:6233,6343:6343,3140:3140,8181:8181,22:221},restart_policy={"Name": "always"}, network='mynetwork', hostname='mycont', name='mycont', command='/root/Applications/script/start.sh')

The easiest thing to do here is to ignore this option, and also remove it from the docker run invocation. You almost never need the container-private IP address for anything and it's almost never necessary to manually set it. It's not usable in a wide variety of common situations, and you can almost always either use Docker networking (between containers) or a published port instead.


This having been said: this is possible using the low-level API . This returns a container ID and not a Container object. This creates the container but does not start it, so you'll have to do that step separately.

container = client.api.create_container(
  '193.163.23.29:5001/myway/myways:'+imageTag,
  networking_config=client.api.create_networking_config({
    'mynetwork': client.api.create_endpoint_config(
      ipv4_address='172.22.5.36'
    )
  }),
  ...
)
client.api.start(container['Id'])

Several other options are different, and this follows the REST API 's option layout more than idiomatic Python.

I recommend checking out this thread: https://github.com/docker/docker-py/issues/2006 . It hasn't entirely worked for me yet, but I am trying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM