[Part 8] - Summary of b2b2c e-commerce technology of Spring Cloud live mall used by Docker container

Docker client

Docker   The client is very simple  , We can enter it directly   Docker   Command to view all command options of Docker client.

xxx@xxx:~# docker

You can use the command   docker command --help   Learn more about the use of the specified Docker command.

For example, we want to view   docker stats   Specific use of instructions:

xxx@xxx:~# 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, 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   Specifies the operating mode of the container.

$ docker run -itd --name ubuntu-test ubuntu /bin/bash

Note: added  - d   The parameter will not enter the container by default. If you want to enter the container, you need to use the instruction   docker exec (described 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

in use  - 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 exiting the container terminal will not cause the container to stop.

attach command

The following demonstrates using the docker attach command.

$ docker attach 1e560fca3906

be careful:   Exiting from this container will cause the container to stop.

exec command

The following demonstrates using the docker exec command.

docker exec -it 243c32535da7 /bin/bash

be careful:   If you exit from this container, the container will not stop, which is why it is recommended   docker exec   The reason for this.

For more parameter descriptions, please use   docker exec --help   Command view.

Export and import containers

Export container

If you want to export a local container, you can use   docker export   Command.

$ docker export 1e560fca3906 > ubuntu.tar

Export the container 1e560fca3906 snapshot to the 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 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 container

Delete container usage   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.

xxx@xxx:~# docker pull training/webapp  # Load image xxx@xxx:~# docker run -d -P training/webapp python app.py

Parameter Description:

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

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

View WEB application container

use   docker ps   To view the containers we are running:

xxx@xxx:~#  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.

PORTS0.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 pass  - p   Parameter to set different ports:

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

docker ps view running containers

xxx@xxx:~#  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

adopt   docker ps   Command to view the port mapping to the container, docker   Another shortcut is also provided   Docker port, using   docker port   You can view the port number of the host mapped to a certain port of the specified (ID or name) container.

The web application container ID we created above is   bf08b7f2cd89   Name is   wizardly_chandrasekhar.

I can use   docker port bf08b7f2cd89   or   docker port wizardly_chandrasekhar   To view the mapping of container ports.

xxx@xxx:~$ docker port bf08b7f2cd895000/tcp -> 0.0.0.0:5000
xxx@xxx:~$ docker port wizardly_chandrasekhar5000/tcp -> 0.0.0.0:5000

View WEB application log

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

xxx@xxx:~$ docker logs -f bf08b7f2cd89 * Running on (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:   Give Way   docker logs   Like use   tail -f   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

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

xxx@xxx:~$ 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 it WEB Application container
xxx@xxx:~$ docker stop wizardly_chandrasekhar   
wizardly_chandrasekhar
 restart WEB Application container
 For containers that have been stopped, we can use the command docker start To start.

xxx@xxx:~$ docker start wizardly_chandrasekhar
wizardly_chandrasekhar

docker ps -l   Query last created container:

#  docker ps -l CONTAINER ID        IMAGE                             PORTS                     NAMES
bf08b7f2cd89        training/webapp     ...        0.0.0.0:5000->5000/tcp    wizardly_chandrasekhar

Running container, we can use   docker restart   Command to restart.

Remove WEB application container

We can use   docker rm   Command to delete unnecessary containers

xxx@xxx:~$ docker rm wizardly_chandrasekhar  
wizardly_chandrasekhar

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

xxx@xxx:~$ docker rm wizardly_chandrasekharError response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove

Keywords: Docker Spring Cloud

Added by Nuser on Sat, 11 Sep 2021 06:09:57 +0300