Getting Started with Docker: A Complete Beginnerâs Guide
Introduction
If youâre a developer, DevOps engineer, or just someone interested in deploying applications efficiently, Docker is a must-have tool in your toolbox. Docker simplifies how you build, ship, and run applications by using containersâlightweight, portable, and self-sufficient packages that include everything your software needs to run.
In this guide, youâll learn what Docker is, how it works, and how to get started using itâstep by step.
What is Docker?
Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight, portable containers.
A container packages your application and all its dependencies (libraries, binaries, configurations) so it runs reliably in any environmentâwhether itâs your laptop, a staging server, or a production environment in the cloud.
Why Use Docker?
Consistency: Runs the same everywhereâno more âIt works on my machineâ issues.
Isolation: Each container is isolated from others, preventing dependency conflicts.
Portability: Containers can run on any system that supports Docker.
Efficiency: Uses less overhead compared to full virtual machines.
Fast Deployment: Launch apps in seconds.
Installing Docker
1. Install Docker Desktop
Windows / macOS:
- Go to the Docker website
- Download Docker Desktop for your OS
- Run the installer and follow the steps
- After installation, verify by running:
docker --version
Linux (Ubuntu example):
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl start docker
sudo systemctl enable docker
To run Docker as a non-root user:
sudo usermod -aG docker $USER
Your First Docker Command
Open a terminal and run:
docker run hello-world
This does the following:
- Downloads the
hello-world
image (if not already downloaded) - Runs it in a new container
- Outputs a confirmation message
Youâve just run your first Docker container!
Docker Concepts
Term | Description |
---|---|
Image | A snapshot or blueprint of your application (read-only). |
Container | A running instance of an image. |
Dockerfile | A text file with instructions to build a Docker image. |
Docker Hub | A registry of pre-built images (like GitHub for containers). |
Building Your Own Docker Image
Letâs create a simple Python app and containerize it.
1. Create a new folder:
mkdir my-python-app && cd my-python-app
2. Create app.py
:
# app.py
print("Hello from Docker!")
3. Create Dockerfile
:
# Use official Python base image
FROM python:3.10-slim
# Set working directory
WORKDIR /app
# Copy current directory contents into the container
COPY . .
# Run the application
CMD ["python", "app.py"]
4. Build the Docker image:
docker build -t my-python-app .
5. Run your container:
docker run my-python-app
You should see:
Hello from Docker!
Docker Commands Cheat Sheet
Command | Description |
---|---|
docker ps |
List running containers |
docker ps -a |
List all containers |
docker images |
List downloaded images |
docker stop <id> |
Stop a running container |
docker rm <id> |
Remove a container |
docker rmi <image> |
Remove an image |
docker logs <id> |
Show logs for a container |
docker exec -it <id> bash |
Open a shell inside container |
Docker Compose (Multi-container Apps)
For complex apps with databases, caches, and frontends, use Docker Compose.
Example: docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Start all services with:
docker-compose up
Clean Up Docker
To remove unused containers, images, and networks:
docker system prune
Pro Tips
- Use
.dockerignore
like.gitignore
to exclude unnecessary files. - Tag your images (
my-app:latest
) to version them. - Always scan your images for vulnerabilities using tools like
docker scan
.
Conclusion
Docker is a game-changing tool for software development, deployment, and DevOps workflows. With containers, you can simplify your development environment, test more efficiently, and deploy apps more reliably.
Now that youâve built and run your first Docker container, youâre well on your way to mastering containerization.
What Next?
- Learn about Docker volumes for data persistence
- Explore Docker networking for inter-container communication
- Push your images to Docker Hub
- Explore Kubernetes for container orchestration