Extra Commands

Here is a list of useful commands when working with Docker containers.

Docker Process Status

The first command to learn is: docker ps. This displays the running containers on the device. Add --all to see all containers on the device, even if they are not running.

docker ps
Copy to clipboard

Example Output:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
244a84742697        shellhttpd:1.0       "/usr/local/bin/http…"   6 minutes ago       Up 6 minutes        0.0.0.0:8080->8080/tcp   shellhttpd
Copy to clipboard

Docker Logs

Often, it is useful to watch Docker container logs. Use docker logs <container name> to see the container’s logs. If you want the command to keep following the log, use the --follow parameter:

The log might be empty unless you tested the shellhttpd application with curl or the browser:

docker logs --follow shellhttpd
Copy to clipboard

Example Output:

GET / HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Cache-Control: max-age=0
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,\*/\*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7

= Thu Mar 18 01:03:14 UTC 2021 =============================
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
DNT: 1
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/\*,\*/\*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://127.0.0.1:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7

= Thu Mar 18 01:03:14 UTC 2021 =============================
Copy to clipboard

Docker Execute

The docker exec command runs a new command in a running container.

To verify the files in the root file system of the container, use the following command:

docker exec shellhttpd ls /usr/local/bin/
Copy to clipboard

Example Output:

httpd.sh
Copy to clipboard

To check what processes are running:

docker exec shellhttpd ps
Copy to clipboard

Example Output:

PID   USER     TIME  COMMAND
1 root      0:00 {httpd.sh} /bin/sh -e /usr/local/bin/httpd.sh
13 root      0:00 nc -l -p 8080
36 root      0:00 ps
Copy to clipboard

Finally, you can start a shell inside the container with:

docker exec -it shellhttpd sh
Copy to clipboard

Example Output:

ls
in    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
exit
Copy to clipboard
Where:
  • -i - keep STDIN open even if not attached.
  • -t - allocate a pseudo-TTY.
  • shellhttpd - container name.
  • sh - shell command.

Docker Remove

To stop and then remove the container, run the commands:

docker stop shellhttpd
docker rm shellhttpd
Copy to clipboard

During development, it is common to make and test changes to the Docker image. Let us give this a try. In the file httpd.sh, we specify the MSG variable with ${MSG-OK}. This means if MSG is not otherwise specified, it is set with the default value “OK”.

Using the text editor of your choice, change OK to FoundriesFactory, then rebuild and run:

gedit httpd.sh
Copy to clipboard

httpd.sh:

#!/bin/sh -e

PORT="${PORT-8080}"
MSG="${MSG-FoundriesFactory}"

RESPONSE="HTTP/1.1 200 OK\r\n\r\n${MSG}\r\n"

while true; do
        echo -en "$RESPONSE" | nc -l -p "${PORT}" || true
        echo "= $(date) ============================="
done
Copy to clipboard

Build and run the container again:

docker build --tag shellhttpd:1.0 .
docker run --name shellhttpd -d -p 8080:8080 shellhttpd:1.0
Copy to clipboard

Test the new change with curl:

curl 127.0.0.1:8080
Copy to clipboard

Example Output:

FoundriesFactory
Copy to clipboard

The docker run command can accept many other parameters. For example, the --env parameter which specifies an environment variable to the container. Remove the previous image and launch it again with: --env MSG=MyFirstContainer

Test the new change with curl:

docker stop shellhttpd
docker rm shellhttpd
docker run --env MSG=MyFirstContainer --name shellhttpd -d -p 8080:8080 shellhttpd:1.0
curl 127.0.0.1:8080
Copy to clipboard

Example Output:

MyFirstContainer
Copy to clipboard

Use docker exec to echo the MSG variable inside the container:

docker exec -it shellhttpd sh
Copy to clipboard

Inside the Container:

echo $MSG
MyFirstContainer
exit
Copy to clipboard

Remove the container:

docker stop shellhttpd
docker rm shellhttpd
Copy to clipboard

All these commands are important in understanding how Docker containers work. Next is going into how docker-compose works.