Docker Tutorial: Complete Setup to Docker Compose
Deploying code reliably requires consistent runtime environments across every machine. This docker tutorial walks through configuring the official repository, containerizing applications, persisting data, and managing multi-container stacks. By the end, you will have a production-ready workflow running on modern Linux.
Before you start
- Ubuntu 24.04 or modern Debian-based Linux system
- A user account with sudo privileges
- Basic familiarity with the bash command line
Installing Docker Engine the Right Way
Many default Linux repositories distribute older packages that miss current features and plugins. Registering the official repository ensures your system receives supported packages such as docker-ce and docker-compose-plugin.
Run the commands below to remove conflicting packages, register the official repository key, and install the daemon components. Once installed, verify that the daemon is active and responsive.
sudo apt-get remove -y docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc 2>/dev/null || true
sudo apt-get update && sudo apt-get install -y ca-certificates curl && sudo install -m 0755 -d /etc/apt/keyrings && sudo curl -fsSL https://download.docker.com/linux/$(. /etc/os-release && echo "$ID")/gpg -o /etc/apt/keyrings/docker.asc && sudo chmod a+r /etc/apt/keyrings/docker.asc
{ echo "Types: deb"; echo "URIs: https://download.docker.com/linux/$(. /etc/os-release && echo "$ID")"; echo "Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")"; echo "Components: stable"; echo "Architectures: $(dpkg --print-architecture)"; echo "Signed-By: /etc/apt/keyrings/docker.asc"; } | sudo tee /etc/apt/sources.list.d/docker.sources
sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
pgrep -x dockerd >/dev/null || (service docker start 2>/dev/null || nohup dockerd > /tmp/dockerd.log 2>&1 &) && sleep 3 && docker version
Output from our test run
$ sudo apt-get remove -y docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc 2>/
dev/null || true
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
$ sudo apt-get update && sudo apt-get install -y ca-certificates curl && sudo install -m 0755 -d /etc/apt/keyrin
gs && sudo curl -fsSL https://download.docker.com/linux/$(. /etc/os-release && echo "$ID")/gpg -o /etc/apt/keyri
ngs/docker.asc && sudo chmod a+r /etc/apt/keyrings/docker.asc
Get:1 http://deb.debian.org/debian stable InRelease [140 kB]
Get:2 http://deb.debian.org/debian stable-updates InRelease [47.3 kB]
Get:3 http://deb.debian.org/debian-security stable-security InRelease [43.4 kB]
Get:4 https://dl.google.com/linux/chrome-stable/deb stable InRelease [2548 B]
Get:5 https://deb.nodesource.com/node_22.x nodistro InRelease [12.1 kB]
Get:6 https://dl.google.com/linux/chrome-stable/deb stable/main amd64 Packages [1406 B]
Get:7 https://deb.nodesource.com/node_22.x nodistro/main amd64 Packages [12.8 kB]
Get:8 http://deb.debian.org/debian stable/main amd64 Packages [9678 kB]
Get:9 http://deb.debian.org/debian stable-updates/main amd64 Packages [1108 B]
Get:10 http://deb.debian.org/debian-security stable-security/main amd64 Packages [298 kB]
Fetched 10.2 MB in 2s (6210 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libcurl3t64-gnutls libcurl4t64
The following packages will be upgraded:
ca-certificates curl libcurl3t64-gnutls libcurl4t64Images vs Containers
Understanding the core distinction between images and containers is essential for container management. An image serves as a read-only blueprint constructed from layered filesystem changes.
A container is a running instance instantiated from an image. You can launch multiple isolated containers from a single image without modifying the underlying blueprint.
Running Your First Container
Launching a prebuilt web server demonstrates container isolation and network forwarding. The commands below run an nginx:alpine container in the background, inspect active workloads, and test endpoint access.
When mapping container ports to your host system, keep the ordering straight. The host port always comes first, followed by the target container port.
# Run Nginx detached on port 8080
docker run -d --name web-demo -p 8080:80 nginx:alpine
# List running containers
docker ps
# Verify the web server is answering on host port 8080
curl -s -I http://localhost:8080
# Stop and clean up the test container
docker stop web-demo && docker rm web-demo
Writing a Clean Dockerfile
Packaging your own service requires defining build steps in a Dockerfile. Starting from a lightweight base such as python:3.12-alpine keeps image footprint minimal.
The configuration shown below sets an isolated working directory, adds server.py, documents the listening port, and specifies the startup command using structured array formatting.
FROM python:3.12-alpine
WORKDIR /app
COPY server.py .
EXPOSE 5000
CMD ["python", "server.py"]
Building and Running a Custom Image
Creating custom images allows you to package dependencies and code together. The commands below generate a minimal Python script and build the image tagged as my-python-app.
Once built, run the container instance and send a test request to confirm that incoming connections log correctly inside the container output.
# Create minimal Python web server
mkdir -p /root/docker-demo && cd /root/docker-demo && cat << 'EOF' > server.py
from http.server import HTTPServer, BaseHTTPRequestHandler
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'Works on First Try!\n')
def log_message(self, format, *args):
print(f'[HTTP] {self.address_string()} - {format % args}', flush=True)
HTTPServer(('0.0.0.0', 5000), Handler).serve_forever()
EOF
# Write Dockerfile to disk
cat << 'EOF' > Dockerfile
FROM python:3.12-alpine
WORKDIR /app
COPY server.py .
EXPOSE 5000
CMD ["python", "server.py"]
EOF
# Build the custom image with BuildKit
docker build -t my-python-app .
# Run the custom container detached
docker run -d --name app-instance -p 5000:5000 my-python-app
# Test custom server endpoint
curl -s http://localhost:5000
# View container standard output logs
docker logs app-instance
# Stop and remove the container
docker stop app-instance && docker rm app-instance
Persistent Storage with Volumes
Files written directly inside a container exist only within its temporary writable layer. When that container is removed, all unmounted data is permanently lost.
The commands below create a named volume called app-data and attach it to separate containers. This allows data written by an initial container to persist safely across container lifecycles.
# Create a managed Docker volume
docker volume create app-data
# Write data into the volume from an ephemeral container
docker run --rm -v app-data:/data alpine sh -c 'echo "Saved at $(date)" > /data/timestamp.txt'
# Read the persisted data from a separate container
docker run --rm -v app-data:/data alpine cat /data/timestamp.txt
# Show the host filesystem location of the volume
docker volume inspect app-data --format '{{ .Mountpoint }}'
Multi-Container Orchestration with Compose
Managing multiple services through individual command strings quickly becomes difficult as applications expand. Docker Compose allows you to describe services, image sources, and port bindings within a single declarative file.
The compose.yaml configuration below defines an Nginx web gateway alongside a redis:alpine caching layer on a shared internal network.
services:
web:
image: nginx:alpine
ports:
- "8080:80"
restart: unless-stopped
cache:
image: redis:alpine
restart: unless-stopped
Launching with Docker Compose
With a declarative file defined, managing the entire application stack requires only standard compose commands. Run the instructions below to launch all services in the background and confirm their running states.
When development is complete or configuration updates are needed, the down command cleanly halts and cleans up all running service containers.
# Write compose file
cd /root/docker-demo && cat << 'EOF' > compose.yaml
services:
web:
image: nginx:alpine
ports:
- "8080:80"
restart: unless-stopped
cache:
image: redis:alpine
restart: unless-stopped
EOF
# Start both services detached
docker compose up -d
# Verify status of compose services
docker compose ps
# Verify the composed web service is reachable
curl -s -I http://localhost:8080
# Stop and remove compose services cleanly
docker compose down
Wrap-up
You now have a clean foundation for installing the container engine, building custom images, persisting data with volumes, and coordinating multi-container stacks using modern Compose workflows.
FAQ
Why should I avoid installing the docker.io package from my Linux distribution?
Distribution packages like docker.io are often outdated and do not include official CLI extensions like docker-compose-plugin.
What happened to the standalone docker-compose command?
The older hyphenated tool reached end-of-life and has been replaced by the integrated docker compose CLI plugin.
What happens to data stored inside a container without a volume?
Any data stored without a mounted volume resides only in the temporary writable layer and is permanently erased when the container is deleted.