Using the Python docker client docker-py, how do you create a container which is attached to a specific network, and exposes a specific port?
Ie the compose file may look like this:
foo:
...
expose:
- "1234"
networks:
- my-network
We expose a port within my-network, without mapping it to the host.
Using docker-py's high level containers.run method only allows for Port Mappings (ie publishing ports externally) with it's ports
kwarg, rather than simply exposing the port internally.
docker-py's low-level create_container method doesn't accept a simple network name to use.
How can the above compose file be replicated using docker-py?
Thanks
"Expose" as a verb hasn't meant much since Docker introduced multiple named Docker-internal networks. You can delete this setting from your docker-compose.yml
file and it won't affect anything; other services on the same network will still be able to reach yours. That is, it should be enough to run
client.containers.run('my-image', network: 'my-network')
From what I can tell the docker-py library never supported this option (even during the time when --link
was the only way to connect containers, manual --expose
was never that useful). docker/docker-py#2242 asks a similar question, with a similar answer.