Docker container usage
A Docker container is a lightweight, portable, self-contained software environment for running applications.
A Docker container packages an application and all its dependencies (including libraries, configuration files, system tools, etc.) into a standardized package, so the application can run consistently anywhere.
Containers are operating-system-level virtualization; they do not need to run a full operating system, making startup and operation more efficient.
The relationship between images and containers
- Image: The static template of a container, containing all dependencies and files needed to run the application. An image is immutable.
- Container: A running instance of an image, with its own filesystem, processes, network, etc., and is dynamic. A container starts from an image and remains mutable at runtime.

Docker client
The Docker client is a command-line tool that interacts with the Docker daemon (Docker Daemon).
The docker client is very simple; we can directly enter the docker command to view all command options of the Docker client.
runoob@runoob:~# docker

You can use the command docker command --help to gain a deeper understanding of how to use a specific Docker command.
For example, if we want to view docker stats the specific usage of a command:
runoob@runoob:~# docker stats --help

The following are commonly used Docker client commands:
| Command | Function | Example |
|---|---|---|
docker run |
Start a new container and run a command | docker run -d ubuntu |
docker ps |
List currently running containers | docker ps |
docker ps -a |
List all containers (including stopped ones) | docker ps -a |
docker build |
Build an image using a Dockerfile | docker build -t my-image . |
docker images |
List all locally stored images | docker images |
docker pull |
Pull an image from a Docker repository | docker pull ubuntu |
docker push |
Push an image to a Docker repository | docker push my-image |
docker exec |
Execute a command in a running container | docker exec -it container_name bash |
docker stop |
Stop one or more containers | docker stop container_name |
docker start |
Start a stopped container | docker start container_name |
docker restart |
Restart a container | docker restart container_name |
docker rm |
Remove one or more containers | docker rm container_name |
docker rmi |
Remove one or more images | docker rmi my-image |
docker logs |
View a container's logs | docker logs container_name |
docker inspect |
Get detailed information about a container or image | docker inspect container_name |
docker exec -it |
Enter a container's interactive terminal | docker exec -it container_name /bin/bash |
docker network ls |
List all Docker networks | docker network ls |
docker volume ls |
List all Docker volumes | docker volume ls |
docker-compose up |
Start a multi-container application (from docker-compose.yml file) |
docker-compose up |
docker-compose down |
Stop and remove started by docker-compose containers, networks, etc. |
docker-compose down |
docker info |
Display detailed information about the Docker system | docker info |
docker version |
Display version information for the Docker client and daemon | docker version |
docker stats |
Display real-time resource usage of containers | docker stats |
docker login |
Log in to a Docker repository | docker login |
docker logout |
Log out of a Docker repository | docker logout |
Common option descriptions:
-d: Runs the container in the background, for exampledocker run -d ubuntu。-it: Runs the container with an interactive terminal, for exampledocker exec -it container_name bash。-t: Specifies a tag for the image, for exampledocker build -t my-image .。
Using containers
Get an 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 a container
The following command starts a container using the ubuntu image, with arguments to enter the container in command-line mode:
$ docker run -it ubuntu /bin/bash

Parameter Description:
- -i: Interactive operation.
- -t: Terminal.
- ubuntu: The ubuntu image.
- /bin/bash: What follows the image name is the command. Here we want an interactive Shell, so /bin/bash is used.
To exit the terminal, simply 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 a larger version:
Use docker start to start a stopped container:
$ docker start b750bbbcfd88

Run in the background
In most scenarios, we want the docker service to run in the background; we can specify the container's run mode with -d.
$ docker run -itd --name ubuntu-test ubuntu /bin/bash
Click the image to view a larger version:
Note:With the -d argument, you will not enter the container by default. To enter the container, you need to use the command docker exec(introduced below).
Stop a container
The command to stop a container is as follows:
$ docker stop <容器 ID>

A stopped container can be restarted with docker restart:
$ docker restart <容器 ID>

Enter a container
When using -d When a container is started with the -d argument, it runs in the background; to enter it, you can use the following command:
-
docker attach: Allows you to interact with the container's standard input (stdin), output (stdout), and standard error (stderr).
-
docker exec: The docker exec command is recommended because it exits the container terminal without stopping the container.
attach command
Example using the docker attach command:
$ docker attach 1e560fca3906
Note: Exiting from this container will cause it to stop.
exec command
Example using the docker exec command:
docker exec -it 243c32535da7 /bin/bash
Note: Exiting from this container will not stop it, which is why the use of docker exec。
For more argument details, use the docker exec --help command.
Export and import containers
Export a container
To export a local container, you can use docker export command.
$ docker export 1e560fca3906 > ubuntu.tar
Exports a snapshot of container 1e560fca3906 to the local file ubuntu.tar.

This exports the container snapshot to a local file.
Import a container snapshot
You can use docker import to import a container snapshot file back 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, you can also import by specifying a URL or a directory, for example:
$ docker import http://example.com/exampleimage.tgz example/imagerepo
Delete a container
Remove containers docker rm Command:
$ docker rm -f 1e560fca3906

The following command cleans up all containers in a terminated state.
$ docker container prune
Run a web application
The containers we ran earlier did not have any particular use.
Next, let's try using docker to build a web application.
We will run a Python Flask application in a docker container to run a web application.
runoob@runoob:~# docker pull training/webapp # 载入镜像
runoob@runoob:~# docker run -d -P training/webapp python app.py

Parameter description:
-
-d:Run the container in the background.
-
-P:Randomly maps the network port used inside the container to a port on our host.
View the WEB application container
Use docker ps to view the containers we are running:
runoob@runoob:~# docker ps
CONTAINER ID IMAGE COMMAND ... PORTS
d3d5e39ed9d3 training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp
Here there is additional port information.
PORTS
0.0.0.0:32769->5000/tcp
Docker maps the exposed port 5000 (the default Python Flask port) to host port 32769.
At this point, we can access the WEB application through a browser

We can also use the -p argument to set a different port:
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
docker psView running containers
runoob@runoob:~# 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
The container's internal port 5000 is mapped to port 5000 on our local host.
Shortcut for network ports
Pass docker ps command can view a container's port mappings,docker also provides another shortcut docker port, using docker port to view the host port number to which a specific port of a given container (ID or name) is mapped.
The ID of the web application container we created above is bf08b7f2cd89 and its name is wizardly_chandrasekhar。
We can use docker port bf08b7f2cd89 or docker port wizardly_chandrasekhar to view the container's port mappings.
runoob@runoob:~$ docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
runoob@runoob:~$ docker port wizardly_chandrasekhar
5000/tcp -> 0.0.0.0:5000
View WEB application logs
docker logs [ID or name] can view the standard output inside the container.
runoob@runoob:~$ 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: Makes docker logs Like using tail -f to output the standard output inside the container.
From the above, we can see that the application uses port 5000 and we can view the application's access logs.
View the processes of the WEB application container
We can also use docker top to view the processes running inside the container
runoob@runoob:~$ docker top wizardly_chandrasekhar
UID PID PPID ... TIME CMD
root 23245 23228 ... 00:00:00 python app.py
Inspect the WEB application
Use docker inspect to view Docker's low-level information. It returns a JSON file that records the configuration and status information of the Docker container.
runoob@runoob:~$ 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"
},
......
Stop the WEB application container
runoob@runoob:~$ docker stop wizardly_chandrasekhar
wizardly_chandrasekhar
Restart the WEB application container
For a stopped container, we can use the docker start command to start it.
runoob@runoob:~$ docker start wizardly_chandrasekhar
wizardly_chandrasekhar
docker ps -l queries the most recently created container:
# docker ps -l
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp wizardly_chandrasekhar
For a running container, we can use the docker restart command to restart it.
Remove the WEB application container
We can use the docker rm command to remove unneeded containers
runoob@runoob:~$ docker rm wizardly_chandrasekhar
wizardly_chandrasekhar
When removing a container, it must be in a stopped state; otherwise, the following error will be reported
runoob@runoob:~$ docker rm wizardly_chandrasekhar
Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove




