Docker Port mapping is often Docker Run By order -p Map the internal port of the container to the specified port of the host , Generally speaking, the port corresponding to the port of the container is determined in advance to be mapped . But in some cases you have to map ports temporarily , for example Docker It's running in the air MySQL Containers , The default port is not open . So what is the way to make the running container expose the specified port ? My guest, please look down --->

Port mapping method 1

By modifying the Docker To achieve our goal , Generally speaking, the following files need to be modified config.v2.json and hostconfig.json. The default path is /var/lib/docker/containers/< Container name >.

First, through systemctl stop docker close Docker service , Second, revise config.v2.json Of documents ExposedPorts Configuration information to add container internal ports, such as "8080/tcp":{}, And then modify hostconfig.json Medium PortBindings Configuration information is shown below .

After completing the above configuration systemctl restart docker restart docker service , And restart the specified container to access the corresponding port .( This method has risks and is not recommended for frequent use )

 "Config": {
"ExposedPorts": {
// Add internal ports 5432 mapping
"5432/tcp": {},
"8080/tcp": {}
},s
...
}, "PortBindings":{
// Add internal ports and external ports 15432
"5432/tcp":[
{
"HostIp":"",
"HostPort":"15432"
}
],
"8080/tcp":[
{
"HostIp":"",
"HostPort":"28080"
}
]
},

Port mapping method 2

Docker The principle of network port mapping is through Iptables Implement port forwarding , Based on this principle, we can directly use iptables Forward port to destination container Ip On . Port forwarding can be realized through the following command . This approach depends on Iptables The rules of , Some scenarios may lead to Iptables The rules of the container conflict, which affects the effective start of the container .

# Port mapping 
iptables -t nat -A DOCKER -p tcp --dport < Container external port > -j DNAT --to-destination < Containers ip>:< Internal port of container >
# Cancel port mapping rule
iptables -t nat -D DOCKER -p tcp -d 0/0 --dport < Container external port > -j DNAT --to-destination < Containers ip>:< Internal port of container >

Docker How to dynamically modify the container port mapping

  1. Docker Modify the created container port mapping

    Modify the created container port mapping . By editing the hostconfig.json File to modify Docker Port mapping for container The file address :/var/lib/docker/containers/[hash_of_the ...

  2. modify docker Container port mapping method

    Everybody knows docker run You can specify port mapping , But once the container is generated , There is no command that can be modified directly . Usually the indirect way is , Save mirrors , Create a new container , Specify a new port mapping at creation time . Is there any way to modify the image without saving it ...

  3. How to modify Docker The port mapping of the running instance

    How to modify Docker The port mapping of the running instance Docker Port mapping of , There are often two stages to deal with : 1. Is in docker It has been confirmed before startup , Which one? docker Which port does the instance map ( Often this situation is compared with , It needs to be done in advance ...

  4. Docker Containers ( 3、 ... and )—— Container port mapping

    (1). Container port mapping Container port mapping uses -p Options ,-p [ Physical machine port ]:[ Container instance port ] Give Way centos:httpd Running in the background [root@youxi1 ~]# docker run -d -p 8 ...

  5. 【Docker】-NO.132.Docker.1 -【Docker Modify container port 】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  6. 【docker】 Additional docker Container port mapping method

    docker run You can specify port mapping , But once the container is generated , There is no command that can be modified directly . Usually the indirect way is , Save mirrors , Create a new container , Specify a new port mapping at creation time . But it's too much trouble . Now there's a new solution : ...

  7. docker Simple operation and port mapping of

    One : brief introduction Docker Mirror image stay Docker The container in is started based on the image The mirror is the core of the boot container The image is layered , The top layer is the read-write layer Use snapshots COW technology , Make sure the bottom layer is not lost adopt ifconfig(ip  a) Check it out. d ...

  8. Docker Add container port mapping

    Reference article : https://blog.csdn.net/chouzhou9701/article/details/86725203

  9. How to modify the running docker Port mapping and mount directory of container

    stay docker run When you create and run a container , Can pass -p Specify port mapping rules . however , We often encounter that we forget to set the port mapping at the beginning or need to modify it due to the wrong setting . When docker start Running the container does not provide a -p Options or settings ...

  10. Teach you how to modify the running docker Port mapping for container

    stay docker run When you create and run a container , Can pass -p Specify port mapping rules . however , We often encounter that we forget to set the port mapping at the beginning or need to modify it due to the wrong setting . When docker start Running the container does not provide a -p Options or settings ...

Random recommendation

  1. php Regular

    PHP Code $str = preg_replace("/(<a.*?>)(.*?)(<\/a>)/", '\1<span class="li ...

  2. Every day php function - list() Assign values to a set of variables

    array list ( mixed $varname [, mixed $... ] ) image array() equally , This is not really a function , It's language structure .list() Assign values to a set of variables in one step . var ...

  3. Java Setup program making

    about Java For desktop applications , What's more cumbersome is the problem of installation and deployment , Such as : Whether the client has jre.jre edition .jre Where to download . How to use jre start-up Java Application and so on . Don't talk about people who have just come into contact with computers , Even if you're familiar with computers , If there's no answer ...

  4. THUSC2015

    These topics are in BZOJ It has , Unfortunately, it's a question of authority . Words BZOJ The above topic is really good , If I wasn't poor , I've been getting a number for a long time ! Get down to business , The difficulty of this year's topic can be seen from a famous saying : The topic is too watery .-- So and so God But I thought it over , Find out ...

  5. String Class usage

    1.1. Byte to string conversion |- byte -->String:public String(byte[]  bytes)        |-String--> byte : public byte[] ...

  6. One a day linux command (53)--ps command

    To ruin the day , Starting in the morning . Linux Medium ps The order is process status Abbreviation .ps The command is used to list the processes currently running in the system .ps The command lists a snapshot of the current processes , Is to perform ps The moment of command ...

  7. Alibaba cloud Lightweight application servers (LAMP) Use logging

    phpStudy(PHP Running environment one click installation package ) https://www.jb51.net/softs/182860.html 0:PHP development tool https://netbeans.org/downloa ...

  8. Java Case insensitive CaseInsensitiveMap

    Java For key value pairs in , We are used to using classes HashMap, Usage mode :Map<String, String> result=new HashMap<String,String>(); ...

  9. The batch ------ @、ECHO OFF、ECHO ON Use

    1. In a batch file , If the order is preceded by @, This command doesn't print out , Just print out the results , namely @ It turns off the echo of the command itself 2.:: In batch processing, a line is annotated 3.ECHO ON In the next command ( Not including this order ), Carry out orders ...

  10. ASP.NET MVC 4 ( Nine ) Model binding

    Model binding refers to MVC Sent from the browser HTTP Create... For us in the request .NET object , stay HTTP Request and C# It's a bridge . One of the simplest examples of model binding is controller with parameters action Method , For example, we can register such a path mapping : ...