Skip to content
Gabor Szarnyas edited this page Dec 19, 2022 · 50 revisions

Concepts

  • container
  • image
  • volume

Docker cannot access networks

  • Problem: neither DNS resolution, nor ping works

  • Solution: check the accepted answer at https://stackoverflow.com/a/23811974/3580502:

    sudo vim /etc/NetworkManager/NetworkManager.conf
    # comment the "dns=dnsmasq" line
    sudo service network-manager restart
    

Move Docker data folder to a different location

  1. Stop the service with sudo service docker stop.
  2. Create or edit the file /etc/docker/daemon.json.
  3. Add the following content
    { 
       "data-root": "/path/to/new/docker/data/dir" 
    }
  4. Copy the contents sudo rsync -aP /var/lib/docker/ /path/to/your/docker-data
  5. Restart the service sudo service docker start

The steps are from this article.

Tips

Installing Docker on Ubuntu 20.04

As of March 2021, you can install a not-too-ancient version of Docker, 19.03.8, on Ubuntu 20.04 by simply issuing:

sudo apt install -y docker.io
sudo gpasswd -a ${USER} docker

There are two options: either reboot the machine (which is often impractical/slow on a big machine) or run the following command in each shell session:

newgrp docker

The previous approach was to use sudo usermod -aG docker $USER but it required a restart (even with newgrp).

docker rmi -f $(docker images | grep "<none>" | awk '{print $3}')
docker exec -it $CONTAINER bash
echo $HOSTNAME

Detach from container without stopping it then attach to the same console

  • Ctrl+P, Ctrl+Q
  • docker attach «CONTAINER ID»

Save all local Docker images then load

docker save $(docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>" ; docker images -qa) | pv | zstd -10 -T0 -o docker.zst
zstdcat docker.zst | pv | docker load

Change these if needed:

  • remove pv if showing progress is not needed
  • use other compression

Permissions in mounted volumes

By default, Docker containers running on Linux write to mounted volumes as the root user. (Note that this problem does not occur on Mac OS due to osxfs remapping the owner in the background.)

The recommended package to use depends on the system running in the container:

  • On Debian/Ubuntu, use gosu.
  • On Alpine Linux, use su-exec.

Fedora images can be based on the gosu/fedora image.

Using an Alpine Linux setup, both gosu and su-exec are displayed in a PR to the LDBC SNB Datagen (which deprecated the usage of gosu in favour of su-exec).

For a more comprehensive example, see the Neo4j Docker container's entrypoint.

See also the Jupyter Docker stack's workaround.

chown

Recursive chown might be slow and it blocks others accessing the mounted files while the container is running. In these cases try to avoid it.

Options:

  • sudo chown -R $USER:$USER folder
  • docker run <image> /bin/chown -R $(stat -c '%u' ..):$(stat -c '%g' ..) .
    • Pros: no need for sudoer password
    • Instead of the regular <image> used in your workflow, you can also use another simple image where the entrypoint has root privileges.

Podman

If you only have Podman, considering adding a soft symlink (for a global alias) to /usr/bin/docker that points to the podman binary.

Debian/Ubuntu 20.04+: apt install prompts for timezone information

Problem: apt install prompts for timezone data and then times out.

debconf: falling back to frontend: Readline
Configuring tzdata
------------------
Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing
the time zones in which they are located.
  1. Africa      4. Australia  7. Atlantic  10. Pacific  13. Etc
  2. America     5. Arctic     8. Europe    11. SystemV
  3. Antarctica  6. Asia       9. Indian    12. US

Solution:

Add the following environment variable to the Dockerfile:

ENV DEBIAN_FRONTEND noninteractive

Remark: This is also listed on the Linux page but it also commonly occurs in Docker.

Ubuntu 22.04.2+: apt prompts for restarting services

Problem: apt upgrade and apt install may prompt for restarting services, causing it to hang in non-interactive environments.

Scanning processes...
Scanning candidates...
Scanning linux images...

Running kernel seems to be up-to-date.

Restarting services...

Solution:

Add the following environment variable to the Dockerfile:

ENV NEEDRESTART_SUSPEND a

Remark: This is also listed on the Linux page but it also commonly occurs in Docker.

Docker Hub

List all tags

wget -q https://registry.hub.docker.com/v1/repositories/$REPOSITORY/tags -O - | jq -r '.[].name'
# or
curl -s https://registry.hub.docker.com/v2/repositories/$REPOSITORY/tags?page_size=10000 | jq -r '.results[].name'

(sources: 1, 2)

Check availability of Docker Hub image

https://github.com/TransformationToolContest/ttc2018liveContest/blob/a10e9f59324d3a7efd1e1b9471078ffa9519a34d/docker/check-docker-hub-tags.sh

Scheduled cron jobs in GitHub Actions are disabled when no repository activity has occurred in 60 days. Repo admin receives e-mail notification. (docs)

Running Docker images on Apple Silicon (M1, etc.)

For some containers, the user needs to specifically request the ARM64-specific container.

$ docker run -it --name ubuntu-default ubuntu:20.04 /bin/bash
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
$ docker run -it --platform linux/arm64 --name ubuntu-arm64 ubuntu:20.04 /bin/bash
# runs the ARM64 variant
$ for i in `docker ps --format "{{.Image}}"` ; do docker image inspect $i --format "$i -> {{.Architecture}} : {{.Os}}" ;done
# lists the architecture of the running containers

Docker caches whether there is an Arm64 variant available for a given image. So subsequent launches of Ubuntu with the default platform will also launch Arm64 containers.

Links

Clone this wiki locally