The container is the second core concept of Docker, and the simple one is that the container is a running instance of the mirror, and the difference is that it has an extra writable file layer.
If the virtual machine is simulated running a full set of operating systems (providing a running state environment and other system environments) and running on the application. then Docker containers are one or a set of applications that run independently, and they must run the environment.
New Container
Docker containers are very lightweight, and users can create or delete containers at their own discretion.
A container can be created using Docler create, such as creating a MySQL container:
Docker Create –it mysql:latest
You can then use the PS command to view all the containers, but add the-a parameter, because the container after the create is not turned on by default.
Docker PS –a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f660cfdf9b1 mysql:latest "/entrypoint.sh -na About a minuteago goofy_newton d8990fec2141Ubuntu:latest "/bin/bash" 3 hours ago Exited ( 0) 3hours ago serene_payne
If you want to open this container, you need Docker start
Docker start 5f660cfdf9b1
In the start can add ID, you can also add the name of the container, you can use –name at the time of creation for the container name , if not named, Docker will automatically assign it a.
Create and start a container
There are two ways to start a container, one is to create a container and start the container, and the other is to restart a container that is in the terminating state.
If you are creating a container and starting immediately, you are using the Docker Run command, which is equivalent to Docker create plus Docker start.
When you use Docker run to create a container, the actions include:
- Checks if there is a specified image locally and does not exist to download from the public repository
- Create and start a container with mirroring
- Allocates a file system and hangs on a read-only layer of layer-readable layers outside the mirror
- Bridging a virtual pretext into a container from the bridge interface configured by the host host
- Configure an IP address from the address pool to the container
- Executing user-developed applications
- Container is terminated when execution is complete
For example, using an Ubuntu image to execute a bash terminal allows users to interact:
Docker run –t –i ubuntu:14.04 /bin/bash
root@ce5e7fe50200:/#
Where the-t parameter is to have Docker assign a pseudo terminal and bind to the container's standard input,-I keeps the container's standard input open. (equivalent to having Docker interact with the command line as you would with other systems)
When exit or ctrl+d exits, the container is terminated because the bash program is finished.
More often, the container needs to run in the background in the daemon , which can be done by adding the-D parameter.
Like what:
Docker Run –D Ubuntu:14.04
Terminate Container
You can use Docker stop to terminate a running container in the following format:
Docker Stop [–t| -time[=ten]]
It will first want the container to send the sigterm signal, then wait for a period of time (default 10s), and then send the Sigkill signal to terminate the container.
Of course, you can use the Docker kill command to send a sigkill signal to forcibly terminate the container.
If you want to restart a container, you only need docker restart
Enter the container
Because after using the- D parameter, the container starts to enter the background and the user cannot see the information in the container. There are times when you need to go into a container and there are several ways to get into the container, including Docker attach commands, Docker exec commands, and nsenter tools.
Attach command
Docker attach is a docker-brought command that uses a simple Docker attach followed by an ID or container name, but the attach command is sometimes inconvenient, and when multiple windows are attach to the same container at the same time, all the Windows will be displayed synchronously , when a window is blocked by a command, other windows will not be able to do anything else.
EXEC command
Exec is a tool provided by the Docker 1.3 release that allows you to run commands directly within a container, such as entering a container and launching a bash:
Docker exec –ti [ID| NAME] /bin/bash
Nsenter Tools
Nsenter is a tool that is included in the Util-linux pack 2.23 release, not a Docker's own tool that requires the installation of a Util-linux package, which is a bit cumbersome to use and not intended to attempt.
Delete Container
Use the Docker RM directive to delete the specified container, syntax format:
Docker rm [OPTIONS] CONTAINER[CONTAINER. ]
The supported options are:
- -f,–force=false forcibly terminates and deletes a running container
- -l,–link=false Delete a container link, but keep the container
- -v,–columes=false Deleting a container-mounted data volume
Export Container
The export container is to guide a container that has been created into a file that can be exported regardless of whether the container is in a running state.
Using the Export command, the Docker Export command uses the following format:
Docker export CONTAINER
Like what:
Docker export boring_galileo > dbserver. Tar
These files can be transferred to other machines, and the migration of containers is implemented by importing commands on other machines.
Import Container
The exported file can also be imported using the Docker Import command as a mirror, for example:
Re-import the file you just exported:
cat dbserver. Tar | Docker Import – dbserver:lastest
This effect is consistent with the creation of images based on local template imports.
In fact, you can export a mirrored storage file to a local mirror library through Docker load, or you can use the Docker Import command to import a container snapshot to a local mirror library. The difference between the two is that the container snapshot file will lose all history and metadata information (that is, only the snapshot state of the container at that time), while the image storage file will hold the full record and be larger in size. In addition, metadata information such as labels can be re-specified when importing from a container snapshot file.
Docker Container Management