DEV Community

Marc Ziel
Marc Ziel

Posted on

2 1

Publishing a port on an existing docker container

When you need to publish some port on a container that was already run, you can do this by changing internal docker files.

First, you need to find the container id with

docker inspect <container_name>
Enter fullscreen mode Exit fullscreen mode

Stop the container if it's running and go to the container directory:

cd /var/lib/docker/containers/<container_id>
Enter fullscreen mode Exit fullscreen mode

For wsl users, the container directory can be found here:
\\wsl$\docker-desktop-data\version-pack-data\community\docker\containers

add "ExposedPorts" to config.v2.json:

"Config": {
    ....
    "ExposedPorts": {
        "<port_number>/tcp": {},
    },
    ....
},
Enter fullscreen mode Exit fullscreen mode

and "PortBindings" to hostconfig.json:

"PortBindings": {
     "<port_number>/tcp": [
         {
             "HostIp": "",
             "HostPort": "<port_number>"
         }
     ]
 }
Enter fullscreen mode Exit fullscreen mode

For best results restart docker service:

systemctl restart docker
Enter fullscreen mode Exit fullscreen mode

Now you can start the container and it should have ports published.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay