DEV Community

Cover image for Install Docker and Docker Compose on Linux
Batkhuu
Batkhuu

Posted on

Install Docker and Docker Compose on Linux

Official pages


1. Install Docker

1.1. Install Docker with the script

# Download docker installer script:
curl -fsSL https://get.docker.com -o get-docker.sh
# Install docker by 'get-docker.sh' script:
DRY_RUN=1 sudo sh get-docker.sh

# Remove downloaded script:
rm -vrf get-docker.sh

# To avoid using 'sudo' for docker commands:
# Create a new 'docker' group:
sudo groupadd docker
# Add current user to the 'docker' group:
sudo usermod -aG docker $(whoami)

# Apply new group changes to the new shell session:
newgrp docker
# Or reboot the system to apply docker group changes:
sudo shutdown -r now

# Configure docker to start on reboot:
sudo systemctl enable docker.service
sudo systemctl enable containerd.service

# Check docker is installed and running:
docker -v
docker info
docker images
Enter fullscreen mode Exit fullscreen mode

1.2. [Only for GPU] Install NVIDIA Docker

## [IMPORTANT] Only for Ubuntu/Debian-based linux
# Download the nvidia-docker GPG key and setup the stable repository:
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
   && curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
   && curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

# Update repository list:
sudo apt-get update
# Install nvidia-docker2:
sudo apt-get install -y nvidia-docker2
Enter fullscreen mode Exit fullscreen mode

Edit the /etc/docker/daemon.json file:

sudo nano /etc/docker/daemon.json
Enter fullscreen mode Exit fullscreen mode

and add the following default-runtime into the /etc/docker/daemon.json file:

{
    "default-runtime": "nvidia"
}
Enter fullscreen mode Exit fullscreen mode

For example:

{
    "default-runtime": "nvidia",
    "runtimes":
    {
        "nvidia":
        {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

save changes and exit from the file editor.

# Restart docker service:
sudo systemctl daemon-reload
sudo systemctl restart docker.service
Enter fullscreen mode Exit fullscreen mode

2. [RECOMMENDED] Limit docker log file max size and max rotation

Edit the /etc/docker/daemon.json file:

sudo nano /etc/docker/daemon.json
Enter fullscreen mode Exit fullscreen mode

and add the following log-opts into the /etc/docker/daemon.json file:

{
    "log-opts":
    {
        "max-size": "10m",
        "max-file": "10"
    }
}
Enter fullscreen mode Exit fullscreen mode

save changes and exit from the file editor.

# Restart docker service:
sudo systemctl restart docker.service
Enter fullscreen mode Exit fullscreen mode

3. Install Docker Compose V2

## Download docker-compose binary release:
# [RECOMMENDED] Compose v2.2.3 (Linux) release:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-$(uname -m)" -o /usr/local/bin/docker-compose
# [For old version] Compose v1.29.2 (Linux) release:
# sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# [Only for experimental] Latest version release:
# sudo curl -L $(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url | grep linux-$(uname -m)\" | cut -d '"' -f 4) -o /usr/local/bin/docker-compose

# Apply executable permission to the docker-compose binary:
sudo chmod +x /usr/local/bin/docker-compose

# Create a symbolic link to '/usr/bin':
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# Check docker-compose version:
docker-compose --version
Enter fullscreen mode Exit fullscreen mode

👍 ✨

INSTALLATION COMPLETE


[OPTIONAL] Change the docker data directory to the external storage

# Stop the docker service:
sudo systemctl stop docker.service
Enter fullscreen mode Exit fullscreen mode

Edit the /etc/docker/daemon.json file:

sudo nano /etc/docker/daemon.json
Enter fullscreen mode Exit fullscreen mode

and add the following data-root into the /etc/docker/daemon.json file (don't forget to change [NEW_STORAGE_PATH] to your new directory):

{
   "data-root": "[NEW_STORAGE_PATH]/docker/data-root"
}
Enter fullscreen mode Exit fullscreen mode

For example:

{
   "data-root": "/mnt/ssd/1/docker/data-root"
}
Enter fullscreen mode Exit fullscreen mode

save changes and exit from the file editor.

# Copy the current docker data directory to the new directory:
sudo rsync -a /var/lib/docker/ [NEW_STORAGE_PATH]/docker/data-root
# For example:
sudo rsync -a /var/lib/docker/ /mnt/ssd/1/docker/data-root

# Rename the old docker data directory as a backup:
sudo mv /var/lib/docker /var/lib/docker.old

# Start docker service:
sudo systemctl start docker.service

# Test docker is working properly:
docker -v
docker info
docker images
docker ps -a

# Remove the old backup docker data directory:
sudo rm -rf /var/lib/docker.old
Enter fullscreen mode Exit fullscreen mode

[OPTIONAL] Install command auto-completion for docker-compose

# Download docker-compose auto-completion script into '/etc/bash_completion.d/':
sudo curl \
    -L https://raw.githubusercontent.com/docker/compose/1.29.2/contrib/completion/bash/docker-compose \
    -o /etc/bash_completion.d/docker-compose

# Reload current bash shell session:
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)