Docker client
Recommended e-commerce source code
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 have a deeper understanding of the usage of the specified Docker command through the command "docker command --help".
For example, we want to check the specific usage of the {docker stats} instruction:
runoob@runoob:~# docker stats --help
Container use
Get image
If we do not have a ubuntu image locally, we can use the docker pull command to load the ubuntu image:
$ docker pull ubuntu
Start container
The following command uses ubuntu image to start a container. The parameter is 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/bash: 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 container that has stopped running
View all container commands as follows:
$ docker ps -a
Click the picture to view the larger picture:
Start a stopped container using docker start:
$ docker start b750bbbcfd88
Background operation
In most scenarios, we want the docker service to run in the background. We can -d# specify the running mode of the container.
$ docker run -itd --name ubuntu-test ubuntu /bin/bash
Click the picture to view the larger picture:
Note: adding the - d , parameter will not enter the container by default. If you want to enter the container, you need to use the instruction , docker exec (which will be introduced below).
Stop a container
The command to stop the container is as follows:
$ docker stop <container ID>
Stopped containers can be restarted through docker restart:
$ docker restart <container ID>
Enter container
When using the - d # parameter, the container will enter the background after starting. If you want to enter the container at this time, you can enter it through the following instructions:
-
docker attach
-
Docker exec: it is recommended that you use the docker exec command, because this exit from the container terminal will not cause the container to stop.
attach command
The following demonstrates using the docker attach command.
$ docker attach 1e560fca3906
Note: if you exit from this container, it will cause the container to stop.
exec command
The following demonstrates using the docker exec command.
docker exec -it 243c32535da7 /bin/bash
Note: if you exit from this container, the container will not stop, which is why you are recommended to use "docker exec".
For more parameter descriptions, please use the "docker exec --help" command to view.
Export and import containers
Export container
If you want to export a local container, you can use the "docker export" command.
$ docker export 1e560fca3906 > ubuntu.tar
Export container 1e560fca3906 snapshot to local file Ubuntu tar.
This exports the container snapshot to a local file.
Import container snapshot
You can use docker import to import from the container snapshot file as an image. The following example will the snapshot file Ubuntu Import 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 container
To delete a container, use the {docker rm} command:
$ docker rm -f 1e560fca3906
The following command can clean up all containers in the terminated state.
$ docker container prune
Run a web application
The container we ran earlier is of no particular 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.
runoob@runoob:~# docker pull training/webapp # Load image runoob@runoob:~# docker run -d -P training/webapp python app.py
Parameter Description:
-
-d: Let the container run in the background.
-
-P: Randomly map the network port used inside the container to the host we use.
View 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
There is more port information here.
PORTS 0.0.0.0:32769->5000/tcp
Docker opens 5000 ports (the default Python Flask port) to map to host port 32769.
At this time, we can access the WEB application through the browser
We can also set different ports through the - p # parameter:
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
docker ps view 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 5000 port inside the container is mapped to the 5000 port of our local host.
Shortcuts to network ports
You can view the port mapping to the container through the # docker ps # command. Docker # also provides another shortcut # docker port. You can use # docker port # to view the port number of the specified (ID or name) container mapped to the host host.
The web application container ID we created above is bf08b7f2cd89 and the name is bf08b7f2cd89 wizard_ chandrasekhar.
I can use # docker port bf08b7f2cd89# or # docker port Wizard_ Chandrasekhar to view the mapping of container ports.
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 log
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: Let , docker logs , use , tail -f , to output the standard output inside the container.
From the above, we can see that the application uses port 5000 and can view the access log of the application.
View the process of 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
Check WEB application
Use # docker inspect # to view the underlying information of Docker. It will return 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 WEB application container
runoob@runoob:~$ docker stop wizardly_chandrasekhar wizardly_chandrasekhar
Restart WEB application container
For containers that have been stopped, we can start them with the command docker start.
runoob@runoob:~$ docker start wizardly_chandrasekhar wizardly_chandrasekhar
docker ps -l} queries the last created container:
# docker ps -l CONTAINER ID IMAGE PORTS NAMES bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp wizardly_chandrasekhar
For the running container, we can restart it with the "docker restart" command.
Remove WEB application container
We can use the , docker rm , command to delete unnecessary containers
runoob@runoob:~$ docker rm wizardly_chandrasekhar wizardly_chandrasekhar
When deleting a container, the container must be stopped, otherwise the following error will be reported
runoob@runoob:~$ docker rm wizardly_chandrasekhar Error response from daemon: You cannot remove a running container bf08b7f2cd897b596 4943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove