I want to use the docker python API (pip install docker-py
) to create a container and link it to an existing container which I created with docker-compose.
Using the command line this is easy:
docker run --link EXISTING_CONTAINER:LINK_NAME mycontainer:mytag
But using the docker API I'm stuck. I think that I have to use the docker.Client().create_container()
method, which takes an - undocumented - parameter links=
. (I strongly think the documentation is pretty much incomplete ...).
I tried reading the docker-compose code, and this seems to use the links=
parameter, but I could not figure out how.
My initial attempt did not work:
client_obj.create_container(..., links=(('EXISTING_CONTAINER', 'LINK_NAME'),))
... which is what I think the docker-compose code is doing.
Can someone help me out here?
https://github.com/docker/docker-py
A Python library for the Docker Remote API. It does everything the docker command does, but from within Python – run containers, manage them, pull/push images, etc.
Creates a container that can then be .start() ed.
Parameters are similar to those for the docker run
command except it doesn't support the attach options (-a).
The source code of create_container
def create_container(self, image, command=None, hostname=None, user=None,
detach=False, stdin_open=False, tty=False,
mem_limit=None, ports=None, environment=None,
dns=None, volumes=None, volumes_from=None,
network_disabled=False, name=None, entrypoint=None,
cpu_shares=None, working_dir=None, domainname=None,
memswap_limit=None, cpuset=None, host_config=None,
mac_address=None, labels=None, volume_driver=None,
stop_signal=None, networking_config=None):
But I found links
at start function:
def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
publish_all_ports=None, links=None, privileged=None,
dns=None, dns_search=None, volumes_from=None, network_mode=None,
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
extra_hosts=None, read_only=None, pid_mode=None, ipc_mode=None,
security_opt=None, ulimits=None):
So I think you should:
from docker import Client
>>> cli = Client(base_url='tcp://127.0.0.1:2375')
>>> container = cli.create_container(
... image='busybox:latest',
... command='/bin/sleep 30')
>>> response = cli.start(container=container.get('Id'),links=[('EXISTING_CONTAINER', 'LINK_NAME')])
I am using CoreOS on DO:
/var/run/docker.sock
from hostEXISTING_CONTAINER
The set of commands:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:12.04 bash
apt-get update;apt-get install python-pip -y;pip install docker-py
docker run -d --name EXISTING_CONTAINER busybox sh -c "while true; do sleep 1;done"
Python example
from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')
container = cli.create_container(
image='busybox:latest',
command='/bin/sleep 30')
response = cli.start(container=container.get('Id'),links=(('EXISTING_CONTAINER', 'LINK_NAME'))
The result on host:
wp-coreos-512mb-ams2-01 ~ # docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2f58e661579d busybox "sh -c 'while true; d" 23 seconds ago Up 22 seconds EXISTING_CONTAINER
6f08dd3f5017 busybox:latest "/bin/sleep 30" 9 minutes ago Up 5 seconds condescending_brown