03 - container use

Posted by AIS4U on Tue, 08 Feb 2022 09:07:37 +0100

Get image
If there is no local image, we can use the unpull command:

$ 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
exit Exit is to stop the container and exit
Ctrl+ P + Q Yes, exit does not stop the container

Start a container that has stopped running
View all container commands as follows:

$ docker ps -a

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 specify the running mode of the container through - d.

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

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 it docker exec Command, because this exits the container terminal, it 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 docker exec is recommended.
Please use the docker exec --help command to view more parameter descriptions.
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. Using docker port, you can view the port number of a specified (ID or name) container mapped to the host host.
The web application container ID we created above is bf08b7f2cd89 named 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 output the standard output inside the container like tail -f.
From the above, we can see that the application uses port 5000 and can view the access log of the application.

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"
        },
......
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

The running container can be restarted by using 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 bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove

Viewing container processes

[root@localhost ~]# docker top d007429f001a
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3252                3230                0                   10:17               ?                   00:00:00            /bin/bash -c while true;do echo helloworld;sleep 1;done

View container details

[root@localhost ~]# docker inspect d007429f001a
[
    {
        "Id": "d007429f001ad110135189e4b47b88dcd1ecdf4fda56a81737e684bc39ed58d2",
        "Created": "2021-05-18T02:17:33.02961258Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo helloworld;sleep 1;done"
        ],
. . . . . . 

Enter container

[root@localhost ~]# docker exec -it d007429f001a /bin/bash
[root@d007429f001a /]# 

Delete all containers
-q: Show container id only
-a: Show all containers

[root@localhost ~]# docker rm -f $(docker ps -qa)  
d007429f001a
cda571909709
39d5ca7bfd22
70194b3061a0
206bd81407b8
bb4b6829d7f6
85b3981dbe76
01e16ecace22
89531bd1e599

Copy files from container to host
docker cp container id: the path within the container to the destination host

[root@localhost ~]# docker cp 08ddebb68f2f:/home/test.txt /root
[root@localhost ~]# ll -rt
 Total consumption 349584
-rw-r--r--   1 root root         0 5 November 18:19 test.txt

Topics: Docker