docker / docker-py

A Python library for the Docker Engine API
https://docker-py.readthedocs.io/
Apache License 2.0
6.81k stars 1.67k forks source link

'link' parameter does not work with client.containers.create(...) #1857

Open kocsob opened 6 years ago

kocsob commented 6 years ago

If I set the 'links' parameter it does not give an error, but it does not work as well. I think this issue comes from: https://stackoverflow.com/questions/37144357/link-containers-with-the-docker-python-api

But in High level API I cannot put the host_config parameter in create(), because

TypeError: run() got an unexpected keyword argument 'host_config'.

SDK version, Docker version and python version

docker==2.7.0

Python 3.6.4

Client:
 Version:   17.12.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    486a48d270
 Built: Fri Dec 29 12:13:10 2017
 OS/Arch:   linux/amd64
 Experimental:  false

Server:
 Engine:
  Version:  17.12.0-ce
  API version:  1.36 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   486a48d270
  Built:    Fri Dec 29 12:13:27 2017
  OS/Arch:  linux/amd64
  Experimental: false

If possible, steps or a code snippet to reproduce the issue:

import docker
import docker.errors

NETWORK_NAME = 'test-network'
CONTAINER_NAME = 'test'
DOMAIN = 'example.com'

client = docker.from_env()

try:
    network = client.networks.get(NETWORK_NAME)
except docker.errors.NotFound:
    network = client.networks.create(NETWORK_NAME)

links = {CONTAINER_NAME: CONTAINER_NAME + '.' + DOMAIN}

container = client.containers.create(
    image='alpine:latest',
    command='/bin/sleep 3600',
    network=network.name,
    name=CONTAINER_NAME,
    links=links,
)
container.start()

The same functionality with docker command:

 docker run --name=test --link=test:test.example.com --network test-network alpine:latest /bin/sleep 3600

Relevant diff of the docker inspect:

              "Networks": {
                  "test-network": {
                      "IPAMConfig": null,
 -                    "Links": null,
 +                    "Links": [
 +                        "test:test.example.com"
 +                    ],

The problem is that "Link" is 'null'. This works good with command line '--link' parameter.

shin- commented 6 years ago

You need to provide links as part of the networking_config argument in create_container. The links parameter in HostConfig is for legacy links, which is a deprecated feature that only applies to the default bridge network.

kocsob commented 6 years ago

And links parameter for user defined network can be provided somehow to the high level API for client.containers.create(..) or it works only with low level create_container(...)?

The documentation shows something similar in create parameters, why I am confused.