Docker concept difficult to understand? Article get Docker port binding


Author | Dieter Jordens

Translator | Soviet Union such as this, Zebian | yugao

Exhibition |  CSDN (ID: CSDNnews)

The following is the translation:

            

As your primary developer, is not participated in this interview, during the interview the interviewer hope you answer it accurately works Docker's? Today interviewer candidates who hope to gain insight into 8, 10, or even more technical. In fact, it's a bit crazy. At a university or other school, they probably will not teach you anything about Docker's. However, if you really understand Docker, then you can stand out from a crowd of candidates in the.

When you start using Docker, one of the first problems encountered is that you can not connect to the Docker containers. This article will explain in detail the question why it happened, and I will explain the port binding (port binding) is how it works.

Even if you are an experienced developer, you should understand what is port binding. Otherwise, you will look silly in the interview. If you do not understand, so now I give myself to take a cup of coffee. I will let you put everything you want to know on this topic are recorded in your mind, you only need to spend six minutes!

Let's start with a Nginx Docker container it!

If you have a little knowledge of the Docker, you do not need to worry about. Because I will try to explain everything in detail about Docker. First, I need to make sure you understand the difference between Docker Docker container and mirror.

You can Docker image as a file that contains all the dependencies and configuration to run a particular program. Why do this? Because Docker want to solve the first problem is the system / installation nightmare.

We have experienced situations installer on Windows, Mac or Linux systems. However frustrating is that each time the system will prompt you for the lack of another program. Like the following system prompts as you do not want to install, and I guess every time you encounter this problem will choose to install, right?

You have to install this program do? ...

Eventually you will find that you not only need to install a lot of different programs, but often also need to configure the system variables, and so on. In the worst case, these would make a mess of your system.

You certainly do not want you in the organization everyone experiences this kind of trouble, right?

Docker mirror can help you solve this trouble, because a Docker image contains everything you need to install the program. While the vessel is running instance Docker Docker mirroring.

Docker for you to solve the nightmare of installation. The Docker container contains everything needed to run it, neither more nor less. You can run a Docker containers on Windows, Linux or Mac. Basically, if you will Docker installed, you can run it from anywhere.

About the advantages of Docker has put more than enough. Next, we need to do is to make a Docker container start running.

Let's start with a Nginx Docker containers. Nginx is running a web server on port 80. Here, I will use the Nginx Docker image in split mode (background) Docker start a container, the following command:

docker container run -d nginx

This command will generate a new Docker containers, you will see the new UUID Docker container. If you do not know what UUID so, then please read what I wrote this article for all content. Docker containers using ps command (docker ps), you will see this Docker container is active:

             

Now, if you try to use the curl command or use a browser to connect directly to the Docker container, you will encounter an error connection failure (see below). Because you can not connect directly to a Docker container, because what is it? Docker document just explained that port 80 is vulnerable ... this explanation somewhat unclear. But do not worry, I will give a detailed explanation in the next section!

curl -I 127.0.0.1:80
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused

Why can not I connect directly to a Docker container?

In fact, Docker containers can be connected to the outside world without any configuration. This is good, because we do not have to change anything we had before programming.

But by default, the outside world can not be connected directly to a Docker container.

Having said that, I think you should understand, and we expected that different. So how should you connect to your Docker container it? Well, there are a variety of options. Let's explore.

1. Docker open all ports

docker container run -P -d nginx

Here -P command for each port to open the container disclosed. Docker will be identified in Dockerfile each port disclosure, and the use of each port --expose Docker container build command with parameters disclosed. Each port open directly bound to a "random" port of the host.

Sounds good, but how can we find these ports it? Do not worry, we'll find your beloved port, and even a variety of ways to help find them. Next, I'll show you two different ways:

  • Docker container port

  • netstat

Our first option is to use the above Docker command (docker container port). You only need to type the above command and container UUID. You will see Docker container port 80 is bound to the IP address of the host port 32768 0.0.0.0 (if you try to execute this command, you'll see a different port).

docker container port *insert container_uuid*
80/tcp -> 0.0.0.0:32768

Our other option is to use the netstat command. To find all open ports, you can execute the following command. Note that the port Docker open mixed in among other ports, the ports in this case the third row is what we're looking for.

netstat -ntlp

     

       

All open ports using the netstat command to find.

2. open a specific port

             

Example port binding: Binding Docker container port 80 to the port 8080 of the host.

Docker open all ports are usually not a good idea, by default it simply does not open any ports, this is for safety reasons. You do not want to disclose all, because it did not have any benefit, right?

If you want to open a port, execute the following command:

docker container run -p 8080:80 -d nginx

This command causes Nginx container port 80 through the host port 8080 open to the public. Now, we can be connected to the container through a variety of ways. For example, using curl command or through a browser. This is not great!

Congratulations, now you finally understand the most important part Docker port bound! Now I'll show you the results of the curl command execution:

curl -I 0.0.0.0:8080HTTP/1.1 200 OK
Server: nginx/1.17.9
Date: Sun, 08 Mar 2020 11:38:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 03 Mar 2020 14:32:47 GMT
Connection: keep-alive
ETag: "5e5e6a8f-264"
Accept-Ranges: bytes

If you visit this address in your browser: 0.0.0.0: 8080, then your browser will show you the following content:

             

0.0.0.0:8080 connected to the case through a browser.

And one more thing

By default, Docker container ports will be disclosed to the IP address 0.0.0.0 (any IP address on the system and the match) on. You can also tell which IP to bind Docker, this IP may be 127.0.0.1, can also be a different IP address.

If you want to bind Docker container port 80 to the host system port 8000 and IP address 127.0.0.1 (also known as local host), you simply run the following command:

docker run -d -p 127.0.0.1:8000:80 nginx

Conclusion

For Docker containers, the port binding Docker is a very important concept. May start because of configuration issues need incoming connections (incoming connection), and be confusing. But Docker in terms of providing all documents required for doing a good job.

However, as a developer, the concept of Docker's more difficult to understand than some other concepts. Hopefully, reading this article, you've been very clear Docker port bound. If not, please leave a comment below!

Original link:

https://medium.com/better-programming/how-does-docker-port-binding-work-b089f23ca4c8

This article is a translation of the article CSDN, please indicate the source.

【End】

Recommended Reading 

Apple has abandoned or released in March cheap new iPhone; Jia Yueting respond family large claims; Microsoft no longer continue to develop Visual Basic | Geeks headlines

US $ 7.558 billion deal! America's largest 5G millimeter wave spectrum auction

super lightweight Chinese OCR, support for vertical text recognition, ncnn reasoning, the total model is only 17M

and hackers fight six days!

article for Spring Boot service monitoring, health checks, thread information, JVM heap information, metrics collection, monitor the operation!

with 3 "dove", tells you how to change the lightning network is encrypted messaging mode!

You look at every point, I seriously as a favorite

Released 1838 original articles · won praise 40000 + · Views 16,590,000 +

Guess you like

Origin blog.csdn.net/csdnnews/article/details/104912592