Docker containers are used


Docker client

The docker client is very simple, and we can enter the docker command directly to see all the command options for the Docker client.

w3cschool@w3cschool:~# docker

Docker containers are used

You can learn more about the specified Docker command usage method by commanding docker command--help.

For example, we want to see how the docker stats instruction is used:

w3cschool@w3cschool:~# docker stats --help

Docker containers are used


Run a web application

The container we ran in front of us didn't have any special use.

Let's try building a web application using docker.

We'll run a Python Flask app in a docker container to run a web application.

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

Docker containers are used

Description of the parameters:

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

  • -P: Map the network ports used inside the container to the hosts we use.


View the WEB app container

Use docker ps to see which container we are running

w3cschool@w3cschool:~$ docker ps

Docker containers are used

There's more port information here.

PORTS
0.0.0.0:32769->5000/tcp

Docker opened 5000 ports (the default Python Flask port) to map to host port 32769.

At this point, we can access the WEB application through a browser

Docker containers are used

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

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

Docker ps view the running container

Docker containers are used

The 5000 ports inside the container are mapped to the 5000 ports of our local host.


A shortcut to the network port

The docker ps command allows you to view the port map to the container, and docker provides another shortcut: docker port, which uses docker port to view the port number of a defined port of the specified (ID or name) container to the host host.

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

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

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

View the WEB application log

Docker logs (ID or first name) can view the standard output inside the container.

w3cschool@w3cschool:~$ 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 as if using tail -f.

From above, we can see that the application is using 5000 ports and can view the application's access logs.


View the process of the WEB application container

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

w3cschool@w3cschool:~$ docker top determined_swanson

Docker containers are used


Check the WEB application

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

w3cschool@w3cschool:~$ 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

w3cschool@w3cschool:~$ docker stop determined_swanson   
determined_swanson

Restart the WEB application container

Containers that have stopped, we can start using the command docker start.

w3cschool@w3cschool:~$ docker start determined_swanson
determined_swanson

docker ps -l to see the running container

Docker containers are used

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


Remove the WEB app container

We can use the docker rm command to remove unwanted containers

w3cschool@w3cschool:~$ docker rm determined_swanson  
determined_swanson

When you delete a container, the container must be stopped or the following error will be reported

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