A simple understanding of Docker's hierarchy from the differences between Docker and Linux and Windows

Source: Internet
Author: User
Tags docker ps docker run

Last article we successfully installed Docker under Windows and exported a simple Hello World program. In this article we will use Docker's existing cloud image Training/webapp to publish a simple Python web program that outputs Hello World in a browser.

The test environment for this article is Docker under Windows7, and use cases are based on official document use cases.

one: From running a simple Python Web The program says

Launch the Docker client and log in. Enter the following in the client:

$ sudo docker run-d-p Training/webapp python app.py

The first time you enter the above command, Docker will first download the Training/webapp image from the Cloud Image library (the author has already downloaded the machine, Figure 1 skips this process), as shown in the successful run of 1.

Figure 1. Running

Let's take a closer look at the specifics of this command, which we can find in the official documentation in English, where I translate it and add some of my own understanding.

As we have said earlier, the Docker virtual machine system is actually based on the Linux kernel, so this statement is actually a Linux command, and we can enter the following to see a detailed description of the Docker command:

$ sudo docker

The output results are as follows:

Figure 2.docker Command List of subcommands

And then back to the first one, the subcommand we used was run.

In the list of subcommands, we see that the RUN subcommand is interpreted as: Run Run a command in a new container, that is, running a cmdlet in a fresh container.

The options that follow our Run command are the-D and-P.

Check the official documentation we can tell that the-D means that Docker puts the container for the current command in the background and monitors it. -P indicates that Docker maps any ports needed inside the container to the virtual machine. There is a need to understand some of the Docker concepts, and here we simply understand that each Docker container under a Docker virtual machine is a virtual machine, each with a virtual NIC, which must pass through the Docker virtual machine when it accesses the container externally. And-P makes the relevant port mappings.

A long string of strings (that is, fc4028b21d84f5c075821ca2425d74d45dea87cd28c4eb92620ae39ddec22f46) returned after the command executes is the container ID of Docker, which is a unique value, We can use it as an indicator to do the appropriate operation of the container.

When we need to view and manage all the containers that are currently running in the background, we can enter the following commands.

$ sudo docker PS

Figure 3. Container details

Here we can see the details of the container, including the container ID (compact ID), image, command, creation time, status, port, and container name.

The next training/webapp is the source of the container we run, called mirroring in Docker, which was created before Docker, which contains a simple Python Flask web App.

Finally, we executed the Python App.py,python app.py in the container to launch our web App.

Two: Visit page--from Linux and the Windows the difference to the Docker a simple understanding of the hierarchy

From the above we look at the details of running the container (Figure 3) we see that in the Ports column, there is this information:

0.0.0.0:49153->5000/tcp

As mentioned above, the Docker VM has port mapping to the container, and here we can see more intuitively that the 49153 ports in the Docker virtual machine are mapped to the 5000 ports of the container (here we are running a Web program, so it is the TCP protocol, Other containers have to be specified as UDP protocol).

In the official documentation, when we visit localhost:49153, we should be able to see Hello World, but I do not have access to this page at this step.

Figure 4: The browser is in a waiting state at this time

So here we need to look at the structure of this virtual network, to see the structure of the entire network, we should first understand the Docker hierarchy. In Linux, the logical structure of Docker is this:

Hardware < Linux systems (Docker Kernel) < docker containers

As we mentioned in the previous article, to run Docker in Windows is actually running under a virtual machine, so the logical structure of Docker in Windows should be:

Hardware < Windows systems < Docker virtual machines (Docker Kernel) < docker containers.

So the use case in official documents should be Linux-based, and Linux is no problem accessing localhost:49153 at this time, because Docker kernel now maps port 49153 to the container's 5000 port, and Windows does not. Because we just mapped the 49153 port of the Docker virtual machine to the container, we entered the localhost:49153 in the browser, which is obviously the Windows layer level of access.

Understand that the problem has been solved, we just go into the VirtualBox, the Docker virtual machine network to do a bridge, or port mapping on the line.

Go to the VirtualBox main interface, select the Docker virtual Machine (BOOT2DOCKER-VM), click the Settings button, select the network in the settings, and here we find that the Docker virtual machine chooses network address translation (NAT) by default.

Figure 5. The network connection for Docker virtual machines is NAT by default

There are also two port mappings by default in port forwarding.

Figure 6. Default port mappings for Docker virtual machines and host hosts

It's not hard to guess that this could be a port for the Docker client to communicate with a Docker virtual machine, so we can't change the way the Docker virtual machine is connected to a bridge, or the Docker client won't be able to communicate with the Docker virtual machine.

So here we choose to add a port map that maps the Windows system's 80 port to the 49153 port of the Docker virtual machine. The name of the port map is random, the protocol must be TCP (HTTP TCP-based)

Figure 7: Adding a mapped port

At this point we can directly access the localhost in the browser under Windows (in general, the browser default access to 80 port, 80 port can be omitted), because we directly mapped the 80 port to the Docker virtual machine 49153 port, The 49153 port of the Docker virtual machine is mapped to the 5000 port of the Docker container we just created, and our web program is running on port 5000 of that container.

Figure 8. Hello world!

Summarize:

This article actually only runs a simple Web program, and the use case is based on official documentation, and if the reader is running on Linux, the first command should be accessed directly in the browser. So this article is basically a simple explanation of the Docker Run command, and a simple understanding of the Docker logic hierarchy through the differences between Docker and Linux and Windows.

Hope to have some help for beginners, I am also a beginner of Docker, the article is a mistake, please forgive me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.