Install Docker Engine and Compose on Linux machines (for developers)

Install docker:

Know the linux architecture type using

uname -m

mkdir -p $HOME/tools/docker_engine

Download latest static binary from https://download.docker.com/linux/static/stable/ to $HOME/tools/docker_engine

sudo groupadd docker

sudo gpasswd -a ${USER} docker

newgrp docker

find $HOME/tools/docker_engine -type f -name docker-*.tar.gz | xargs -I {} mv {} $HOME/tools/docker_engine/docker-engine.tar.gz

tar -xvzf $HOME/tools/docker_engine/docker-engine.tar.gz -C $HOME/tools/docker_engine

chmod a+rwx -R $HOME/tools/docker_engine/*

ls -1 $HOME/tools/docker_engine/docker | xargs -I{} sudo ln -sfn $HOME/tools/docker_engine/docker/{} /usr/bin/{}

Start docker service manually in the foreground

sudo dockerd

press Ctrl+C to stop/kill the docker engine

Start docker service manually in the background

sudo su

nohup dockerd > /dev/null 2>&1 &

exit

Kill the docker service running in the background

sudo ps -Aef | grep dockerd | grep -v grep | tr -s ' ' | cut -d' ' -f2 | xargs sudo kill -9

Clean up dockerd logs and data

sudo rm -rf /var/lib/docker

Install docker compose:

mkdir -p $HOME/tools/docker_compose

mkdir -p $HOME/.docker/cli-plugins

Download latest version from https://github.com/docker/compose/releases to $HOME/tools/docker_compose

cp $HOME/tools/docker_compose/docker-compose-linux-x86_64 $HOME/.docker/cli-plugins/docker-compose

chmod a+rwx $HOME/.docker/cli-plugins/docker-compose

sudo ln -sfn $HOME/.docker/cli-plugins/docker-compose /usr/bin/docker-compose

After making these changes, restart or reboot the Linux machine for the changes to take effect

Verify docker and compose installation:

docker --version

docker ps

docker compose version

Pull and run images without sudo:

sudo chmod a+rwx /var/run/docker.sock

docker run hello-world

Setup docker engine as a service on startup

The following steps can be used to setup docker engine as a service, that can start automatically on system boot.

Create a script file with following content to start dockerd:

sudo nano /usr/bin/startup-dockerd.sh

#!/bin/bash
nohup dockerd > /dev/null 2>&1 &
exit 0

Create a script file to shutdown dockerd:

sudo nano /usr/bin/shutdown-dockerd.sh

#!/bin/bash
pkill dockerd
exit 0

Make the script file executable for all users:

sudo chmod a+x /usr/bin/startup-dockerd.sh
sudo chmod a+x /usr/bin/shutdown-dockerd.sh

Create a service file to enable auto start on boot with the following content:

sudo nano /etc/systemd/system/startup-dockerd-command.service

[Unit]
Description=Script to start dockerd service
# Start after networking and basic multi-user environment is ready
After=network.target multi-user.target 

[Service]
Type=oneshot

# Ensure the system considers the service successful after it runs
RemainAfterExit=yes 

# The command to execute the startup script
ExecStart=/usr/bin/startup-dockerd.sh

# The command to execute the shutdown script
ExecStop=/usr/bin/shutdown-dockerd.sh

[Install]
# Specifies the target that should start this service
WantedBy=multi-user.target

Reload the Systemd manager configuration:

sudo systemctl daemon-reload

Enable auto start up at boot:

sudo systemctl enable startup-dockerd-command.service

Start the service immediately (for testing purposes, without rebooting):

sudo systemctl start startup-dockerd-command.service

Check the service status and its details

sudo systemctl status startup-dockerd-command.service

Check the service name from the entire list

sudo systemctl list-units | grep startup-dockerd-command

Stop the service

sudo systemctl stop startup-dockerd-command.service

Disabled auto start up at boot:

sudo systemctl disable startup-dockerd-command.service

Comments

Popular posts from this blog

Export/Backup Ollama Model

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing