How to bind Docker container to external IP and port

How to bind Docker container to external IP and port

Docker allows network services to be provided by externally accessing containers or by interconnecting containers.

The following operations are simulated through the myfirstapp image. For how to create a myfirstapp image, please click here.

1. External access container

After the container is started, some network applications can be run in the container, and port mapping can be specified through the -p or -P parameter.

a. When using the -P (uppercase) flag, Docker will randomly select a port and map it to the open network port inside the container.

$ docker run -d -P myfirstapp python app.py 
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbcf5d3290ba myfirstapp "python app.py" 4 seconds ago Up 4 seconds 0.0.0.0:32770->80/tcp quizzical_engelbart

At this time, accessing port 32770 of the local machine can access the interface provided by the web application in the container.

$ curl http://192.168.1.160:32770/
<h3>Hello HELLO!</h3><b>Hostname:</b> dbcf5d3290ba<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i> 

You can also use docker logs to view application information

$ docker logs dbcf5d3290ba
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
192.168.1.5 - - [29/Mar/2018 06:17:34] "GET / HTTP/1.1" 200 -
192.168.1.5 - - [29/Mar/2018 06:17:34] "GET /favicon.ico HTTP/1.1" 404 -
192.168.1.160 - - [29/Mar/2018 06:17:43] "GET / HTTP/1.1" 200 -
192.168.1.5 - - [29/Mar/2018 06:18:59] "GET / HTTP/1.1" 200 -

b. When using the -p (lowercase) flag, you can specify the port to be mapped, and only one container can be bound to a specified port. The supported formats are:

ip:hostport:containerport 
ip::containerport 
hostport:containerport

$ docker run -d -p 4000:80 myfirstapp python app.py
$ docker run -d -p 4001:80 myfirstapp python app.py
$ curl http://192.168.1.160:4000/
Hello HELLO!Hostname: f43ed2810353
Visits:cannot connect to Redis, counter disabled
$ curl http://192.168.1.160:4001/
Hello HELLO! Hostname:f43ed2810353
Visits:cannot connect to Redis, counter disabled

You can see that the local port 4000 has been mapped, and another port 4001 has also been mapped.

By default, Docker maps all local addresses. Now let's try to map a specified port to a specified address.

$ docker run -d -p 127.0.0.1:4000:80 myfirstapp python app.py
$ curl http://127.0.0.1:4000/ ##Access via $ curl http://localhost:4000/ ##Access via $ curl http://192.168.1.160:4000/ ##Access denied curl: (7) Failed connect to 192.168.1.160:4000; Connection refused

Then, to access the application in the container, you can only access it through the IP address 127.0.0.1.

The next step is to bind any port on the local machine to port 80 of the container and randomly assign a port

$ docker run -d -p 127.0.0.1::80 myfirstapp python app.py
$ docker ps ##127.0.0.1:32770->80/tcp
$ curl http://127.0.0.1:32770/ ##Access via $ curl http://localhost:32770/ ##Access via $ curl http://192.168.1.160:32770/ ##Access denied

You can also specify the communication protocol

docker run -d -p 5003:80/udp myfirstapp python app.py

2. View the ports and IP addresses bound and mapped to the container

$ docker port 44de1b0b5312 (container ID)
80/tcp -> 127.0.0.1:32770

3. Notes

a.docker inspect to view the container's own internal network and IP address

$ docker inspect 44de1b0b5312 (container ID)

b. Bind multiple IP addresses when starting the container

$ docker run -d -p 5005:5000 -p 5006:80 myfirstapp python app.py #Container ID: 44e703c1279a
$ docker port 44e703c1279a
5000/tcp -> 0.0.0.0:5005
80/tcp -> 0.0.0.0:5006

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to set port mapping for running container in Docker
  • Add port mapping after docker container starts
  • Troubleshooting process for Docker container suddenly failing to connect after port mapping
  • Detailed explanation of Docker port mapping and container interconnection
  • Detailed explanation of how to modify container port mapping in Docker
  • Solution to the Docker container being unable to access the host port
  • Perfect solution to the problem of not being able to access the port of the docker container under Windows 10
  • Docker dynamically exposes ports to containers
  • Detailed explanation of Docker container network port configuration process
  • How to modify the port mapping of a running Docker container

<<:  MYSQL A question about using character functions to filter data

>>:  HTML+CSS+JS realizes canvas follows the mouse small circle special effect source code

Blog    

Recommend

11 Reasons Why Bootstrap Is So Popular

Preface Bootstrap, the most popular front-end dev...

Query the data of the day before the current time interval in MySQL

1. Background In actual projects, we will encount...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

How to install Nginx in a specified location in Centos system

How to install Nginx in a specified location in C...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

Detailed explanation of virtual DOM and diff algorithm in react

The role of virtual DOM First of all, we need to ...

How to make a tar file of wsl through Docker

I've been playing with the remote development...

Detailed explanation of Vue development website SEO optimization method

Because the data binding mechanism of Vue and oth...

Simple implementation method of two-way data binding in js project

Table of contents Preface Publish-Subscriber Patt...

Native js to realize the upload picture control

This article example shares the specific code of ...

How to display a small icon in front of the browser URL

When you browse many websites, you will find that ...

Writing daily automatic backup of MySQL database using mysqldump in Centos7

1. Requirements: Database backup is particularly ...

Bug of Chinese input garbled characters in flex program Firefox

Chinese characters cannot be input in lower versio...