Docker Basics Tutorial--containers

Source: Internet
Author: User
Tags terminates docker ps docker run

Source: http://blog.csdn.net/anxpp/article/details/52476339

Containers are another core concept of Docker.

In general, a running instance of a mirror is a container, and the container provides a writable file layer.

This article describes the same structure as the mirror, and introduces the container gradually. 1. Create a container

Docker containers are very lightweight, easy to create and delete. 1.1. New Container

To create a new container using the Docker create command:

Docker create-it Ubuntu:latest
At this point, the created container is in a stopped state, and Docker pa-a can be used to view the list of containers.

Stopped containers can be started using the Docker Start command. 1.2. Start the container

There are two ways to start a container:

1. Create a new container based on the mirror and start

2, start the container of the state of termination

The main command involved is Docker run, which is equivalent to performing Docker create first and then executing the Docker start command.

Like what:

Docker run Ubuntu/bin/bash ' Hello word '
The container is started and the output Hello Word is then automatically terminated.

When you create and start a container with Docker run, the standard operations that Docker runs in the background include:

Checks whether the specified image exists locally, does not exist, creates and launches a container from the repository to allocate a file system, and mounts a layer of read-write layers from the Network Bridge interface configured by the host host to bridge a virtual interface channel container out of the mirrored layer. Assigning an IP from the address pool to the container execution the user-specified application finishes after the container is terminated to start a bash terminal with user interaction:

Docker Run-t-I Ubuntu/bin/bash
-T: Let Docker assign a pseudo terminal and bind to the standard input of the container.

-I: Keep the container's standard input open.

In interactive mode, users can enter commands through the interrupts they create, which is the same as normal Linux interrupts.

Enter exit or press Ctrl+d to exit the container.

For a bash container that you create, the Docker container automatically terminates when you exit with the Exit command, because there is no need for the container to continue running when there are no running apps in Docker.

1.3 . The daemon operation allows the Docker container to run in the Daemon (daemonized) in the background with the-D parameter.

Like what:

Docker run-d ubuntu/bin/bash-c "while true;do each Hello word; Sleep 1 Done "
Use the Docker PS command to view container information.

Use Docker logs to get the output information of the container.

2. Terminate the container

Terminating a running command is Docker stop, in the format:

Docker stop [-t|--time[=10]]
This will first send a sigterm signal to the container, wait for a period of time (by default, 10s), send the Signkill signal to terminate the container again.

Similarly, as I said earlier, when the application specified in the Docker container terminates, the container page is automatically terminated.

Can be viewed through the Docker PS command:

Docker Ps-a-Q
The parameter here indicates that the container for all exits is viewed.

After the container (IS) terminated, it can be restarted via Docker start.

and the Docker Restart command, page as we expected, would first terminate a running state container and then restart it.

3, enter the container to start the container when the plus parameter-D will enter the background, can not directly see the contents of the container.

There are several ways to get into a container: Docker attach commands, Docker exec commands, and nsenter tools.

3.1. Attach command

This is the command that comes with Docker.

If we have the following containers running:

Docker Run-idt Ubuntu
This can be entered:
Docker Attach container Name
However, when multiple windows (terminals) are attach to a container at the same time, all windows are displayed synchronously, and when a window is blocked because of a command, other windows are blocked and cannot perform the operation.

3.2. The EXEC command exec is the tool provided by the 1.3 version of Docker and can run commands directly in the container.

Or just the example of that running container, if you want to enter and start a bash:

Docker Exec-ti Container Id/bin/bash

3.3, Nsenter tool Nsenter tool is included in the Util-linux 2.23 version and later, if the corresponding package version of the system is low, you can install the new version as follows:

。。。。。。

4. Remove container Delete container using the Docker RM command in the format:

Docker RM [OPTIONS] CONTAINER [CONTAINER ...]
Supported options:

-F 、--Force=false: Because a running container cannot be deleted by default, this parameter is to forcibly terminate and delete a running container. -L 、--link=false: Removes the container's connection, but retains the container.     -V 、--volumes=false: Deletes the data volume that the container is hanging on. For example, remove the container for the terminating state:

Docker RM Container ID
To delete a running container:

Docker rm-f Container ID

5, Import and export containers as with mirrors, containers can also be exported as a file and imported from a file container.

5.1. Export container to file export container is an instance that is created as a container from a mirror and exported to a file regardless of whether the container is running at this time.

Command for Dicker export, format:

Docker Export CONTAINER
An example of an export:

Docker export Container ID > file name after export. tar
You can then send the file to another location to implement the container migration.

5.2. Import the container from the file for the container you just exported, use the Docker Import command for importing, and then become the mirror :

Cat file name. tar | Docker Import-image name: TAG
Like what:

Cat Test.tar | Docker import-test/ubuntu:v0.0.1
The import of a mirror uses Docker load, and here's the difference:

The container snapshot file loses all history and metadata information (only the snapshot state of the container is saved), so metadata information such as labels is typically re-specified when importing from a container snapshot. The image file holds the full record, so the volume page is larger.


The summary container is a component that provides application services directly, is very lightweight, and is the basis for Docker's fast start-stop and efficient service performance.

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.