flypenguin
flypenguin

Reputation: 735

link containers with the docker python API

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?

Upvotes: 4

Views: 6101

Answers (3)

josepainumkal
josepainumkal

Reputation: 1733

Below is the current working way to do it.

links=[('postgres-modeldb', 'modeldb'),('postgres-userdb', 'userdb'),('redis-workerdb', 'workerdb')]

host_config = client.create_host_config(
    links=links
)

networking_config = client.create_networking_config({
    'my-net': client.create_endpoint_config(
        links=links
    )
})

container = client.create_container(
    image='josepainumkal/vwadaptor:jose_toolUI',
    name=container_name,
    host_config=host_config,
    networking_config = networking_config
) 

response = client.start(container=container.get('Id'))

Upvotes: 0

ootwch
ootwch

Reputation: 960

Yes, the networking documentation for docker-py is seriously lacking - the maintainers agree (https://github.com/docker/docker-py/issues/982 for a global alias example).

Note that Valeriy's answer above will create a legacy link, which might (will in my case) lead to issues if you use a non-default network such as the ones created by docker-compose.

In any case, adding parameters to Client.start is depreciated.

The new way to do this can be found in the unitttest: https://github.com/docker/docker-py/blob/master/tests/integration/network_test.py#L190-213

@requires_api_version('1.22')
def test_create_with_links(self):
    net_name, net_id = self.create_network()

    container = self.create_and_start(
        host_config=self.client.create_host_config(network_mode=net_name),
        networking_config=self.client.create_networking_config({
            net_name: self.client.create_endpoint_config(
                links=[('docker-py-test-upstream', 'bar')],
            ),
        }),
    )

    container_data = self.client.inspect_container(container)
    self.assertEqual(
        container_data['NetworkSettings']['Networks'][net_name]['Links'],
        ['docker-py-test-upstream:bar'])

    self.create_and_start(
        name='docker-py-test-upstream',
        host_config=self.client.create_host_config(network_mode=net_name),
    )

    self.execute(container, ['nslookup', 'bar'])

Valeriy's Example would then look as follows:

Python Example

from docker import Client
cli = Client(base_url='unix://var/run/docker.sock', version='auto')

# Note: 'bridge' is the default network
net_config = cli.create_networking_config(
        {'bridge': self.docker_client.create_endpoint_config(
            links=[('EXISTING_CONTAINER', 'LINK_NAME')]
        )}
    )

container = cli.create_container(
  image='busybox:latest',
  command='/bin/sleep 30',
  network_configuration=net_config 
)
response = cli.start(container=container.get('Id'))

I have not tested this specific code, but this is the way I have been able to connect a new container to an existing container, whereas the existing one had been created by compose into a network "project_default"

You might also want to check this link for more information and background.

Upvotes: 2

Valeriy Solovyov
Valeriy Solovyov

Reputation: 5648

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.

create_container:

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')])

The working example (DO)

I am using CoreOS on DO:

  1. run docker container and mount inside the /var/run/docker.sock from host
  2. install tools
  3. run test container EXISTING_CONTAINER
  4. run python example

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

Upvotes: 4

Related Questions