Summary of experience notes for the first time using Docker _docker

Source: Internet
Author: User
Tags stdin docker ps

First, the preface

The Docker container has been released for a long time, but as a programmer, it is a sin to begin to contact now ...

Before that, I had not had a thorough understanding of Docker, and the knowledge of it remained: it was a new type of virtual machine. Such a level. I'm going to record my first experience today. Statement point: The following content is mostly subjective one-sided, and another from Baidu (well, that is, Baidu). If there is anything wrong, or understand the problem of the place, please tell me, thank you first.

Second, the environment

My underlying operating environment is the Centos7-minimal version that runs in the WIN10 professional hyperv virtual machine. New installed virtual machines to ensure that the operating environment is not affected by strange things.

CentOS installation Docker simple and crisp: sudo yum install docker-y. Just wait for it. The installation contents are as follows:

Third, start Docker service

As simple and crisp as installation: sudo systemctl start Docker. If there is no error, it is the start of the completion. I like to use Systemctl, anyway, as long as can start service, you want to start on how to start. command does not echo, I don't have screenshots.

Four , getting container mirroring

Docker provides mirror search commands to help you quickly find the mirror you want. Of course, the source of the mirror you can configure yourself, default is Docker.io. I'm just looking for a centos mirror, so the default source is OK. Search commands are as follows:

As you can see, search for many mirrors. I don't understand the last two columns, but it should be the image of the official image and the developer uploading. I chose the first one, marked "official," and should not have anything else.

Well, then there's the download (pull) mirror. Command as follows (slag speed):

As you can see, the default download is the latest (latest). Of course, you can download other not the latest version, the specific operation please query the pull command parameters. I just want the latest (proud face). This mirror size is also 6. This is so small, it is really not a thing to install it (or compression?) )......

The above is the information that is displayed when the download is complete.

V. 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 performed with the root user.

Creating a container (run) command is more complex and has more parameters:

As above, I come to an explanation:

    • -ITD: This is the shorthand for-i-t-D. -I indicates that the standard input stream (stdin) remains open regardless of whether it is connected to this container. -T assigns a virtual TTY to the container. -D indicates that the background mode runs the container, that is, the container will disconnect from the current terminal after it starts. (Then we'll show you how to connect back).
    • --name: This is a name for the container, 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 it.
    • -V: Represents a Mount host (is this the right name?). Directory to the container's directory (host directory path: container directory path). Of course you can set read and write properties, this is not said, you can explore.
    • -P: Represents mappings (port forwarding?) Host port to container port (host Port: Container port). This parameter can occur repeatedly, mapping multiple ports. This parameter also has a lot of situation, also please explore yourself.
    • CentOS: This is the local mirror name (CentOS) and must be a local mirror that has been downloaded (pull). Of course, if not locally, Docker will execute search and ask if you want to download it.
    • /bin/bash: This is the task that the mirroring of this launch will perform. This doorway is quite a lot, I'll talk about it later.
    • The-a parameter is omitted here. The-a parameter can specify connection quasi-input stream (STDIN), standard output stream (STDOUT), and standard error stream (STDERR), which appear to be connected by default. Can be explicitly used to connect to the specified stream.

The above is the relevant interpretation of the order, is my own with Baidu understanding, if there is wrong, please leave a message oh.

Six, view the container

Use the Docker ps-a command to view all currently existing containers:

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

Seven, the connection container

There are several ways to connect a container. Use the Docker attach command in the host to connect to the container:

As above, it is already connected to the container. You can see 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 also use the Attach command to connect the standard input output stream of this command (Error stream I do not know the connection is not). Because run specifies that the command is bash, attach is also connected to bash so that the container can be manipulated at will.

Closing a connection is easy to think of, is to exit Bash:exit.

But, however, this exit can cause a problem when the container stops because the 1th process exit (the Attach command Connection bash) will assume that the task is complete.

As above, the container has been stopped (exited).

Therefore, I recommend using the EXEC command to manipulate the container. We reboot the container:

As you can see, start and container names can be started.

Using the EXEC command, you can have the container execute the command directly (Docker exec container name command):

As above, I used exec to nethost the PS command to view the process and execute the LS command to list the root directory.

You can also execute bash with exec and mount the input output stream:

As above, I started bash with the EXEC command and mounted the input output stream. The-it parameter has the same meaning as the Run command, but the-D command is not used, so (foreground mode) allows the command to connect directly to the input and output stream after it has been executed. I executed the LS command and the PS command.

As can be seen from the display of the PS command, the EXEC command starts a new bash,pid of 24 (of course, the PID should be randomly assigned). Different from the PID 1 bash, we are currently connected to this PID 24 bash, the same can execute commands, arbitrary operation. And after exiting, it will not affect the PID 1 bash, so that the container can continue to run.

As shown above, the container is still running after I quit bash in the container. After I use EXEC to perform the PS command in the host, the 1th process in the Nethost container is still running.

Viii. Stopping and deleting containers

The Start container (start) command is mentioned above, and stopping the container is, of course, stopping the command (stop).

The Stop command performs the same effect as above.

It is easier to delete a container by using the RM command:

As above, I removed the container named Nethost because there was only one container, so there was no container after the deletion. Note that the RM command can only delete containers that have been stopped. and the RM command does not have a delete confirmation like the Linux RM command, the Docker RM command deletes the container directly, so be careful when you use it.

Ten, other

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

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.