Docker - Common docker commands

Keywords: Docker Container

Docker - Common docker commands

1. Background start

Syntax:

docker run -d Image name

Usage example:

Run a container in background mode:

docker run -d centos

Note: when we start a container in the background mode, there must be a foreground process, otherwise the docker container will automatically stop if it finds no application.

As shown in the figure above, after running a container in the background mode, we check the currently running container and find that the container stops automatically.

2. View log

docker logs: get the logs of the container

Syntax:

docker logs [OPTIONS] CONTAINER

OPTIONS Description:

Name, abbreviationdefaultdescribe
–detailsDisplays additional details provided to the log
–follow , -fTrace log output
–sinceDisplay logs since timestamp (e.g. 2013-01-02T13:23:37Z) or related (e.g. 42 minutes for 42m)
–tail,-nallThe number of rows displayed from the end of the log. When passing a negative number or non integer, it defaults to all
–timestamps,-tpresentation time stamp
–untilThe log is displayed before the timestamp (e.g. 2013-01-02T13:23:37Z) or related (e.g. 42 minutes for 42m)

Usage example:

Write a shell script and run it:

docker run -d centos /bin/sh -c "while true;do echo wanliguyicheng;sleep 1;done"

Run successfully:

Track and view the earliest 10 logs and display the timestamp:

docker logs -tf --tail 10 f05c3273a591

To view the log of the last 10 minutes:

docker logs --since 10m container id

View logs after a certain time:

docker logs -t --since="2021-11-23T10:38:37" container id

To view a time period log:

docker logs -t --since="2021-11-23T10:38:37" --until "2021-12-23T10:38:37" container id

3. View process

docker top: view the process information running in the container and support ps command parameters.

Syntax:

docker top [OPTIONS] CONTAINER [ps OPTIONS]

The container does not necessarily have a / bin/bash terminal to execute the top command interactively when running, and the container does not necessarily have a top command. You can use docker top to view the running processes in the container.

Usage example:

To view the process information in the container:

docker top 0197c43d9498

To view the process information of all running containers:

for i in  `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done

4. View metadata

docker inspect: get metadata of container / image

Syntax:

docker inspect [OPTIONS] NAME|ID [NAME|ID...]

OPTIONS Description:

  • **-f 😗* Specifies the template file for the return value.
  • **-s 😗* Displays the total file size.
  • **–type 😗* Returns JSON for the specified type.

Usage example:

To view the metadata of the image centos:

[root@CHENG /]# docker inspect centos
[
    {
        "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-15T18:20:05.184694267Z",
        "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
        "ContainerConfig": {
            "Hostname": "9bf8a9e2ddff",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 231268856,
        "VirtualSize": 231268856,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/d255cc6c74c735197fa066b58845c6e297ce05c1340149b6ed409e5ad8f62efc/merged",
                "UpperDir": "/var/lib/docker/overlay2/d255cc6c74c735197fa066b58845c6e297ce05c1340149b6ed409e5ad8f62efc/diff",
                "WorkDir": "/var/lib/docker/overlay2/d255cc6c74c735197fa066b58845c6e297ce05c1340149b6ed409e5ad8f62efc/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

To view the IP of the running container centos:

[root@CHENG /]# docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 0197c43d9498
172.17.0.2

5. Execute commands in a running container

**docker exec: * * execute commands in the running container

Syntax:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

OPTIONS Description:

  • **-d 😗* Separation mode: running in the background
  • **-i 😗* Keep STDIN open even if there is no attachment
  • **-t 😗* Assign a pseudo terminal

Usage example:

First view the currently running container, enter the running container, and open an interactive terminal in the container:

docker exec -it 0197c43d9498 /bin/bash

6. Connect to the executing container

docker attach: connect to a running container.

Syntax:

docker attach [OPTIONS] CONTAINER

OPTIONS Description:

Name, abbreviationdefaultdescribe
–detach-keysOverrides the key sequence used to detach the container
–no-stdinDo not attach standard input
–sig-proxytrueProxy all received signals to the process

Use example

Connect to a running container:

docker attach 0197c43d9498

The difference between docker exec and docker attach

Both docker exec and docker attach commands can enter the container;

The docker exec -it command enters the container. If you enter the exit command, you can exit the container directly, but it will not stop the container. You usually use this command more.

docker attach can attach to the stdin of a container that has already been run, and then execute the command. However, it should be noted that using exit from this stdin or pressing CTRL-D will cause the container to stop.

Test 1: first connect to the container with docker attach, exit with exit, and then check the currently running container. It is found that the container has stopped.

Test 2: first connect to the same container with docker exec -it, exit with exit, and then check the currently running container. It is found that the container is still running.

7. Data copy

docker cp: used to copy the data in the container to the host.

Syntax:

docker cp  container id:In container host path

Usage example: copy the files in the container to the host

First, enter the container and create an empty file in the centos container:

Return to the host and execute the copy command:

docker cp 0197c43d9498:/home/test1.md /home

Copy succeeded!

Rookie tutorial Docker commands: https://www.runoob.com/docker/docker-command-manual.html

Posted by BenMo on Wed, 24 Nov 2021 00:24:14 -0800