How to modify the port mapping of a running Docker container

How to modify the port mapping of a running Docker container

Preface

When docker run creates and runs a container, you can specify port mapping rules using -p. However, we often forget to set up port mapping at the beginning or set it up incorrectly and need to modify it. When docker start runs the container, it does not provide a -p option or setting that allows you to modify the specific port mapping rules. So how should we deal with this situation? Today, Docker will teach you how to modify the port mapping of a running Docker container?

Method 1: Delete the original container and create a new one

This solution is the simplest. Just delete the original container and create a new one. Of course don't forget to add port mapping this time.

Advantages and disadvantages: The advantage is that it is simple and fast, and is more commonly used in test environments. The disadvantage is that if it is a database mirror, then rebuilding it and reconfiguring it again is more troublesome.

Method 2: Modify the container configuration file and restart the Docker service

Container configuration file path:

/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json

The hashofthecontainer is the hash value of the Docker image, which can be viewed through docker ps or docker inspect containername. (You can tell from the CONTAINER ID)

As shown above, one of the items in the file is PortBindings, where 8080/tcp corresponds to port 8080 inside the container, and HostPort corresponds to port 9190 mapped to the host. 8361/tcp corresponds to port 8361 inside the container, and HostPort corresponds to port 9191 mapped to the host. Modify the port as needed, restart the docker service, and then start the container service.

systemctl restart docker

Advantages and disadvantages: The advantage of this method is that it has no side effects and is simple to operate. The disadvantage is that the entire Docker service needs to be restarted. If multiple container services are running on the same host, other container services will be affected.

Method 3: Use docker commit to build a new image

Docker commit: commit a container's file changes and configuration information to a new image. This is very useful when testing. You can import all the file changes and configuration information of the container into a new Docker image, and then restart a container with this new image. This will not affect the previous container.

1. Stop the Docker container

docker stop container01

2. Commit the docker container

docker commit container01 new_image:tag

3. Start a new container using the newly generated image in the previous step

docker run --name container02 -p 80:80 new_image:tag

Advantages and disadvantages: The advantage of this method is that it will not affect other containers on the same host machine. The disadvantage is that it is more messy to manage and is not as intuitive as the second method.

Summarize

This is the end of this article about how to modify the port mapping of a running docker container. For more information about how to modify the port mapping of a running docker container, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Troubleshooting process for Docker container suddenly failing to connect after port mapping
  • Docker port mapping and external inaccessibility issues
  • Add port mapping after docker container starts
  • How to set port mapping for running container in Docker
  • Demonstration and analysis of four port mappings of docker containers

<<:  Some parameter descriptions of text input boxes in web design

>>:  Introduction to Vue life cycle and detailed explanation of hook functions

Recommend

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

Practical example of Vue virtual list

Table of contents Preface design accomplish summa...

A brief discussion on the principle of React two-way data binding

Table of contents What is two-way data binding Im...

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

Detailed installation and configuration of Subversion (SVN) under Ubuntu

If you are a software developer, you must be fami...

Detailed process of installing nginx1.9.1 on centos8

1.17.9 More delicious, really Nginx download addr...

Implementing carousel effects with JavaScript

This article shares the specific code for JavaScr...

MySQL 4 common master-slave replication architectures

Table of contents One master and multiple slaves ...

Summary of CSS3 practical methods (recommended)

1. Rounded border: CSS CodeCopy content to clipbo...