homeabouttwitter

Docker everyday commands

27 August, 2020 - 2 min read

These are the most useful commands to help you every day using Docker.

Check ten last lines on the container terminal

This command will return the last ten lines on the container terminal. This comand is great to check errors fetched recently in your API or if your process has finished.

docker logs --tail 10 api_webapp

Return:

[Mon Aug 15 11:16:14 2022] 127.0.0.1:49408 Closing
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49409 Accepted
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49410 Accepted
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49411 Accepted
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49412 Accepted
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49411 Closing
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49409 Closing
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49410 Closing
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49412 Closing
[Mon Aug 15 11:16:14 2022] 127.0.0.1:49413 Accepted

You can choose to return more lines if you need to.

Check container IP address

Fetch container IP address.

docker inspect --format '{{ .NetworkSettings.IPAddress }}' api_webapp

Response:

172.17.0.2

You can use the command return to create bash scripts.

PS: Check container network configuration, run:

To fetch container general configuration:

docker inspect api_webapp

Return:

[
    {
        "Id": "2c8455e17162558247302d8789ebf9b08036fe0c8ce9017407768058f53c616c",
        "Created": "2022-08-15T18:33:28.167780969Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1978,
            "ExitCode": 0,
...

You can use --format argument to fetch different addresses (IP, MAC): https://docs.docker.com/engine/reference/commandline/inspect.

Check your container CPU usage

Fetch container statistics – CPU, memory and and network usage. Much like the top command for your container.

docker stats api_webapp

Return:

CONTAINER ID   NAME         CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O     PIDS
2c8455e17162   api_webapp   0.00%     3.691MiB / 7.667GiB   0.05%     1.74kB / 0B   3.62MB / 0B   2

Useful to check resource consumption in many containers.

Build images with tag (avoid images being market as latest)

In your Dockerfile folder, run:

docker build -t="project1-api-webapp" .