Latest web development tutorials

Docker containers

Docker client

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

w3big@w3big:~# docker

Docker command --help command through a deeper understanding of the specified command Docker use.

For example, we want to see specific instructions for use docker stats:

w3big@w3big:~# docker stats --help


Run a web application

We run the front of the container and not some of what particular use.

Let's try to use docker to build a web application.

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

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

Parameter Description:

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

  • -P: Internal network used by the container port mapping to the host we use.


See WEB application container

Use docker ps to view the vessel we are running

w3big@w3big:~$ docker ps

Here more information about the ports.

PORTS
0.0.0.0:32769->5000/tcp

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

Then we can access the application through a browser WEB

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

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

docker ps See vessel running

Port 5000 is mapped to the interior of the container port 5000 on our local host.


Shortcut network port

By docker ps command to view port mapping container, docker also provides another shortcut: docker port, use docker port can view the specified (ID or name) of a container port mapping to determine the host port number.

The above web application we created for the container ID: 7a38a1ad55c6 named: determined_swanson

Can I use docker port 7a38a1ad55c6 or docker port determined_swanson to view the mapping for the container port

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

See WEB application log

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

w3big@w3big:~$ 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 the same as using the tail -f to standard output inside the container.

From the above, we can see the application using port 5000 to be able to view and access log application.


View the process WEB application container

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

w3big@w3big:~$ docker top determined_swanson


Check the WEB application

Use docker inspect to see the underlying information Docker's. It will return a JSON file records the configuration and status information Docker container.

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

Stop WEB application container

w3big@w3big:~$ docker stop determined_swanson   
determined_swanson

Restart WEB application container

The vessel has stopped, we can use the command docker start to start.

w3big@w3big:~$ docker start determined_swanson
determined_swanson

docker ps -l to see the vessel running

Container is running, we can use docker restart command to restart


Remove WEB application container

We can use docker rm command to delete unnecessary container

w3big@w3big:~$ docker rm determined_swanson  
determined_swanson

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

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