English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Docker Container Usage

Docker client

The Docker client is very simple, we can directly input the docker command to see all the command options of the Docker client.

w3codebox@w3codebox:~# docker

You can use the command docker command --help To gain a deeper understanding of the usage method of the specified Docker command.

For example, we want to view docker stats The specific usage of the command:

w3codebox@w3codebox:~# docker stats --help

Container usage

Get the image

If we do not have the ubuntu image locally, we can use the docker pull command to load the ubuntu image:

$ docker pull ubuntu

Start container

The following command uses the ubuntu image to start a container with the parameter to enter the container in command line mode:

$ docker run -it ubuntu /bin/bash

Parameter description:

  • -i: interactive operation.

  • -t: terminal.

  • ubuntu: ubuntu image.

  • /bin/bashThe command placed after the image name is the command, here we want an interactive Shell, so we use /bin/bash.

To exit the terminal, directly enter exit:

root@ed09e4490c57:/# exit

Start a stopped container

The command to view all containers is as follows:

$ docker ps -a

Click the image to view the full size:

Use docker start to start a stopped container:

$ docker start b750bbbcfd88

Background running

In most scenarios, we hope that Docker's service runs in the background, so we can pass -The d parameter specifies the container's running mode.

$ docker run -itd --name ubuntu-test ubuntu /bin/bash

Click the image to view the full size:

Note:added -The d parameter does not enter the container by default. To enter the container, you need to use the command docker exec(This will be introduced later).

Stop a container

The command to stop a container is as follows:

$ docker stop <container ID>

Stopped containers can be restarted with docker restart:

$ docker restart <container ID>

Enter the container

When using -d When the parameter is specified, the container will enter the background after starting. At this time, if you want to enter the container, you can use the following command to enter:

  • docker attach

  • docker execIt is recommended to use the docker exec command because it exits the container terminal without stopping the container.

attach command

The following demonstrates the use of the docker attach command.

$ docker attach 1e560fca3906

Note: If you exit this container, it will cause the container to stop.

exec command

The following demonstrates the use of the docker exec command.

docker exec -it 243c32535da7 /bin/bash

Note: If you exit this container, the container will not stop, which is why it is recommended that everyone use docker exec reason.

For more parameter descriptions, please use docker exec --Use the help command to view.

Export and import containers

Export container

If you want to export a local container, you can use docker export Command.

$ docker export 1e560fca3906 > ubuntu.tar

Export container 1e560fca3906 Snapshot to local file ubuntu.tar.

This will export the container snapshot to a local file.

Import container snapshot

You can use docker import to import a container snapshot file as an image. The following example imports the snapshot file ubuntu.tar into the image test./ubuntu:v1:

$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1

In addition, it can also be imported by specifying a URL or a directory, for example:

$ docker import http://example.com/exampleimage.tgz example/imagerepo

Delete container

Usage for deleting a container docker rm Command:

$ docker rm -f 1e560fca3906

The following command can clean up all containers in a terminated state.

$ docker container prune

Run a web application

The container we ran earlier doesn't have any special use.

Next, let's try to build a web application using docker.

We will run a Python Flask application in the docker container to run a web application.

w3codebox@w3codebox:~# docker pull training/webapp # Load the image
w3codebox@w3codebox:~# docker run -d -P training/webapp python app.py

Parameter Description:

  • -d:Run the container in the background.

  • -P:Randomly map the network ports used internally by the container to the host we use.

Check the WEB application container

Use docker ps to view the containers we are running:

w3codebox@w3codebox:~# docker ps
Use docker ps to view the containers we are running:                 
d3d5e39ed9d3        training/CONTAINER ID ... IMAGE ... COMMAND ... ... PORTS32769->5000/tcp

webapp "python app.py" ... 0.0.0.0:

Here is more port information.
0.0.0.0:32769->5000/tcp

PORTS 5Docker has opened 32769 000 port (default Python Flask port) mapped to the host port

.

We can also access the WEB application through the browser at this time -p parameter to set a different port:

w3codebox@w3codebox:~$ docker run -d -p 5000:5000 training/webapp python app.py

docker psView the running containers

w3codebox@w3codebox:~# docker ps
CONTAINER ID        IMAGE                             PORTS                     NAMES
bf08b7f2cd89        training/webapp     ...        0.0.0.0:5000->5000/tcp    wizardly_chandrasekhar
d3d5e39ed9d3        training/webapp     ...        0.0.0.0:32769->5000/tcp xenodochial_hoov

inside the container 5000 port mapped to our local host 5000 port.

the quick way of network port

through docker ps The command can view the port mapping of the containerdocker it also provides another shortcut docker port, using docker port can view the specific port mapping of the host port of the container (ID or name).

Above, the ID of the web application container we created is bf08b7f2cd89  The name is wizardly_chandrasekhar.

I can use docker port bf08b7f2cd89  or docker port wizardly_chandrasekhar to view the port mapping of the container.

w3codebox@w3codebox:~$ docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
w3codebox@w3codebox:~$ docker port wizardly_chandrasekhar
5000/tcp -> 0.0.0.0:5000

View the WEB application log

docker logs [ID or name] can view the standard output inside the container.

w3codebox@w3codebox:~$ docker logs -f bf08b7f2cd89
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.239.1 - - [09/May/2016 16:30:37] "GET / HTTP/1.1" 200 -
192.168.239.1 - - [09/May/2016 16:30:37] "GET /favicon.ico HTTP/1.1" 404 -

-f: lets docker logs like using tail -f to output the standard output inside the container.

From above, we can see that the application uses 5000 端口并且能够查看到应用程序的访问日志。

查看WEB应用程序容器的进程

我们还可以使用 docker top 来查看容器内部运行的进程

w3codebox@w3codebox:~$ docker top wizardly_chandrasekhar
UID     PID         PPID          ...       TIME                CMD
root    23245       23228         ...       00:00:00            python app.py

检查 WEB 应用程序

使用 docker inspect 来查看 Docker 的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。

w3codebox@w3codebox:~$ docker inspect wizardly_chandrasekhar
[
    {
        "Id": "bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85",
        "Created": "2018-09-17T01:41:26.174228707Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 23245,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-09-17T01:41:26.494185806Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
......

停止 WEB 应用容器

w3codebox@w3codebox:~$ docker stop wizardly_chandrasekhar   
wizardly_chandrasekhar

重启WEB应用容器

已经停止的容器,我们可以使用命令 docker start 来启动。

w3codebox@w3codebox:~$ docker start wizardly_chandrasekhar
wizardly_chandrasekhar

docker ps -l 查询最后一次创建的容器:

#  docker ps -l 
CONTAINER ID        IMAGE                             PORTS                     NAMES
bf08b7f2cd89        training/webapp     ...        0.0.0.0:5000->5000/tcp    wizardly_chandrasekhar

For running containers, we can use the docker restart command to restart them.

Remove WEB application container

We can use the docker rm command to delete unnecessary containers

w3codebox@w3codebox:~$ docker rm wizardly_chandrasekhar  
wizardly_chandrasekhar

The container must be in a stopped state when deleting a container, otherwise the following error will be reported

w3codebox@w3codebox:~$ docker rm wizardly_chandrasekhar
Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85Stop the container before attempting removal or force remove