Notes for using Docker for the first time and notes for using docker for the first time
I. Preface
Docker containers have been released for a long time, but as a programmer, it is a sin to come into contact with them now ......
Before that, I have not had a deep understanding of Docker, and I still have a deep understanding of it: this is a new type of virtual machine. This level. Today I will record my first experience. One point: most of the following content is subjective and one-sided, and is also from Baidu (ah, Baidu ). If something is wrong or something is wrong, please let me know. Thank you first.
II. Environment
My basic runtime environment is the CentOS7-Minimal version that runs on the Win10 pro HyperV virtual machine. The newly installed Virtual Machine ensures that the running environment is not affected by strange things.
CentOS Docker installation is concise and clear: sudo yum install docker-y. Just wait ...... The installation content is as follows:
4. Start the Docker Service
As concise and clear as installation: sudo systemctl start docker. If no error is reported, the startup is complete. (I like to use systemctl. As long as the service can be started, you can start it whenever you want .) If the command does not display back, I cannot.
5. Obtain container Images
Docker provides the image search command (search) to help you quickly find the image you want. Of course, you can configure the image source by yourself. The default value is docker. io. I only want to find a CentOS image, so the default source is enough. The search command is as follows:
As you can see, you can find many images. I do not understand the literal meaning of the last two columns, but it should mark the official image and the image uploaded by the developer. I chose the first one, marked as "official", and should have not installed anything else.
Well, the next step is to download the (pull) image. The command is as follows (slag network speed ):
We can see that the latest (latest) is downloaded by default ). Of course, you can download other non-Latest versions. For more information, see pull command parameters. I only need the latest (proud face ). The image size is also 6. So small, it is estimated that nothing has been installed (or compressed ?)......
The above is the information displayed after the download is complete.
6. Create a container
The next step is to create a container from the downloaded image. All the content of the new container is copied from the image ).
Declaration: the input of sudo is too troublesome, so the following demonstration is executed by the root user.
The create container (run) command is complicated with many parameters:
As mentioned above, I will explain one by one:
-Itd: This is short for-I-t-d. -I indicates to keep the standard input stream (stdin) Open, regardless of whether it is connected to this container. -T assigns a virtual tty to the container. -D indicates that the container runs in background mode, that is, the container will be disconnected from the current terminal after it is started. (I will demonstrate how to connect back later ).
-- Name: This is a name for the container, which is used for interaction with the container. Of course, you can use the container Id (that is, the giant echo after the run Command ). Of course, the default name will also appear if you do not write it.
-V: Mount the host. (Is this correct ?) Directory to the container directory (host directory path: Container directory path ). Of course, you can set the read/write attribute. If you do not need to do this, you can explore it on your own.
-P: Ing (port forwarding ?) Host port to container port (host port: container port ). This parameter can be repeated and mapped to multiple ports. This parameter also has many situations, so please explore it on your own.
Centos: This is the name of the local image (centos) and must be the image that has been downloaded (pull) to the local. Of course, if not, Docker will execute search and ask if you want to download it.
/Bin/bash: This is the task to be executed for the started image. There are many doorways. I will talk about them later.
This is omitted.-Parameters. -The a parameter can specify the connection quasi-input stream (STDIN), standard output stream (STDOUT), and standard error stream (STDERR). By default, it seems that they are all connected. It can be used explicitly to connect to the specified stream.
The above is the command-related explanation, which I understood with Baidu. If there is something wrong, please leave a message.
7. View containers
Run the docker ps-a command to view all existing containers:
As shown above, I only run one container. The command displays the container ID, used image, running command, creation time, running time, port ing, and name.
8. connect to a container
There are multiple ways to connect to a container. Use the Docker attach command on the host to connect to the container:
As shown above, it is connected to the container. You can see the changes in command line information.
I used the ps command to view the processes in the container. You can see a process with ID 1, which is the last parameter of the run Command. The task process Id specified by the Run command must be 1. We use the attach command to connect to the standard input and output stream of this command (the error stream I don't know if the connection is complete ). Because the command specified by run is bash, attach is connected to bash, so that the container can be operated at will.
To close the connection, you can easily exit bash: exit.
HoweverThis will cause a problem. At that time, the container stops because of the exit of process 1 (bash connected by the attach command.
As shown above, the container has been stopped (Exited ).
Therefore, we recommend that you use the exec command to operate the container. Restart the container:
As you can see, start can be started with the container name.
Using the exec command, the container can directly execute the command (docker exec container name command ):
As shown above, I used exec to run the ps command on nethost to view the process, and executed the ls command to list the root directory.
You can also execute bash with exec and mount the input and output streams:
As shown above, I used the exec command to start bash and mount the input and output streams. -The significance of the it parameter is the same as that of the run Command. The difference is that the-d command is not used, so that (in foreground mode) The command can be directly connected to the input and output streams after execution. I executed the ls and ps commands.
From the display of the ps command, we can see that the exec command starts a new bash with a PID of 24 (of course, the PID should be assigned a random value ). Unlike bash with PID 1, we are currently connected to bash with PID 24, which can execute commands and perform arbitrary operations. After exiting, the bash with PID 1 is not affected, so that the container can continue to run.
It can be seen that the container is still running after I exit the bash of the container. After executing the ps command using exec on the host machine, the process 1 in the nethost container is still running.
9. Stop and delete a container
As mentioned above, the start command is stopped.
The execution result of the stop command is as follows.
It is easier to delete a container. Run the rm command:
As shown above, I deleted the container named nethost, because there is only one container, so after the deletion, nine no containers are available. Note that the rm command can only delete stopped containers. In addition, unlike the linux rm command, the rm command of docker will delete the container directly, so be careful when using it.
10. Others
I have not tried other content, such as backup, migration, and image uploading.