Get to know the basic operations of Docker

What is Docker?

Docker is a lightweight virtualized container management engine

Two basic concepts of Docker

  • Mirror : It can be understood as a special file system that packs the operating environment. It contains all the information required for the container to start and run, including running programs and configuration data. The image does not contain any dynamic data, and its content will not change after it is built. For example, an official Ubuntu 14.04 image contains a complete set of the root file system of the Ubuntu 14.04 minimal system.
  • Container : 镜像and 容器relationship, similar to the design of object-oriented programming and 实例, like 镜像是静态的定义, 而容器时镜像运行时的实体can be called a runtime environment with a very lightweight virtual machine. Containers can be created, started, stopped, and deleted. When creating a container, you need to display the specified image for the container. After the image is specified, the container has the operating environment saved in the image. For example, you can specify the image of Ubuntu 14.04 for the container, and then the container will have the running environment of Ubuntu 14.04.

The basic process of using Docker

  • The container needs to be created and used based on an image. Therefore, the use of Docker containers generally has the following three steps:

1. Obtain an image
2. Create and start a container based on the image
3. Enter the container and execute the "program"

  • Step 1: Obtain a mirror
    "Mirror" can be understood as a file system that packs the operating environment. After installing Docker, there is no mirror locally, so you need to get the required mirror first.
  • Step 2: Create and start the container
    After obtaining the required image, you can create and start a container based on the image, and the container has the operating environment included in the image. At the same time, you can also set the container startup command when creating the container, which will be executed when the container starts.
  • Step 3: Enter the container and execute the "program".
    After the container is successfully created and started, the container has the Ubuntu operating environment. We can enter the container and execute any program on the Ubuntu system inside it. The "program" here can be "Linxu command", "shell script", "C++ program" and so on.

Get mirror

By default, using the Docker pull command will pull the image locally from the Docker Hub library on the official website.
Command format

docker pull [OPTIONS] <仓库名>:<标签>
  • docker pull :Keyword of Docker pull image command
  • [OPTIONS]: Command options
  • Warehouse name: The format of warehouse name is generally <user name>/<software name>. For Docker Hub, if the user name is not specified, the default is library, which is the official image
  • Label: The label is an important parameter to distinguish different versions of the mirror. <warehouse name>:<label> will uniquely identify a mirror, and the default is latest.
  • For example, we want to pull an official image of Ubuntu 14.04 from the official Docker Hub warehouse. The statement is as follows:
docker pull ubuntu:14.04

Start the container

The docker run command will create a container based on the specified image and start it. The basic syntax of docker run is as follows:

docker run [OPTIONS] 镜像名 [command] [ARG[
  • docker run: command keywords created and started by Docker
  • OPTIONS: Command options, the most commonly used include -d to run the container in the background and return the container ID, -i to run the container in interactive mode, -t to assign a pseudo input terminal to the container...
  • Image name: Specify in the form of <warehouse name>:<tag>
  • COMMAND: Set the startup command, which is executed after the container is started.
  • ARG: Some other parameters.
    Example: Create a container with the Ubuntu operating environment in the container, and output hello docker
    Insert picture description here
    Note: If there is no image locally, an error will be reported when the container is created, and then the specified image will be downloaded from the official website, and then deployed in the container , Then execute the echo command

Stop container

Stop is relatively simple, run the command directly

docker stop [容器名]

This is an example

Enter the container

There are many ways to enter the container. Only the docker exec and docker attach commands currently provided by docker are mentioned here.

docker attach into the container
docker attach containerId|containName

The following example is: first create a container through docker run, install Ubuntu in it, use docker attach containerId after startup (the containerId here is da7d, and the first four digits can be uniquely identified), and the file directory under Ubuntu can be viewed through ls
Insert picture description here

docker exec enters the container
docker exec [options] containerName|containerId command [args]

For example, docker exec da7d mkdir testthat is, create a test folder in the container, as can be seen in the test folder has been created
Insert picture description here

The difference between docker attach and docker exec

Through the above example, it is not difficult to find that using docker attach can directly obtain a terminal-like window, and then directly enter the shell command, and through docker exec, the command to be executed can only be run in the background. In this way, we can choose to use the docker attach or docker exec command according to our needs.

Delete container

What should I do if more containers are created? delete! The container delete command is the same as the command to delete files in the shell, except that when executing the docker command, you need to add a docker, which is:

docker rm containerName|containerId 

Note: This command 只能删除已经停止过的容器, this point, can be compared to the simple rm in the shell command, which can only delete files, not folders. How to delete a container that is not stopped? Two methods:

  • Cong (sha) Ming (gua) method: first stop the container, and then delete.
  • The most effective method: delete a folder in the shell will use rm -r, what about docker? There must be something similar, so he came: add parameters
docker rm -f containerName|containerId

With this command, you can delete a running container~


Delete all stopped containers

docker rm $(docker ps -a -q)

We know that docker ps -a is to view all container information. And docker ps -a -q only looks at the containerId of all containers. In Linux, putting a command in $() will execute the command and return the execution result of the command. So $( docker ps -a -q) will return the container id of all containers, and docker rm can only kill terminated containers, and if you use docker rm to delete a running container, it will not be deleted. So you can use docker rm $(docker ps -a -q) to delete all containers in the terminated state.


Thinking, how to delete all the containers?

docker rm -f $(docker ps -a -q)




This article is written based on a training platform, invaded and deleted.
Training platform address Educoder

Guess you like

Origin blog.csdn.net/weixin_43716048/article/details/111004941