Docker Beginner's Guide: Installation and Essential Commands
Complete Docker guide covering installation, essential commands (run, stop, rm, ps), image management, volumes, and networking basics on Linux servers.
Table of Contents
Docker Beginner's Guide: Installation and Essential Commands
Docker is an open-source platform that lets you run applications in isolated containers. It provides a consistent environment from development to production. This guide covers Docker installation, essential commands, and practical usage scenarios.
What is Docker?
Docker packages applications and their dependencies into portable containers. Unlike virtual machines:
- Shares the host OS kernel (much lighter)
- Starts and stops in seconds
- Uses far fewer resources
- Eliminates the "works on my machine" problem
Docker is the cornerstone of modern software development and deployment. On REXE servers, you can easily deploy applications using Docker.
Installing Docker
Debian/Ubuntu
# Remove old versions
sudo apt remove docker docker-engine docker.io containerd runc
# Install prerequisites
sudo apt update
sudo apt install ca-certificates curl gnupg lsb-release -y
# Add Docker GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
# Start and enable service
sudo systemctl start docker
sudo systemctl enable docker
RHEL/CentOS/AlmaLinux
# Add Docker repository
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# Install Docker
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
# Start service
sudo systemctl start docker
sudo systemctl enable docker
Verify Installation
# Check Docker version
docker --version
# Run test container
sudo docker run hello-world
# Use Docker without sudo
sudo usermod -aG docker $USER
newgrp docker
Essential Docker Commands
Running Containers
# Run a simple container
docker run nginx
# Run in background (detached)
docker run -d nginx
# Run with port mapping
docker run -d -p 8080:80 nginx
# Run with a name
docker run -d --name web-server -p 8080:80 nginx
# Run with environment variable
docker run -d -e MYSQL_ROOT_PASSWORD=secret --name mysql mysql:8.0
# Run with volume mount
docker run -d -v /host/dir:/container/dir nginx
# Auto-restart policy
docker run -d --restart unless-stopped nginx
# Interactive terminal
docker run -it ubuntu bash
Container Management
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container
docker stop container_name
# Start a container
docker start container_name
# Restart a container
docker restart container_name
# Remove a container (must be stopped first)
docker rm container_name
# Force remove running container
docker rm -f container_name
# Remove all stopped containers
docker container prune
Container Monitoring
# View container logs
docker logs container_name
# Follow logs in real-time
docker logs -f container_name
# Last 100 lines of logs
docker logs --tail 100 container_name
# Container resource usage
docker stats
# Container details
docker inspect container_name
# Attach to container shell
docker exec -it container_name bash
# Run command in container
docker exec container_name ls /var/www
Image Management
# Pull an image
docker pull nginx
docker pull nginx:1.25
docker pull ubuntu:22.04
# List local images
docker images
# Remove an image
docker rmi nginx
# Remove unused images
docker image prune
# Remove all unused resources
docker system prune -a
# Show image sizes
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Building Images with Dockerfile
# Example Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
# Build image
docker build -t myapp:1.0 .
# Push to Docker Hub
docker tag myapp:1.0 username/myapp:1.0
docker push username/myapp:1.0
Volume Management
Volumes ensure data persists even when containers are removed.
# Create a volume
docker volume create database-data
# List volumes
docker volume ls
# Mount volume to container
docker run -d -v database-data:/var/lib/mysql mysql:8.0
# Bind mount (host directory)
docker run -d -v /root/data:/app/data nginx
# Remove a volume
docker volume rm database-data
# Remove unused volumes
docker volume prune
# Volume details
docker volume inspect database-data
Networking Basics
# List networks
docker network ls
# Create custom network
docker network create app-network
# Connect containers to network
docker run -d --network app-network --name web nginx
docker run -d --network app-network --name db mysql:8.0
# Containers on same network can reach each other by name
# From web container: ping db
# Network details
docker network inspect app-network
# Remove network
docker network rm app-network
Practical Examples
Nginx Web Server
docker run -d \
--name nginx-web \
-p 80:80 \
-v /var/www/html:/usr/share/nginx/html:ro \
--restart unless-stopped \
nginx:alpine
MySQL Database
docker run -d \
--name mysql-db \
-e MYSQL_ROOT_PASSWORD=strong_password \
-e MYSQL_DATABASE=myapp \
-v mysql-data:/var/lib/mysql \
-p 3306:3306 \
--restart unless-stopped \
mysql:8.0
WordPress + MySQL
# Create network
docker network create wordpress-net
# Start MySQL
docker run -d \
--name wordpress-db \
--network wordpress-net \
-e MYSQL_ROOT_PASSWORD=root_pass \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wp_user \
-e MYSQL_PASSWORD=wp_pass \
-v wp-db-data:/var/lib/mysql \
mysql:8.0
# Start WordPress
docker run -d \
--name wordpress \
--network wordpress-net \
-e WORDPRESS_DB_HOST=wordpress-db \
-e WORDPRESS_DB_USER=wp_user \
-e WORDPRESS_DB_PASSWORD=wp_pass \
-e WORDPRESS_DB_NAME=wordpress \
-p 8080:80 \
wordpress:latest
For managing multiple containers, use Docker Compose. A docker-compose.yml file lets you start all services with a single command.
Conclusion
Docker is an indispensable tool for modern application deployment. Once you master the basics, move on to Docker Compose for multi-service management. On REXE servers, Docker lets you run applications in an isolated, secure, and portable manner.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.