Docker Tutorial Series 05 - Common Commands for Containers

In this section, we'll discuss common commands for Docker containers.

Create and start a new container [important]

Use the following command to create and start a container. This command is the most common command we use, it has many options, below I list some common options.

① -d option: indicates running in the background

② -P option: random port mapping

③ -p option: specifies the port mapping, which has the following four formats.





④ --network option: Specifies the network mode, which has the following optional parameters.

--network=bridge: The default option, indicating connection to the default bridge.

--network=host: The container uses the host's network.

--network=container:NAMEorID: tells Docker to let the newly created container use the network configuration of the existing container.

--network=none: Do not configure the network for this container, user can customize the network configuration.

Example 1.

The terminal will then print the words Hello World, just as if you had executed it directly locally.

Example 2.

This will start an Nginx container. In this example, we have added two parameters to docker run with the following meaning.

Visit http://Dockerhost computerIP:91/ and you will see the screen as shown in Figure 12-3.

Figure 12-3 Nginx home page


Note that when creating a container using the docker run command, it will first check if the specified image exists locally. If an image by that name does not exist locally, Docker automatically downloads the image from Docker Hub and starts a Docker container.

Listing of packagings [important]

Use the command to list the running containers. After executing this command, you can see a table similar to the one below.

For a list of all containers (including stopped containers), use the -a parameter.

The table contains seven columns with the following meanings.

① CONTAINER_ID: Indicates the container ID.

② IMAGE: indicates the mirror name.

(iii) COMMAND: Indicates the command that is run when the container is started.

④ CREATED: indicates the creation time of the container.

⑤ STATUS: Indicates the status of the container operation. Up means running, Exited means stopped.

(6) PORTS: Indicates the port number of the container to the outside world.

(7) NAMES: Indicates the container name. The name is automatically generated by Docker by default, or you can specify it yourself using the --name option of the docker run command.

Order format.



Stopping the packaging [important]

Use the command to stop the container.

Order format.



where is the container ID, and of course can be used to stop the specified container.

Forced cessation of packagings [important]

You can stop one or more running containers with the command

Order format.


For example.

Starting a stopped container [important]

Use the command to create and start a new container. For stopped containers, you can use the command to start them.

Order format.


For example.

Restarting the packaging [important]

The command can be used to restart the container. The command actually executes the command first, then the command.

Order format.


Access to packagings [important]

There is a scenario where we may need access to a running container.

① Use the command to enter the container.

For example.

In many scenarios, it is not convenient to use the command. When multiple windows are ATTACHED to the same container at the same time, all windows are displayed synchronously. Similarly, if a window blocks, other windows cannot perform the operation.

② Use Enter the container

The nsenter tool is included in util-linux 2.23 or later. In order to connect to the container, we need to find the PID of the first process of the container, which can be obtained with the following command.

Once the PID has been obtained, the container can be accessed using the nsenter command:.

A complete example is given below.

The reader can also simplify the process of getting into the container by wrapping the two commands above into a single Shell.

③ docker exec

Delete packaging [important]

Use the command to delete the specified container.

Command Format


Example 1: Delete the specified container.

This command can only delete stopped containers, if you need to delete running containers, you can use the -f parameter.

Example 2: Delete all containers.

Exporting containers

Export the container as a zip file.

Order format.



Importing containers

Use the command to import content from an archive and create an image.

Order format.



1、Net implementation of the WeChat JSSDK sharing feature
2、LogisticRegression Experiment
3、Javaprivate private class of the default constructor generation process
5、Benchmarking gRPCProtoBuf against HttpJson in Go

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送