docker-py: attach to network before starting the container

Until recently, I could attach a container to a network before starting the container as follows:

params = {
    'image': 'busybox',
    'name': 'test',
}
result = docker_client.create_container(**params)
docker_client.connect_container_to_network(result['Id'], network_id)
docker_client.start(result['Id'])

However, now I always get an error, because the container has not been started when I’m connecting it to the network:

  • Apache HTTP ProxyPass inside Docker container linked to other containers: Wrong remote IP interpreted by linked container
  • Supervisor on a Digital Ocean docker image
  • Query docker embedded dns from host
  • What is the significance of the user in `/etc/sub{u,g}id`? (Help me grok user namespaces)
  • COS is running out of inodes for /var/lib/docker volume
  • libgit2 alpine linux docker error
  • APIError: 500 Server Error: Internal Server Error ("Container 0b1005fc86be565d1a54c44f89f2a60d338b541f8b73805c2a367116bf04a060 is not running")
    

    I can reproduce the same error with the command-line client:

    % docker create --name test busybox
    7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e
    % docker network connect test 7aa04b908b2e
    Error response from daemon: Container 7aa04b908b2ec45a37f272809ec909116cfae5ae80a13c6596822ca4d9b39a0e is not running
    

    So I need to connect the container to the network directly when I create the container:

    % docker create --net=test busybox test
    

    In docker-py, how do I attach a container to a network directly during creation?

  • Error Connecting to Docker hub
  • Download Github build artifact (release) using wget/curl
  • How to run Recurrent Neural Networks sample programs is tensorflow?
  • Disable AppArmor for Docker for ptrace_scope
  • Providing host files for docker machine with cloud provider
  • Port forwarding with docker for wordpress
  • 2 Solutions collect form web for “docker-py: attach to network before starting the container”

    You can use the HostConfig to configure the network the container is attached to:

    network_mode is available since v1.11 and sets the Network mode for the container (‘bridge’: creates a new network stack for the container on the Docker bridge, ‘none’: no networking for this container, ‘container:[name|id]’: reuses another container network stack, ‘host’: use the host network stack inside the container or any name that identifies an existing Docker network).

    As an example, you would run:

    params = {
        'image': 'busybox',
        'name': 'test',
        'host_config': docker_client.create_host_config(network_mode='test')
    }
    docker_client.create_container(**params)
    

    This connects the container to the network test.

    Try in this way:

    host_config = client.create_host_config(
        network_mode='my-net'
    )
    networking_config = client.create_networking_config({
        'my-net': client.create_endpoint_config(
        )
    })
    container = client.create_container(
        image='josepainumkal/vwadaptor:jose_toolUI',
        host_config=host_config,
        networking_config = networking_config
    ) 
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.