1.
Download Nginx
[[Email protected] my. shells]# Docker imagesrepository TAG IMAGE ID CREATED sizedocker.io/redis latest 1E70071F4AF4 6 weeks ago 106.7 Mb[[email protected] my. shells]# Docker Pull Nginx //download nginxusing default tag:latesttrying to pull repository docker.io/library/nginx ... lat Est:pulling from Docker.io/library/nginxe7bb522d92ff:pull complete 6edc05228666:pull complete Cd866a17e81f:pull compl Ete Digest:sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d[[email protected] my. shells]# Docker imagesrepository TAG IMAGE ID CREATED sizedocker.io/nginx latest 3f8a4339aadd 3 weeks ago 108.5 mbdocker.io/redis latest 1e70071f4af4 6 weeks ago 106.7 MB
2.
Run
[[Email protected] my. shells]# Docker run-p 8080:80-d Docker.io/nginx //80 port is mapped to 8080, or 80:80 or the original 80 port, can not be written. C0462d5e18783e20f9515108fa62ab0f2ac808ea85370a7c82aee9407abf4672[[email protected] My. shells]# NETSTAT-ANP | grep 8080 //port already open tcp6 0 0::: 8080 :::* LISTEN 2529/docker-proxy-c [[email protected] my. shells]# Docker PS //nginx is already running the container ID IMAGE COMMAND CREATED STATUS PORTS namesc0462d5e1878 docker.io/nginx "nginx-g ' daemon Off" 4 minutes ago up 4 minutes 0.0.0.0:8080->80/tcp Angry_mccarthy
3.
Run Results
[[Email protected] my. shells]#./openfirewallport.sh //First open a port on the fire wall enter the Port:8080success
---openfirewallport.sh-------
echo "Enter the port:"
Read Port
Firewall-cmd--add-port= $port/tcp
has successfully accessed the
Note: When Docker runs Nginx, the outside access is the same IP address as the Docker, which is equivalent to the nginx running on that machine. But for the machine where Docker is located, Nginx is a mirror attached to Docker. If the operation Nginx or Docker login Nginx container, to operate. The login Nginx container is a Linux system, but only Nginx, Nginx installed according to the Linux default path. For example [email protected]:/usr/share/nginx/html# ls This path is the default static page storage path 50x.html Index.htmlbash commands are the same, but VI in my machine is not available, but can use CP, MV and other commands, because Nginx are configured well, can not be changed.
1) You can write the required files before you log in to the Nignx container, and then copy them to the specified directory:[[email protected] my. shells]# Docker CP hello.html C0462d5e1878://usr/share/nginx/html[[email protected] my. shells]# Docker exec-it c0462d5e1878 bash[email protected]:/usr/share/nginx/html# ls50x.html hello.html Index.html
2)
2) mapping to containers via host directory
Docker.io/nginx
-V $PWD/html:usr/share/nginx/html indicates that the current path under the HTML directory is mapped to usr/share/nginx/html
This means that the HTML under the host is the usr/share/nginx/html under the container.
File modifications and additions in HTML are equivalent to container usr/share/nginx/html file operations
Network access can be accessed, you do not have to log into the container operation files
4.
Stop Service
[[Email protected] my. shells]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS namesc0462d5e1878 Docker.io/nginx "nginx-g ' daemon off" minutes ago up, minutes 0.0.0.0:8080->80/tcp Angry_mccarthy[[email protected] My. shells]# Docker stop c0462d5e1878 c0462d5e1878[[email protected] my. shells]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.
Restart Service
[[Email protected] my. shells]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS names[[email protected] My. shells]# Docker start c0462d5e1878 c0462d5e1878[[email protected] my. shells]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS namesc0462d5e1878 docker.io/nginx "nginx-g ' daemon off" minutes ago up seconds 0.0.0.0:8080->80/tcp Angry_mccarthy
6.
and start a similar service.
[[Email protected] my. shells]# Docker run-p 8081:80-d Docker.io/nginx//Start a service again, Port is 80811fd8a0b5d138203150f1cdbfb9690235159159881785a4654abb04c7c96c5b18[[email protected] my. shells]# Docker PS//There will be two processes, one 8080, one 8081CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS names1fd8a0b5d138 docker.io/nginx "nginx-g ' daemon Off" 4 seconds ago up 3 seconds 0.0.0.0:8081 ->80/tcp suspicious_hypatiac0462d5e1878 docker.io/nginx "nginx-g ' daemon Off" about an hour Ago up 4 minutes 0.0.0.0:8080->80/tcp angry_mccarthy
Access to the newly opened 8081 service
Note: The newly started service and the original service are two containers, the original hello.html is not in the new service
7.
Uninstall Service
[[email protected] My. shells]# Docker PS//at this time 8080 and 8081 are running container ID IMAGE COMMAND CREATED STATUS PORTS names1fd8a0b5d138 docker.io/nginx "nginx-g ' daemon off" 4 minute S ago up 4 minutes 0.0.0.0:8081->80/tcp suspicious_hypatiac0462d5e1878 Docker.io/nginx "Nginx -G ' daemon off ' about a hour ago up 8 minutes 0.0.0.0:8080->80/tcp angry_mccarthy[[email protected] My. shells]# Docker Stop 1fd8a0b5d138//stop 80811fd8a0b5d138[[email protected] my. shells]# Docker PS//left 8080 still running container ID IMAGE COMMAND CREATED statu S PORTS namesc0462d5e1878 docker.io/nginx "nginx-g ' daemon Off" about an hour ago up 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy[[email protected] my. shells]# Docker ps-a//Can see 8080 running, 8081 already Exitedcontainer ID IMAGE COMMAND CREATED STATUS PORTS names1fd8a0b5d138 docker.io/nginx "nginx-g ' daemon off" 5 minutes ago Exited (0) 7 seconds ago suspicious_hypatiac0462d5e1878 docker.io/nginx "nginx-g ' daemon off" about a hour ago U P 9 minutes 0.0.0.0:8080->80/tcp angry_mccarthy[[email protected] my. shells]# [[email protected] My. shells]# Docker RM 1fd8a0b5d138//Remove this process is done, note that the process of operation is not RM, first stop1fd8a0b5d138
Category: 4 ways to enter Linux docker containers
After using Docker to create a container, people are more concerned about how to enter the container, in fact, there are several ways to enter the Docker container, here we talk about several common ways to enter the Docker container.
There are several common ways to enter Docker containers:
- Using Docker attach
- Using SSH
- Using Nsenter
- Using the Exec
First, use Docker attach to enter the Docker container
Docker provides the attach command to enter the Docker container.
Next we create a Daemon Docker container and then use the Docker Attach command to enter the container.
- $ sudo docker run-itd Ubuntu:14.04/bin/bash
We then use Docker PS to view the container information and then use Docker attach to enter the container
- $ sudo docker attach 44fc0f0582d9
You can see that we have entered the container.
However, there is a problem with using the command. When multiple Windows use the command to enter the container at the same time, all windows are displayed synchronously. If one of the Windows is blocked, other windows can no longer operate.
For this reason, the Docker attach command is not suitable for production environments and can be used when developing your own applications.
Second, use SSH to enter the Docker container
After the use of the Docker attach command to enter the container in the production environment, it is believed that SSH is the first thought of everyone. Install SSH Server in the mirror (or container) so that multiple people can enter
Containers and are not disturbed by each other, I believe you in the current production environment (not using Docker) is also done. However, it is not recommended to use SSH to enter the Docker content after using the Docker container.
Inside the device. Please refer to the following article for the reasons why it is not recommended:
Why don't you need to run sshd in a Docker container
Third, use Nsenter to enter the Docker container
In the case where neither of the above is appropriate, there is a more convenient way to use Nsenter to enter the Docker container. For what is Nsenter please refer to the following article:
Https://github.com/jpetazzo/nsenter
After knowing what Nsenter is, the system defaults to installing the nsenter we need into the host.
If not installed, follow the steps below (note that the host is not a container or mirror)
The specific installation commands are as follows:
- $ wget https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz
- $ TAR-XZVF util-linux-2.24.tar.gz
- $ CD util-linux-2.24/
- $./configure--without-ncurses
- $ make Nsenter
- $ sudo cp nsenter/usr/local/bin
After installing Nsenter, you can check the use of the command.
Nsenter can access the namespace of another process. So in order to connect to a container we also need to get the PID of the first process of the container. You can use the Docker inspect command to get the PID.
The Docker inspect command is used as follows:
- $ sudo docker inspect--help
The inspect command displays information for a mirror or container hierarchically. For example, we currently have a running container
You can use Docker inspect to view the details of the container.
- $ sudo docker inspect 44FC0F0582D9
This information is very much, and only a portion of it is captured here for presentation. If you want to display the first PID of the container, you can use the following method
- $ sudo docker inspect-f {{. State.pid}} 44fc0f0582d9
After we get the PID of the process, we can use the Nsenter command to access the container.
- $ sudo nsenter--target 3326--mount--uts--ipc--net--pid
- $ sudo nsenter--target 3326--mount--uts--ipc--net--pid
3326 of which is the PID of the process just got
Of course, if you think it is too cumbersome to input so many parameters every time, there are many good scripts available on the web for everyone to use.
The address is as follows:
Http://yeasy.gitbooks.io/docker_practice/content/container/enter.html
Http://www.tuicool.com/articles/eYnUBrR
Iv. using Docker exec to enter the Docker container
In addition to the above, Docker provides a new command exec to enter the container after the 1.3.X version, which is relatively simpler, so let's take a look at the use of the command:
- $ sudo docker exec--help
Next we use this command to enter a container that is already running
- $ sudo docker PS
- $ sudo docker exec-it 775c7c9ee1e1/bin/bash
Reference: https://www.cnblogs.com/xhyan/p/6593075.htmlhttps://www.cnblogs.com/wwzyy/p/8337965.html
Docker operation in Centos7