Port mapping for docker containers - Updating ports of running docker containers

 Senario: 

Weblogic container consisted of 3 servers on port 7001, 8001 & 9001. On addition of additional server there was a need to expose and additional port 9002. As port 9002 was not exposed the services over the port ver not exposed externally.

Below is the image of the current state of the container using docker ps command.



We expect to expose the port 9002 without recreating the container.



Above image shows the expected port post updates.

Solution:

1. list the containers using docker command.
docker ps
2. Shut down all the containers.
docker stop <contianer-id>
3. Shut down the docker service.
systemctl stop docker

4. Traverse to the directory 

cd /var/lib/docker/containers

5. Directory with the name of the container id would be in the above location. 

6. Move to the container directory & list the files. There are 2 files that are of our interest.

-rw-r--r--. 1 root root   1689 Jun  6 14:58 hostconfig.json

-rw-------. 1 root root   4903 Jun  6 14:58 config.v2.json

7. Take backup of hostconfig.json & config.v2.json

8. Open the config.v2.json and find the property exposed ports & add the additional port required in the similar format as the existing one.

e.g. - "9002/tcp":{}


9. Open the hostconfig.json and find the property exposed ports & add the additional port required in the similar format as the existing one.
e.g. - "9002/tcp":[{"HostIp":"","HostPort":"9002"}]

10. Restart the docker engine.
systemctl start docker

11. Verify if the new ports are mapped.


Summary: 
Docker ports can be updated in 2 ways, 
1. Stopping the container, commit the container & execute the run command adding he ports.
2. As defined above  hostconfig.json & config.v2.json files can be updated with the specific ports. 




















Post a Comment

0 Comments