Getting Started with Docker: A Complete Guide
Everything you need to know to go from zero to productive with Docker containers.
Getting Started with Docker: A Complete Guide
Docker has revolutionized how we deploy and manage applications. Whether you're setting up a homelab or building production infrastructure, understanding Docker is essential.
What is Docker?
Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings.
Prerequisites
Before we begin, make sure you have:
- A Linux server (Ubuntu, Debian, or similar)
- Root or sudo access
- Basic command-line knowledge
Installing Docker
Here's how to install Docker on Ubuntu:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add the Docker repository and install:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Your First Container
Let's run a simple container:
docker run hello-world
This command downloads a test image and runs it in a container. If everything is working correctly, you'll see a welcome message.
Project Structure
A typical Docker project might look like this:
my-docker-project/
├── docker/
│ ├── Dockerfile
│ ├── docker-compose.yml
│ └── .env
├── app/
│ ├── index.js
│ └── package.json
├── config/
│ ├── nginx.conf
│ └── traefik.yml
└── README.md
Docker Compose
Docker Compose is a tool for defining and running multi-container applications. Here's a simple example:
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
Save this as docker-compose.yml and run:
docker compose up -d
Essential Commands
Here are the Docker commands you'll use most often:
| Command | Description |
|---------|-------------|
| docker ps | List running containers |
| docker images | List downloaded images |
| docker logs <container> | View container logs |
| docker exec -it <container> /bin/sh | Enter a container |
| docker stop <container> | Stop a container |
| docker rm <container> | Remove a container |
Best Practices
- Use official images - Always prefer official images from Docker Hub
- Pin versions - Don't use
latesttag in production - Use volumes for data - Never store important data in containers
- Set resource limits - Prevent containers from consuming all resources
- Use networks - Isolate containers with custom networks
Common Pitfalls
Warning: Never run commands like
docker run --rm -v /:/host alpine rm -rf /host- it could delete your entire filesystem! Always be careful with volume mounts and root access.
Next Steps
Now that you have Docker running, consider exploring:
- Portainer - A web UI for managing Docker
- Traefik - A reverse proxy for Docker containers
- Watchtower - Automatic container updates
Stay tuned for more Docker tutorials and homelab guides!
Was this article helpful?