Skip to content

Docker

Usage

Build an image

The general command is

docker build [OPTIONS] PATH | URL | -

Most commonly you'll use docker build -t tag . if there is a Dockerfile in the directory.

Build image from a git repository

docker build -t tag https://github.com/docker/rootfs.git#[tag_or_branch]

There are more possible configurations available, check the Docker documentation.

Registry as a cache

docker run -d -p 5000:5000 --restart always --name registry -e "REGISTRY_DELETE_ENABLED=true" -e "REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io" registry:2

And the in the clients run dockerd with --registry-mirror http://host:5000.

docker-doc

GUI

X server

You should mount your local ~/Xauthority to the docker.

If it doesn't work, try from your machine:

xhost +local:root # this enables local non-network connections

So it enables access control.

ros.org

Configuration

Change docker data root path

In the systemd unit file:

ExecStart=/usr/bin/dockerd --data-root [docker_data_root_path]

Tips

Manage dockers with systemd

[Unit]
Description=Foo Service
After=docker.service
Requires=docker.service
After=docker.redis.service
Requires=docker.redis.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker stop foo
ExecStartPre=-/usr/bin/docker rm foo
ExecStartPre=/usr/bin/docker pull foo
ExecStart=/usr/bin/docker run --name foo
  --link docker.redi.service:redis --rm
  foo

[Install]
WantedBy=multi-user.target

Note: The “-” at the start means systemd won’t abort if the command fails.

container-solutions

Delete dangling volumes

Delete orphan volumes (created when not running docker run with the --rm option): docker volume rm \docker volume ls -q -f dangling=true``

coderwall

Remove unused data

Use docker system prune.

Automated clean up

Add the following line to crontab -e:

0 3 * * * /usr/bin/docker system prune -f 2>&1 > /dev/null

-f prevents manual confirmation from being asked.

docker stats $(docker ps --format '{{.Names}}')

stackoverflow

Get the run command of a docker

Sometimes you have a running docker of which you want to get the command line options it was originally launched with. You can get it with:

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
    assaflavie/runlike [container]

stackoverflow

Upgrade all your currently downloaded docker images

docker images | grep -vE "(REPOSITORY|local|<none>)" | awk '{print $1":"$2}' | xargs -L1 docker pull

Sort docker images by size desc

docker images --format '{{.Size}}\t{{.Repository}}\t{{.Tag}}\t{{.ID}}' | sed 's/ //' | sort -h -r | column -t

Don't send unnecessary files/directory to build context

When building an image from a Dockerfile, Docker copies the files and directories in the local path, which can take a long time if the files are large.

You can add unnecesary files and directories to .dockerignore so that they are't copied when building.

Reference

Healthcheck