Docker container usage



Docker client

The docker client is very simple. We can directly enter the docker command to view all command options of the Docker client.

php@php:~# docker

docker27.png

You can use the command docker command --help to learn more about how to use the specified Docker command.

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

php@php:~# docker stats --help

docker28.png


Run a web application

The container we ran earlier has no special use.

Next let’s try building a web application using docker.

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

php@php:~# docker run -d -P training/webapp python app.py

docker29.png

Parameter description:

  • -d:Let the container run in the background.

  • -P:Map the network port used inside the container to the host we use.


View WEB application container

Use docker ps to view our running container

php@php:~$ docker ps

docker30.png

There is more port information here.

PORTS
0.0.0.0:32769->5000/tcp

Docker opens port 5000 (the default Python Flask port) mapped to host port 32769.

At this time we can access the WEB application through the browser

docker31.png

We can also specify the -p flag to bind the specified port.

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

docker psView the running container

docker32.png

The 5000 port inside the container is mapped to the 5000 port of our local host.


Shortcut to network port

You can view the port mapping of the container through the docker ps command. Docker also provides another shortcut: docker port. Use docker port to view the specified (ID or name) A certain port of the container is mapped to the port number of the host.

The web application container ID we created above is: 7a38a1ad55c6 and the name is: determined_swanson

I can use docker port 7a38a1ad55c6 or docker port determined_swanson to view the mapping of the container port

php@php:~$ docker port 7a38a1ad55c6
5000/tcp -> 0.0.0.0:5000
php@php:~$ docker port determined_swanson
5000/tcp -> 0.0.0.0:5000

View WEB application log

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

php@php:~$ docker logs -f 7a38a1ad55c6
 * 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 dokcer logs output the standard output inside the container just like using tail -f.

From the above, we can see that the application uses port 5000 and can view the application's access log.


View the processes of the WEB application container

We can also use docker top to view the processes running inside the container

php@php:~$ docker top determined_swanson

docker33.png


Inspect WEB applications

Use docker inspect to view the underlying information of Docker. It returns a JSON file recording the configuration and status information of the Docker container.

php@php:~$ docker inspect determined_swanson
[
    {
        "Id": "7a38a1ad55c6914b360b565819604733db751d86afd2575236a70a2519527361",
        "Created": "2016-05-09T16:20:45.427996598Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
......

Stop the WEB application container

php@php:~$ docker stop determined_swanson   
determined_swanson

Restart the WEB application container

We can use the command docker start to start the stopped container.

php@php:~$ docker start determined_swanson
determined_swanson

docker ps -l to view the running containers

docker34.png

Running containers, we can use the docker restart command to restart


Remove WEB application container

We can use the docker rm command to delete unnecessary containers

php@php:~$ docker rm determined_swanson  
determined_swanson

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

php@php:~$ docker rm determined_swanson
Error response from daemon: You cannot remove a running container 7a38a1ad55c6914b360b565819604733db751d86afd2575236a70a2519527361. Stop the container before attempting removal or use -f