Experience notes for the first time using Docker

Source: Internet
Author: User
Tags docker ps

First, preface

Docker containers have been released for a long time, but it is a sin to be a programmer now.
prior to that, I had not had a deep understanding of Docker, and I still knew that it was a new type of virtual machine. Such a level. Take a note of my first experience today. Statement one point: The following is mostly subjective one-sided, and another from Baidu (Ah, Baidu). If there is anything wrong, or understanding the problem of the place, but also please tell me, thank you all first.

Second, the environment
My underlying runtime environment is the Centos7-minimal version that runs in the WIN10 Professional Edition Hyperv virtual machine. The newly installed virtual machine ensures that the operating environment is unaffected by strange things.
The CentOS installation Docker is simple and crisp: sudo yum install docker-y. Just wait ... The installation content is as follows:

Iv. Start Docker Service
as simple as installation: sudo systemctl start Docker. If there is no error, it is the start-up completed. (I like to use Systemctl, anyway as long as can start the service, you want to start on how to start.) The command did not echo, I could not.

V. Get the container image
Docker provides a mirror search command that allows you to quickly find the image you want. Of course, the source of the image you can configure yourself, the default is Docker.io. I just want to find a centos image, so the default source is OK. The search commands are as follows:

As you can see, a lot of mirrors are searched. I don't understand the last two columns, but it should be the official image and the image uploaded by the developer. I chose the first one, marked "official", should not have anything else to wear.
Well, the next step is to download (pull) the image. The command is as follows (slag speed):

As you can see, the default download is up-to-date (latest). Of course, you can download other non-up-to-date versions, and check the relevant parameters of the pull command for specific operations. I just want the latest (proud face). This image size is also 6. So small, it is estimated that there is nothing to install it (or compression?) )......

The above is the information displayed after the download is complete.

VI. Create a container
The next step is to create a container from the image you just downloaded. All the contents of the new container will be copied from the mirror (that is, copy it).
Disclaimer: Because the input sudo is too cumbersome, the following demo is executed with the root user.
The Create container (run) command is more complex and has many parameters:

as above, I come to an explanation:
-ITD: This is the shorthand for-i-t-D. -I means to keep the standard input stream open (stdin), regardless of whether it is connected to this container. -T assigns a virtual TTY to the container. -D indicates that the container is running in background mode, that is, the connection to the current terminal will be broken when the container starts. (We'll show you how to connect back later.)
--name: This is a name for the container, and then the interaction with the container needs to be used, of course, the ID of the container is also possible (that is, the long echo after the Run command). Of course, there will be a default name if you don't write.
- v: Indicates Mount host (is this the salutation?). Directory to the container directory (host directory path: container directory path). Of course, you can set the read and write properties, this is not said, you can explore on your own.
- P: Indicates mappings (port forwarding?). ) host port to the container port (host Port: Container port). This parameter can be repeated, mapping multiple ports. This parameter also has a lot of situations, please explore it yourself.
CentOS: This is the local image name (CentOS), which must be a mirror that has been downloaded (pull) to the local. Of course, if not locally, Docker will execute search and ask if you want to download it.
/bin/bash: This is the task that will be performed for this boot image. This doorway is more, I'll talk about it later.
the-a parameter is omitted here. The-a parameter can specify the connection quasi-input stream (STDIN), the standard output stream (STDOUT), and the standard error stream (STDERR), which appears to be connected by default. Can be used explicitly to connect to the specified stream.
the above is the command of the relevant explanation, is my own cooperation with Baidu understand, if there is wrong, please leave a message oh.

Vii. Viewing containers
Use the Docker ps-a command to view all the containers that currently exist:

As above, I only run a container. The command displays the ID of the container, the image used, the command to run, creation time, run time, port mapping, name.

Eight, the connection container
There are several ways to connect containers. Use Docker's attach command in a host to connect to the container:

As above, it is already connected to the container. You can see the change in command line information.
I used the PS command to see 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 (Error stream I do not know the connection is not). Because the command specified by run is bash, attach is also connected to bash so that it can manipulate the container at will.
Close connection It's easy to think of quitting Bash:exit.


But, however, this exit can cause a problem, when the container is stopped because it exits the 1th process (the bash that the attach command connects to) and the task is considered complete.

As above, the container has stopped (Exited).
Therefore, I recommend that you use the EXEC command to manipulate the container. We re-start the container:

As you can see, start plus container name can be started.
With the EXEC command, you can have the container execute commands directly (Docker exec container name command):

As above, I used exec to nethost perform PS command to view the process, the execution ls command lists the root directory.
You can also use Exec to perform bash and mount the input and output streams:

As above, I used the EXEC command to start bash and mount the input/output stream. The meaning of the-it parameter is the same as the Run command, unlike the-D command, which allows the command to be directly connected to the input and output stream after execution (foreground mode). I executed the LS command and the PS command.
As can be seen from the display of the PS command, the EXEC command launches a new bash,pid of 24 (of course, the PID should be randomly assigned). Unlike bash, which has a PID of 1, we are currently connected to the 24 bash, which can execute commands and operate at will. And after exiting, it does not affect the 1 PID of Bash, so that the container can continue to run.

Visible, after I quit the container bash, the container is still running. After I use EXEC to execute the PS command on the host, the Nethost 1th process is still running in this container.

Ix. Stopping and deleting containers
The boot container (start) command is mentioned above, and the Stop container is of course the Stop command (stop).

The Stop command performs the effect as above.
Deleting a container is easier, using the RM command:

As above, I deleted the container named Nethost, because there is only one container, so there is no container for nine after deletion. Note that the RM command can only delete containers that have stopped. and the RM command does not have a delete acknowledgement like the Linux RM command, and the Docker RM command deletes the container directly, so use caution.

X. Other
Other content, such as: Backup, migration, upload image, etc., I have not tried, temporarily do not record.

Experience notes for the first time using Docker

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.