Docker

Variable expansion inside the container

  • To prevent the replacement from happeningin the outer shell, you need to use single quotes.
  • TO ensure that there is a shell within the docker container that can do replacement, you need to set the shell explicitly. Docker will just directly invoke execpl("echo", "$foobar", NUL) inside the container. This does not do any substitution.
docker run --rm --env FOOBAR="hello world" $image sh -c 'echo "$FOOBAR"'

Cleanup

Source

Old containers can be removed all at once:

# Containers used hours,weeks or months ago
docker ps -a | egrep '(hours|weeks|months) ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

# All containers
docker rm $(docker ps -a -q)

Old Images: Source

# All images
docker rmi $(docker images -q)

# Untagged images
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

Check for experimental feature

Source

docker version -f '{{ .Server.Experimental }}'

Error

Registry Endpoint

Source

  • Description: When running docker pull <imagename>, the following error message appears:
...

Could not reach any registry endpoint
  • Solution: This can have two reasons. Docker tries to download automatically the tag 'latest'. If that is not available, this can come up as error. Second: The installed docker version can be too old to play harmonously with the hub. Upgrade the docker version. This error has been seen at version 1.4 and 1.5 already.

Docker CentOS7

Source

Errormessage:

/usr/bin/docker: relocation error: /usr/bin/docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference

Solution:

sudo dnf install device-mapper-devel

Alternative: Update to the latest version with

sudo dnf update

DeleteDevice

Source Error:

Unable to delete device: Error running DeleteDevice dm_task_run failed

Solution:

Ran full of disk space the first time, he? Well, happened to me as well. Reboot does not fix this. When filling up the disk, some garbage is left that prevents docker from starting now.

Just delete /var/lib/docker and try again (migh want to save something?)

sudo rm -rf /var/lib/docker

docker build

Environment variables during build

docker build [...] --build-arg VARIABLE=value

Conditional copy of files

Source

This example from a Dockerfile runs the COPY command in dependency of an environment variable being set. This example is based on using the alpine docker image.

ARG BUILD_ENV=copy

FROM alpine as build_copy
ONBUILD COPY file /file

FROM alpine as build_no_copy
ONBUILD RUN echo "I don't copy anythin."

FROM build_${BUILD_ENV}
# other stuff

Analysing images

dive can be used to analyse an existing image: https://github.com/wagoodman/dive

dive $imageid

Errors

Import vs load

The commands import and load are not identical and can lead to interessting effects when trying to run the handled images.

In the specific example an image was exported by podman using save (equivalent to docker save) and imported with docker import. This resulted in an error message like executeable not found in $PATH. While the image was running fine in podman, it failed to run in docker.

The cause for this is that the correct command for importing images that have been exported with save is load, while images saved with export can only be imported with import. Using import instead of load will not result in an error message though.

Export with Import with
docker save docker load
docker export docker import