Mastering Docker: A Complete Guide to Setup, Automation, and Management!

I'm excited to share my latest deep dive into Docker, including the steps to install Docker, a script to automate Docker management, the differences between systemctl and service commands, and tips for log analysis using journalctl. Whether you're new to Docker or just looking to optimize your workflow, I hope these insights help!

📑 Table of Contents:

  1. Docker Installation Steps

  2. Automating Docker Start/Stop

  3. Differences Between systemctl and service

  4. Using journalctl to Analyze Docker Logs


🔹 Docker Installation Steps

Getting Docker installed and ready is the first step! Here’s a quick rundown on how to do it:

For Ubuntu:

sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

For CentOS:

sudo yum update -y
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker

🔹 Automating Docker Start/Stop

I created a simple Bash script to automate starting and stopping Docker, so managing your containers is as easy as running a single command. Check it out below!

#!/bin/bash

if [ "$1" == "start" ]; then
    echo "Starting Docker..."
    sudo systemctl start docker
elif [ "$1" == "stop" ]; then
    echo "Stopping Docker..."
    sudo systemctl stop docker
else
    echo "Usage: $0 {start|stop}"
fi

Run this script with ./docker_control.sh start or ./docker_control.sh stop to manage Docker effortlessly!


🔹 Differences Between systemctl and service

Ever wondered about the differences between systemctl and service commands?

  • systemctl: This command is used for managing services under systemd, the init system for modern Linux distributions. It provides more features, like viewing service status, logs, and dependencies.

  • service: This command is part of the older SysV init system. It still works with systemd but provides fewer options.

Example usage:

# Using systemctl
sudo systemctl start docker
sudo systemctl status docker

# Using service
sudo service docker start
sudo service docker status

🔹 Using journalctl to Analyze Docker Logs

For troubleshooting or understanding what’s happening with Docker, journalctl is a fantastic tool!

To view Docker-specific logs:

sudo journalctl -u docker

Additional options:

  • Real-time logs: sudo journalctl -u docker -f

  • Filter by time: sudo journalctl -u docker --since "1 hour ago"

  • Error-level logs: sudo journalctl -u docker -p err

This approach helps you quickly identify issues and keep your Docker environment running smoothly!

0
Subscribe to my newsletter

Read articles from Anirban Banerjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anirban Banerjee
Anirban Banerjee