Some docker snippets

Random docker snippets that have helped me out in the past

Some docker snippets

How to connect to a running container

docker exec -it <container-id> bash

How to move the image cache

source

# stop the docker service
sudo systemctl stop docker

# check the docker status is stopped
sudo systemctl status docker

# check there are not docker processes running
ps faux | grep -i docker

# create a folder for the new location of the docker images
mkdir /new/home/for/docker/images

# copy across all the images from the current home to the new home
rsync -avxP /var/lib/docker/ /new/home/for/docker/images
# edit the docker service configuration
sudo nano /lib/systemd/system/docker.service

# change this line
# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

# for this line
# ExecStart=/usr/bin/dockerd -g /new/home/for/docker/images -H fd:// --containerd=/run/containerd/containerd.sock
# reload the docker daemons
sudo systemctl daemon-reload

# start the docker service
systemctl start docker

# list images to check all working
docker images

# check location of images have now moved
docker inspect image_id | grep WorkDir

How to periodically prune dangling images

source

# navigate to cron job folder and create a new file
cd /etc/cron.daily
sudo nano docker-prune

The contents of the file with clean up the images every month:

#!/bin/bash
docker system prune -af  --filter "until=$((30*24))h"
# provide the correct rights on the script file
sudo chmod +x /etc/cron.daily/docker-prune

How to migrate images from one registry to another

  1. login into the source registry
az acr login --name myregistry

or

docker login --username <uid> --password <pwd> <src-registry>
  1. pull the image
docker pull <src-image> -a
  1. re-tag the image
docker tag <src-registry>/<src-image:tag> <dest-registry>/<dest-image:tag>
  1. login into the destination registry
docker login --username <uid> --password <pwd> <dest-registry>
  1. push the image
docker push <dest-registry>/<dest-image:tag> 
  1. (optional) save a .tar of the docker image for backup
docker image save <dest-registry>/<dest-image> <filename>.tar

How to copy in and out of a container

#  copy from the container to the host
docker cp <containerId>:/file/path/within/container /host/path/target

# copy from the host to the container
docker cp my.file <containerId>:/home/dir1

How to filter images

docker images --filter=reference='bit*/*:*'

assuming the following images on my system, if i were to execute the above command:

bitwarden/one:1.0
bitwarden/onepointfive:1.5
bitwarden/two:2.0
something/else:latest

I would expect the results:

bitwarden/one:1.0
bitwarden/onepointfive:1.5
bitwarden/two:2.0

executing the command docker images --filter=reference='bit*' would yield zero results because of the forward slash for multi part names. bit* is not the same as bit**

The difference between ENTRYPOINT and CMD

CMD allows us to override the call

ENTRYPOINT allows us to extend the call

for example:
if our Dockerfile ends with: CMD dosomething then we can run it as:

docker run \<docker-image\> ls

the ls command overrides the dosomething command

if our Dockerfile ends with: ENTRYPOINT ["dosomething"] then we can run it as:

docker run \<docker-image\> --myparam

this will run the dosomething command with the --myparam switch

can also use both commands to set the baseline and optional values that may be overriden:

ENTRYPOINT ["top", "-b"]
CMD ["-c"]

The args of a docker run command are the command entry of a docker-compose file.

Adding to the path

# add to PATH

ENV PATH /root/.dotnet/tools:$PATH