Posted on 3 mins read

Note: If you have as well some tricks to cover you when you use Docker, I’ll be pleased to add them

As a software developer, I work with Docker quite rarely. I create at the beginning of a project:

  • the scripts to build the needed custom docker images
  • the scripts to launch quickly the docker containers
  • and that’s almost all.

If you’re like me, you probably forget quickly the Docker way or the tips and after 30 mins of déjà vu digging, you get frustrated.

Debug docker run when the container terminates immediately

Launch your container with the docker run command

docker run -d --name cdadb --rm -p 5432:5432 pgsql
a0bfa9fa74b66ddd1c2242f169fc508d47c83263ac92667cd8adaa7bbc1c9ae4

Then check if it’s running with the docker ps command

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

If your container is not listed above, we have the same problem. Our containers are not running, probably terminated during the initialization steps because of an error in the customization.

First way

To find what happened, give the docker run command result to the docker log command. The docker run command print the ID of the container it creates and so one gives it as a parameter to the docker log command

docker logs -f $(docker run -d --name $BASENAME --rm -p 5432:5432 $BASENAME-backend)
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

[...]

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/01_setup_db_dump.sql

[...]

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/02_backup_dump.sh
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.

Et voilà! The last line printed by the container logs tells me that I use the dump of the postgres database in a wrong way. I should use psql instead of pg_restore.

This trick is applicable to all images. I describe it with the postgres image because it’s my personal case.

Alternative way

I have the habits to execute the docker run command with the --rm option: Automatically remove the container when it exits.

If one executes the docker run command without it, it’s possible to get the container logs after its termination

# Run without --rm option
docker run -d --name cdadb -p 5432:5432 pgsql
bd6ddfba3ce3a9dd0d9e912b8617603ba49425a2df67d45ff183bd1a6a380603
# List all containers even the terminated ones
docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
bd6ddfba3ce3        pgsql               "docker-entrypoint..."   6 seconds ago       Exited (1) 3 seconds ago                       cdadb
# Print the logs
docker logs pgsql

Free your SSD space, delete docker history

After few months of usage and if you never clean, projects after projects, Docker is literally eating your disk space.

Remove all the containers from your machine

docker rm -f $(docker ps -a -q)

Remove all the images from your machine

docker rmi -f $(docker images -q)