Skip to content

Instantly share code, notes, and snippets.

@nurrony
Last active September 14, 2024 06:31
Show Gist options
  • Select an option

  • Save nurrony/fc1e93cbae13457e9fe9cfcd71c563f7 to your computer and use it in GitHub Desktop.

Select an option

Save nurrony/fc1e93cbae13457e9fe9cfcd71c563f7 to your computer and use it in GitHub Desktop.
This Gist collects all docker hacks that I needed to do for various reasons for my work and during playing with it.

Docker Images backup

This document show you how to save an image or export a container+image and load or export it.

Save an image

docker save <image-name>:<tag> > /path/to/save/the/image.tar

Load the image again after save

docker load < /path/to/save/the/image.tar

Export a container

docker export <CONTAINER ID> > /path/to/export.tar

Import a container

docker cat /home/export.tar | sudo docker import - <image-name>:<tag>

The exported-imported image has lost all of its history whereas the saved-loaded image still have its history and layers. This means that you cannot do any rollback to a previous layer if you export-import it while you can still do this if you save-load the whole (complete) image (you can go back to a previous layer by using docker tag <LAYER ID> <IMAGE NAME>).

Why Docker container eating disk space?

Docker includes multiple logging strategies to help you to get information from running containers. These strategies are called logging drivers. By default, Docker uses the “json-file” logging driver.

We can fix the issue if we restrict the size of the log file by setting max-size equals 10m. There is a second option max-file allows to restrict the maximum numbers of log files. The oldest files will be removed if the logger creates an excess file. Every time when the current log file exceeds the value defined in max-size it leads to create a new logs file and add to the name current file with a suffix. The suffix is a number index.

NOTE max-file option is effective only when max-size is set too.

The Docker team suggests using the “local” logging driver to prevent disk-exhaustion that by default preserves 100MB of log messages per container but you also can configure the “json-file” driver with log-rotation as well.

Docker Command

We can run the container:

docker run -it -log-opt max-size=10m -log-opt max-file=5 IMAGE[:TAG]

Docker Compose

If you run containers using docker-compose just add the following snippet into a service definition:

logging: 
  options:
    max-size: "10m"
    max-file: "5"

Configuring Docker with local registry

First create directory and file for custom configuration:

sudo mkdir /etc/systemd/system/docker.service.d
sudo touch /etc/systemd/system/docker.service.d/docker.conf

Open docker.conf using your favourite editor

sudo vi /etc/systemd/system/docker.service.d/docker.conf

Add the following lines there

# content for /etc/systemd/system/docker.service.d/docker.conf
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// $DOCKER_OPTS
EnvironmentFile=-/etc/default/docker

Open docker file in your favourite editor

sudo vi /etc/default/docker

Edit the DOCKER_OPTS like below. Replace localhost:5000 with your registry domain name and port

DOCKER_OPTS="--insecure-registry localhost:5000"

Reload configuration and restart Docker Daemon

sudo systemctl daemon-reload
sudo systemctl restart docker

Changing Docker default storage location

Credit goes to this Stackoverflow Answer

Following advice from comments I utilize Docker systemd documentation to improve this answer. Below procedure doesn't require reboot and is much cleaner.

First create directory and file for custom configuration:

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo $EDITOR /etc/systemd/system/docker.service.d/docker-storage.conf

For docker version before 17.06-ce paste:

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --graph="/mnt"

For docker after 17.06-ce paste:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --data-root="/mnt"

Then reload configuration and restart Docker:

 sudo systemctl daemon-reload
 sudo systemctl restart docker

To confirm that Docker was reconfigured:

docker info|grep "loop file"

In recent version (17.03) different command is required:

docker info|grep "Docker Root Dir"

Output should look like this:

Data loop file: /mnt/devicemapper/devicemapper/data
Metadata loop file: /mnt/devicemapper/devicemapper/metadata

Or:

Docker Root Dir: /mnt

Then you can safely remove old Docker storage:

rm -rf /var/lib/docker

Restart Application Container with server restart

First create systemd configuration file as follows. Replace app-name with your container service name

sudo touch /etc/systemd/system/app-name.service

Open the file with your favourite editor and add the content as follows. Edit according to your need.

Description=My Containerized App
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/local/bin/docker-compose -f /path/to/docker-compose.yml down
ExecStart=/usr/local/bin/docker-compose -f /path/to/docker-compose.yml up -d

[Install]
WantedBy=multi-user.target

Reload configuration and enable Docker Container App Service as follows

sudo systemctl daemon-reload
sudo systemctl enable app-name.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment