DEV Community

Marc Ziel
Marc Ziel

Posted on

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.

Top comments (0)