Docker Container-Centric Commands for Beginners: Part 2

Docker Container Inspect

From https://docs.docker.com/engine/reference/commandline/container_inspect/?spm=a2c41.12663068.0.0.14f42769HiTBdO#description

docker container inspect mycontain
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,

Docker Container Commit and Export

  • docker container commit — Create a new image from a container’s changes
  • docker container export — Export a container’s filesystem as a tar archive
  • commit: create a new image. You can then build a new container using this image as the base image. The new container is like a clone of original container. environment variables still exist.
  • export: save container content as a tar archive file. You can then untar and investigate the file content at any Linux shell. Original container totally gone. All we have is a file system: no environment variables and no concept of ‘container running state’.

Summary Demo — Docker Container Commands

This part of the tutorial will use several docker container commands discussed in part 1 and this part 2.

  • write simple bash signal trapping script
  • write short Dockerfile to create mytraps:demo image
  • run mytraps:demo image
  • observe echo output via docker logs
  • send TERM signal via docker kill — observe result in logs
  • rerun mytraps:demo image
  • send INT signal via docker kill — observe result in logs
  • rerun mytraps:demo image
  • attach to container via docker attach
  • press ctrl-c — observe result in logs
  • rerun mytraps:demo image
  • send KILL signal via docker kill — observe result in logs
  • run mytraps:demo image
  • observe echo output via docker logs
  • method 1: change our environment variable via docker exec — observe result in logs
  • method 2: change our environment variable via docker exec — observe result in logs
  • send KILL signal via docker kill — observe result in logs
  • write simple bash signal trapping script
remove-me#!/bin/bash
function SIGINT_trap() {
echo . . . SIGINT signal caught
echo . . . doing SIGINT cleanup
exit
}
function SIGTERM_trap() {
echo . . . SIGTERM signal caught
echo . . . doing SIGTERM cleanup
exit
}
trap SIGINT_trap SIGINT
trap SIGTERM_trap SIGTERM
for i in `seq 1 50`; do
sleep 1
echo -n " . "
echo $i
done
exit 0
  • write short Dockerfile to create mytraps:demo image
nano Dockerfile
FROM alpine:3.8
ENV myvar original value
COPY traps /root/
RUN chmod +x /root/traps
CMD ["/bin/sh", "/root/traps"]
  • line 1: use Alpine 3.8 image
  • line 2: define myvar environment variable and supply a value
  • line 3: copy our bash traps script to /root/ directory in container
  • line 4: make our traps script executeable
  • line 5: define we want to run our traps script upon container startup.
docker build --tag mytraps:demo --file Dockerfile  .
  • prepare second shell console to observe container logs
docker container logs mycontain -f
  • run mytraps:demo image
docker container run -d --name mycontain mytraps:demo
  • observe echo output via docker logs
  • send TERM signal via docker kill
docker container kill --signal 15  mycontain
  • observe result in logs
...
. 15
. 16
. 17
. 18
. 19
. . . SIGTERM signal caught
. . . doing SIGTERM cleanup
  • rerun mytraps:demo image … for signal interrupt test
docker container prune -f
docker container run -d --name mycontain mytraps:demo
  • observe echo output via docker logs
  • send interrupt signal via docker kill
docker container kill --signal SIGINT  mycontain
  • observe result in logs
...
. 16
. 17
. 18
. 19
. 20
. . . SIGINT signal caught
. . . doing SIGINT cleanup
  • rerun mytraps:demo image
docker container prune -f
docker container run -d --name mycontain mytraps:demo
  • observe echo output via docker logs
  • send interrupt signal via docker kill
docker container kill --signal SIGKILL  mycontain
  • observe result in logs
...
. 6
. 7
. 8
. 9
. 10
. 11
. 12

Docker Container Wait

From https://docs.docker.com/engine/reference/commandline/container_wait/

docker container run -d --name mycontain1 mytraps:demo sh -c 'sleep 10;exit 1'
docker container run -d --name mycontain2 mytraps:demo sh -c 'sleep 15;exit 2'
docker container run -d --name mycontain3 mytraps:demo sh -c 'sleep 25;exit 3'
docker container wait mycontain1 mycontain2 mycontain3
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6c7fe618e23b mytraps:demo "sh -c 'sleep 25;exi…" 40 seconds ago Exited (3) 14 seconds ago mycontain3
d4e1bb11e55c mytraps:demo "sh -c 'sleep 15;exi…" 43 seconds ago Exited (2) 27 seconds ago mycontain2
bee5cbf37052 mytraps:demo "sh -c 'sleep 10;exi…" 44 seconds ago Exited (1) 33 seconds ago mycontain1
docker ps -a --filter "name=mycontain1"  --filter "name=mycontain2" --filter "name=mycontain3"
docker ps -a --filter "ancestor=mytraps:demo"
docker ps -a | grep mycontain
docker ps -a | grep mytraps
alias mypsa='docker ps -a | grep '
mypsa traps
mypsa contain

docker container port

Tutorial Cleanup

Prune stopped containers:

docker container prune -f 

docker image rm mytraps:demo

Conclusion

In these 2 tutorials you used nearly all 25 docker container commands. You now have a good understanding of what commands you can apply against your containers.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com