The previous article explained the principle and common commands of docker image. This article continues to demonstrate the common operations and commands related to docker container through the official Apache image.
In my previous Linux introductory series articles, “Linux entry series 18 — Apache service 1 of Web services”, demonstrated the installation and use of Apache services under centos7. This time, we still use the official Apache image to demonstrate. Through this case, we can compare the differences between the two methods, so as to better understand what docker can do and the benefits of docker.
Next, we will demonstrate how to create, stop, and delete containers.
1、 Introduction to docker container
Another core concept of an instance container is docker.
The docker image is static. Only when a container is created from the docker image and run, the program in the container will run to complete specific functions.
We want to complete the business function of the program is to run in the container. The image itself is a static read-only file, and the container has a layer of writable files required by the runtime, and the application process in the container is in the running state.
Through the demonstration below, we will understand it more deeply.
2、 Common commands of docker container
2.1 Apache image preparation
Our subsequent demonstration is based on the Apache image officially provided on the docker hub.
The image name is httpd. The image does not contain PHP environment and can only run static HTML pages. Therefore, if you want to run a dynamic website of PHP, you need to select a PHP image. This article is only to demonstrate the operation of the container, so static page can be used.
[[email protected] ~]# docker pull httpd:2.4 2.4: Pulling from library/httpd 68ced04f60ab: Pull complete 35d35f1e0dc9: Pull complete 8a918bf0ae55: Pull complete d7b9f2dbc195: Pull complete d56c468bde81: Pull complete Digest: sha256:946c54069130dbf136903fe658fe7d113bd8db8004de31282e20b262a3e106fb Status: Downloaded newer image for httpd:2.4 docker.io/library/httpd:2.4 [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd 2.4 c5a012f9cf45 2 days ago 165MB
After downloading, we can see that the image size of httpd is 165m. What exactly does it contain? At the end of the article, we will analyze it carefully.
2.2 create container and run in the background
After the image download is completed, the container needs to be created through the docker run command.
Docker run [options] image name [command] [parameter]
There are many options, which can be viewed through the help command. The following is a list of the most commonly used options
|–name||Specifies the name of the container|
|-d||The container runs in the background and does not print messages on the console|
|-i||That is — interactive|
|-t||Assign pseudo terminal|
|-p||Specifies the mapping between host and container port. Host port: port in container|
|-P||The host port is mapped to the container port, and the host port is specified randomly|
The – it parameter and – D parameter are not used at the same time, and represent two different ways to start the containerInteractive startup containerandGuardian boot container。
So called interactive startupAfter the container is started, it will enter the container directly and automatically assign a pseudo terminal to execute various commands in the containerGuardian boot containerAfter the container is started, there is no need to enter the container. The container runs in the background and provides services silently.
As for how to choose which way to use, it depends on whether you need to enter the container. If you want to enter, you can choose interactive. If you just want the container to run in the background to provide services, you can choose the guardian mode。
nextThrough the – D parameter, in the form of a daemonCreate a container based on httpd image and run in the background
[[email protected] ~]# docker run -d --name mywebsit -p 8888:80 httpd:2.4 783b46f5cddcc3ea919329a99f83a783da98bce4abce05ccc9b3f27fda859b09 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783b46f5cddc httpd:2.4 "httpd-foreground" 6 seconds ago Up 4 seconds 0.0.0.0:8888->80/tcp mywebsit [[email protected] ~]
Only by doing this step, the web environment based on Apache will be built. At this time, the website based on apahce just built can be accessed through the browser on centos7.
If a firewall is configured, it can also be accessed on the host.
Next, you can modify the data in the website directory in the container and replace it with your own web page. It’s easy to build an Apache Web static website. Isn’t this very convenient? You don’t even need to configure any environments. Migration is also very convenient. For example, if you want to change to another host to run the website, you only need to download the image and run it.
This is the power of docker, and there are many more powerful functions, which will be demonstrated later.
To explain my local environment, we must clarify the relationship between physical machine, virtual machine and container. My local physical machine system is win10. I installed a VMware virtual machine on it, created an instance of centos7 in the virtual machine, and then installed docker in it and ran mywebsite container. Because I don’t have centos7 installed locally, I’ll use a virtual machine for demonstration. Of course, you can also install docker directly on windows or local CentOS.
External access is to access the host port 8888 of centos7 specified to be mapped in the run container, rather than port 80 of mywebsite container.
Special note: the reason why – D is used to run containers in the background as daemons is that when a container is not executed by the foreground process, it will stop immediately after the container is created. So you can create a container interactively, docker run – it — name mywebsit1 – P 8888:80 httpd:2.4 After the container is created, you can’t see the container at all by using the docker PS command. If you use the docker PS – a command to view the container, you will find that after the container is created, it stops immediately after it is started for a short time. This is why.
2.3 view container command
To see which containers are running or have been created locally, you can use the docker PS command
Docker PS [options]
|-a||–All to view all containers. By default, only running containers are displayed|
|-l||–Latest to view the recently created container|
|-n||Displays the last n containers created|
|–no-trunc||Do not truncate display|
(1) View running containers
[[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783b46f5cddc httpd:2.4 "httpd-foreground" 4 hours ago Up 4 hours 0.0.0.0:8888->80/tcp mywebsit [[email protected] ~]#
You can see that only the myweb it container you just created is running.
2.4 viewing container information outside the container
2.4.1 view container log command
Docker logs [options] container name or ID
|-f||Dynamic tracking printing log information, once there is a new log will continue to print|
|-t||Print the time in front of each line of log|
|–tail||Specifies the number of rows to display|
(1) View the log of mywebsite, only 2 lines are displayed
[[email protected] ~]# docker logs -t --tail 2 mywebsit 2020-02-29T03:09:57.654372509Z 192.168.78.1 - - [29/Feb/2020:03:09:57 +0000] "-" 408 - 2020-02-29T03:09:57.854284777Z 192.168.78.1 - - [29/Feb/2020:03:09:57 +0000] "-" 408 - [[email protected] ~]#
2.4.2 view process command in container
(1) View the process inside myweb it
[[email protected] ~]# docker top mywebsit UID PID PPID C STIME TTY TIME CMD root 3673 3655 0 10:29 pts/0 00:00:01 httpd -DFOREGROUND bin 3708 3673 0 10:29 pts/0 00:00:00 httpd -DFOREGROUND ... omit part of the output
This command is similar to the top command under Linux.
2.4.3 check vessel interior details
Docker inspect container name or ID
(1) View the internal information of myweb container
[[email protected] ~]# docker inspect mywebsit ... omit output
Because there is too much output, it will not be pasted.
After executing the command, a JSON string object describing the container information in detail will be obtained. The object contains the container details, including container port mapping, Mount information, volume information, network IP and other information.
2.5 exit container command
If you enter a container, there are two ways to exit:
Press the shortcut key:ctrl+p+q
2.6 enter the container and interact with it
Enter the running container and interact with the command line
2.6.1 docker exec command
Using docker exec command, you can enter the container or execute the command directly without entering the container
Docker exec [options] container name or ID command [parameter…]
Option is similar to the docker run command with – ITD and other parameters.
After entering the container with this command and exiting the container with the exit command or shortcut key, the container will not stop.
(1) Execute the command directly without entering the container
[[email protected] ~]# docker exec -it mywebsit pwd /usr/local/apache2 [[email protected] ~]#
The meaning of this command is to view the current directory in the mywebsite container. We can see that we have not entered the container after the command execution.
Pwd is a Linux command, but why can it be executed here? The reason is that the httpd image is inherited from Debian base image. As we all know, Debian is also a distribution version of Linux, so the container has Debian function. In short, you can think of myweb container as a small Linux system running on docker.
(2) Enter the container to execute the command
[[email protected] ~]# docker exec -it mywebsit /bin/bash [email protected]:/usr/local/apache2# pwd /usr/local/apache2 [email protected]:/usr/local/apache2# exit exit [[email protected] ~]#
The effect of this process is the same as that of the previous case, except that you enter the container, print the current directory, and then exit the container to return to the centos7 host.
Through the comparison of the two cases, it should be clear that their differences.
2.6.2 docker attach command
Docker attach [options] container name or ID
Use this command to re-enter the container. After entering the container, if you exit with the exit command, the container will stop.
2.7 data copy between container and host
Docker CP command can copy data between hosts, even when the container is stopped.
Docker CP container ID: container path host path
(1) Modify the contents of the first page in the container
First of all: the directory of httpd image website is / usr / local / apache2 / htdoc/ index.html
Secondly, because httpd image is based on Debian, there is no VI / VIM tool inside.
Thirdly, docker CP can be used to demonstrate the file copy between host and container.
Therefore, we copy the web page file to the host computer, and then modify it and copy it back to the directory of the container website to achieve the purpose of modifying the home page. (in the following explanation, the website directory can also be mapped to the host through data volume to realize sharing and modification.)
[[email protected] ~]# docker cp mywebsit:/usr/local/apache2/htdocs/index.html / [[email protected] ~]# ll /index.html -rw-r--r-- 1 root root 45 Jun 12 2007 /index.html [[email protected] ~]# echo "my websit is updating">/index.html [[email protected] ~]# cat /index.html my websit is updating [[email protected] ~]# docker cp /index.html mywebsit:/usr/local/apache2/htdocs/index.html [[email protected] ~]#
If you look at the web page again in the browser, you will find that it has been modified.
2.8 stop container command
To stop a container, you can use the stop and kill commands
Kill is to force the container to stop, and stop will stop the container if it is too small. Both of them can specify the waiting time before the container stops.
Docker stop|kill container ID or container name
(1) Stop container
[[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783b46f5cddc httpd:2.4 "httpd-foreground" 6 hours ago Up 21 minutes 0.0.0.0:8888->80/tcp mywebsit [[email protected] ~]# docker stop mywebsit mywebsit [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]#
Container 2.9 start command
You can start or restart the container by starting or restarting
Docker start|restart container ID or container name
(1) Start stopped container
[[email protected] ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783b46f5cddc httpd:2.4 "httpd-foreground" 6 hours ago Exited (0) 2 minutes ago mywebsit [[email protected] ~]# docker start mywebsit mywebsit [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783b46f5cddc httpd:2.4 "httpd-foreground" 6 hours ago Up 1 second 0.0.0.0:8888->80/tcp mywebsit [[email protected] ~]#
You can see that the mywebsite container that was just stopped is in the exited state, and it runs again after restarting.
2.10 delete container command
Docker RM [options] container name or ID
|-f||–When the container is running, it can only be forcibly deleted|
|-v||–Vomumes, deleting data volumes|
(1) Delete running container
[[email protected] ~]# docker rm mywebsit Error response from daemon: You cannot remove a running container 783b46f5cddcc3ea919329a99f83a783da98bce4abce05ccc9b3f27fda859b09. Stop the container before attempting removal or force remove [[email protected] ~]# docker rm -f mywebsit mywebsit [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]#
You can see that the container cannot be deleted when it is running. You need to add the – f parameter to force the deletion.
(2) Force deletion of all containers
Use with caution, for demonstration only. He will delete all containers, including those that are running and those that have stopped.
[[email protected] ~]# docker rm -f $(docker ps -aq) 3d228a470c53 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [[email protected] ~]#
The above deletion statement can also be written as: docker PS – A – Q | xargs docker RM
3、 Inside the container
The above is based on the httpd image to create a container for a variety of container operations demonstration. Next, we study the internal structure and principle of the image through CentOS image
[[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 470671670cac 6 weeks ago 237MB
If you don’t have CentOS locally, download it to the local by using the docker pull command. Next, we use it to create a container named mycentos, and then enter the container to view the structure
[[email protected] ~]# docker run -it --name mycentos centos [[email protected] /]# ls bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [[email protected] /]# pwd / [[email protected] /]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) [[email protected] /]#
After entering the container, we can see that it is actually a CentOS system running, in which various CentOS commands can be executed and the kernel version can be viewed.
If you expand your imagination, it’s easy to think that image is actually to package all kinds of operating system environments and software packages we need to run, then upload them to the warehouse, pull them down when necessary, and create containers by running the run command. This is exactly what we mentioned repeatedly in the first two articles. If you go back to the previous articles now, you will have a deeper understanding.
After the demonstration of container related operation command, the three elements of docker are not good enough for warehouse. The next article will explain the theory and operation of warehouse. Please look forward to it.