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

Methods and techniques for quickly displaying web page images

1. Use .gifs rather than .jpgs. GIFs are smaller ...

Implementation of Nginx configuration of local image server

Table of contents 1. Introduction to Nginx 2. Ima...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

JavaScript to implement drop-down list selection box

This article example shares the specific code of ...

Detailed explanation of the pitfalls of MySQL 8.0

I updated MySQL 8.0 today. The first problem: Nav...

IDEA2020.1.2 Detailed tutorial on creating a web project and configuring Tomcat

This article is an integrated article on how to c...

How to deploy Oracle using Docker on Mac

How to deploy Oracle using Docker on Mac First in...

Mac+IDEA+Tomcat configuration steps

Table of contents 1. Download 2. Installation and...

Detailed tutorial on installing Prometheus with Docker

Table of contents 1. Install Node Exporter 2. Ins...

MySQL column to row conversion and year-month grouping example

As shown below: SELECT count(DISTINCT(a.rect_id))...

10 HTML table-related tags

In fact many people will say “I’ve seen that table...

Vue global filter concepts, precautions and basic usage methods

Table of contents 1. The concept of filter 1. Cus...

Solution to the blank page after vue.js packaged project

I believe that many partners who have just come i...

Vue implements top left and right sliding navigation

Navigation and other things are often used in dai...

Detailed explanation of XML syntax

1. Documentation Rules 1. Case sensitive. 2. The a...