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

Recommend

A "classic" pitfall of MySQL UPDATE statement

Table of contents 1. Problematic SQL statements S...

MySQL inspection script (must read)

As shown below: #!/usr/bin/env python3.5 import p...

Flash embedded in web pages and IE, FF, Maxthon compatibility issues

After going through a lot of hardships, I searched...

Several specific methods of Mysql space cleaning

Table of contents Preface 1. Check the file disk ...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

Summary of Mysql exists usage

Introduction EXISTS is used to check whether a su...

HTML table tag tutorial (8): background image attribute BACKGROUND

Set a background image for the table. You can use...

jQuery realizes the picture following effect

This article shares the specific code of jQuery t...

How to install suPHP for PHP5 on CentOS 7 (Peng Ge)

By default, PHP on CentOS 7 runs as apache or nob...

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

Analysis and summary of the impact of MySQL transactions on efficiency

1. Database transactions will reduce database per...