question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Equivqlent of docker run --expose {port} in docker-py

See original GitHub issue

Hi

I’ve been trying to expose ports with this library as you would with docker create --expose 80 but have been unable to. I can’t seem to find anything in the documentation on how to do this. Any help would be appreciated.

OS: Ubuntu 18.04

~$ pip3 freeze | grep docker && python3 --version && docker version
docker==3.6.0
docker-pycreds==0.4.0
Python 3.6.7
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:49:01 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:16:44 2018
  OS/Arch:          linux/amd64
  Experimental:     false

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
to-beecommented, Jan 12, 2020

Try this:

import docker
from docker.models.containers import Container
client = docker.from_env()
container: Container = client.containers.run(
    ...
    ports={5432: []},
    ...
)
1reaction
Resistycommented, Nov 1, 2019

Hello,

I think there’s a subtlety being missed. If I use docker-py to start a container with

import docker

c = docker.from_env()
ctnr_id = c.api.create_container('image', 'command', ports={8080: None}

I can see with docker inspect that it comes across as

            "Ports": {
                "8080/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "32801"
                    }
                ]
            },

However, if I run the container directly with --expose 8080, docker inspect shows

"Ports": {
                "1935/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1935"
                    }
                ],
                "8080/tcp": null
            },

Additionally, I’m unable to hit the container’s ip address on port 8080 with docker-py’s setup, but I am able to hit it with the container that used --expose 8080.

I could be missing some critical piece of docker know-how, here, so any help/advice is greatly appreciated. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Port Bindings - docker-py Documentation
Port bindings is done in two parts. Firstly, by providing a list of ports to open inside the container in the Client().create_container() method....
Read more >
Run your image as a container
Let's start our image and make sure it is running correctly. Run the following command in your terminal. $ docker run python-docker.
Read more >
python - What is the equivalent command for docker port ...
I trying to automate a docker server using docker-py. Need to check whether the host URL is pinging or not using python. Hence...
Read more >
docker-py
Client(base_url='unix://var/run/docker.sock', version='1.9', timeout=10). base_url refers to the protocol+hostname+port where the docker server is hosted.
Read more >
Running Python on Docker - Earthly Blog
The container image can be pushed and retrieved from the Docker Hub registry. Docker Hub is an open source library and community for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found