Introduction / Why This Matters
Docker is a containerization platform that allows you to package applications and their dependencies into isolated, portable environments—containers. On Linux, this is particularly convenient because Docker leverages built-in kernel capabilities (cgroups, namespaces). After completing this guide, you will be able to:
- Install and configure Docker Engine on your server or workstation.
- Run, stop, and remove containers.
- Work with images: search, download, and create your own.
- Understand the basic Dockerfile syntax for automating builds.
This is the foundation for further study of orchestration (Kubernetes) and CI/CD pipelines.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, etc.) with
sudoprivileges. - The system is 64-bit (x86_64/amd64 or arm64).
- You have an internet connection to download packages and images.
- (Optional) Basic understanding of the command line and package management (
apt,yum,dnf).
Step 1: Installing Docker Engine
Installing via the official repository is the most reliable method.
For Ubuntu/Debian:
# 1. Update the package index and install dependencies
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
# 2. Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# 3. Add the Docker repository to sources.list
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 4. Install Docker Engine, containerd, and Docker Compose (plugin)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
For CentOS/RHEL/Fedora:
# 1. Install yum-utils (for repository management)
sudo yum install -y yum-utils
# 2. Add the Docker repository
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 3. Install Docker Engine
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify the installation:
sudo docker version
The output should show both the client and server (Engine) with their versions.
Step 2: Configuring Permissions and First Check
By default, docker commands require sudo privileges. To run containers as a regular user, add your user to the docker group.
# Add the current user to the docker group
sudo usermod -aG docker $USER
# To apply changes, log out and log back in,
# or run the following in your current session:
newgrp docker
Now verify that everything works without sudo:
docker run hello-world
You should see a welcome message from Docker confirming the successful download of the hello-world image and container launch.
Step 3: Essential Commands for Working with Images
An image is a template (filesystem + metadata) for creating containers.
Search for an image on Docker Hub (the official repository)
docker search nginx
Key columns: NAME (official images are marked [OK]), DESCRIPTION, STARS.
Download an image locally
docker pull ubuntu:22.04
This command downloads the latest LTS Ubuntu 22.04 image. If no tag is specified, latest is used.
List local images
docker images
Note the columns: REPOSITORY, TAG, IMAGE ID, SIZE.
Remove an image
docker rmi ubuntu:22.04
If an image is used by a running container, it cannot be removed. Remove the container first.
Step 4: Running and Managing Containers
A container is a running instance of an image.
Run a container in interactive mode
docker run -it --name my-ubuntu ubuntu:22.04 /bin/bash
-it— interactive mode with TTY (terminal).--name— assigns a convenient name to the container (otherwise a random one is generated).- After the command, you will be inside the container's shell. Use
exitto leave.
Run a container in detached (daemon) mode
docker run -d --name web-server -p 8080:80 nginx:alpine
-d— detached mode (container runs in the background).-p 8080:80— port mapping: host:container. The web server inside the container listens on port 80, while externally it's accessible on port 8080 of your host.- To verify: open
http://localhost:8080in your browser.
View running containers
docker ps
Key columns: CONTAINER ID, NAMES, STATUS, PORTS.
To see all containers (including stopped ones):
docker ps -a
Stop and remove a container
# Stop the container (preserves the filesystem)
docker stop web-server
# Start a stopped container
docker start web-server
# Remove a stopped container
docker rm web-server
# Force remove a running container (data will be lost!)
docker rm -f web-server
View container logs
docker logs web-server
Add -f or --follow to stream logs in real time.
Step 5: Creating Your Own Image via Dockerfile
A Dockerfile is a text file with instructions for building an image.
- Create an empty directory and a
Dockerfileinside it:mkdir my-nginx && cd my-nginx cat > Dockerfile << 'EOF' # Use the official lightweight Alpine Linux image FROM alpine:3.18 # Install nginx RUN apk add --no-cache nginx # Copy a custom config (if available) or use the default # COPY nginx.conf /etc/nginx/nginx.conf # Create a directory for the website files RUN mkdir -p /var/www/html # Copy the local index.html file into the image COPY index.html /var/www/html/index.html # Expose port 80 EXPOSE 80 # Run nginx in the foreground CMD ["nginx", "-g", "daemon off;"] EOF - Create a simple
index.htmlin the same directory:echo "<h1>Hello from my custom Docker image!</h1>" > index.html - Build the image:
docker build -t my-custom-nginx:latest .-t— tag (name:version) for the image..— build context (current directory).
- Run a container from the new image:
docker run -d -p 8081:80 --name my-app my-custom-nginx:latest
Openhttp://localhost:8081— you will see your custom header.
Verification
You have successfully mastered the basic Docker workflow if you can:
- Install Docker on a clean Linux system.
- Run a container from the
nginximage and see the page in a browser. - Create a
Dockerfile, build an image from it, and run a container. - Stop and remove all created resources:
docker stop my-ubuntu web-server my-app docker rm my-ubuntu web-server my-app docker rmi ubuntu:22.04 nginx:alpine my-custom-nginx:latest
Troubleshooting
Error: Got permission denied while trying to connect to the Docker daemon socket...
Cause: The current user is not in the docker group or the session hasn't been reloaded.
Solution: Run sudo usermod -aG docker $USER, then log out of the terminal and log back in (or run newgrp docker).
Container exits immediately after starting
Cause: The main process inside the container has finished (e.g., docker run ubuntu echo "test").
Solution: For long-running containers (web servers, databases), the main process must run in the foreground (like nginx -g 'daemon off;'). Use docker logs <container_id> to see the termination reason.
Running out of disk space
Cause: Accumulation of images, containers, volumes. Solution: Clean up unused resources:
# Remove all stopped containers, unused networks, and dangling images
docker system prune
# Remove ALL unused data (be careful!)
docker system prune -a --volumes