How to Get Started with Docker for Beginners
What is Docker
Docker is a platform that enables developers to package applications and their dependencies into containers. Containers are lightweight, portable, and consistent across different environments, making development, testing, and deployment more efficient.
Docker Concepts and Terminology
Term | Description |
---|---|
Image | A read-only template with instructions for creating a container. |
Container | A runnable instance of an image. |
Dockerfile | Text file with instructions to build a Docker image. |
Docker Hub | Public registry to store and share Docker images. |
Volume | Persistent storage for containers. |
Network | Isolated virtual networks for containers to communicate. |
Installing Docker
Windows and macOS:
- Download Docker Desktop from Docker’s website.
- Run the installer and follow the prompts.
- After installation, launch Docker Desktop.
Linux (Ubuntu example):
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
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
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
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
To verify installation:
docker --version
Running Your First Container
Start a simple container using an official image:
docker run hello-world
This downloads the hello-world
image and runs it in a container, printing a test message.
Docker Images and Containers
- Listing Images:
bash
docker images - Listing Running Containers:
bash
docker ps - Listing All Containers (including stopped):
bash
docker ps -a - Stopping and Removing Containers:
bash
docker stop <container_id>
docker rm <container_id> - Removing Images:
bash
docker rmi <image_id>
Building a Docker Image
Create a simple Dockerfile for a Python application:
Directory Structure:
myapp/
├─ app.py
└─ Dockerfile
app.py:
print("Hello from Docker!")
Dockerfile:
FROM python:3.10-slim
COPY app.py .
CMD ["python", "app.py"]
Building the Image:
docker build -t my-python-app .
Running the Container:
docker run my-python-app
Working with Docker Hub
- Login:
bash
docker login - Tagging an Image:
bash
docker tag my-python-app username/my-python-app:latest - Pushing to Docker Hub:
bash
docker push username/my-python-app:latest - Pulling an Image:
bash
docker pull username/my-python-app:latest
Docker Volumes
Docker volumes persist data beyond the container lifecycle.
- Create and Mount a Volume:
bash
docker volume create mydata
docker run -d -v mydata:/data busybox - List Volumes:
bash
docker volume ls
Networking in Docker
- Bridge Network (default): Containers can communicate on the same host.
- Custom Network:
bash
docker network create mynetwork
docker run -d --name app1 --network mynetwork busybox sleep 3600
docker run -d --name app2 --network mynetwork busybox sleep 3600
docker exec -it app1 ping app2
Common Docker Commands Summary
Command | Description |
---|---|
docker run IMAGE | Run a container from an image |
docker ps | List running containers |
docker images | List images |
docker stop CONTAINER | Stop a running container |
docker rm CONTAINER | Remove a container |
docker rmi IMAGE | Remove an image |
docker build -t NAME . | Build a new image from Dockerfile |
docker pull NAME | Download an image from a registry |
docker push NAME | Upload an image to a registry |
docker exec -it CONTAINER COMMAND | Run a command in a running container |
Best Practices for Beginners
- Use official images as base images where possible.
- Keep images lean by removing unnecessary files in Dockerfile.
- Use
.dockerignore
to exclude files from builds. - Avoid running applications as root inside containers.
- Tag images with meaningful version numbers.
Example: Running a Web Application
Dockerfile for Flask App:
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
requirements.txt:
flask
app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello from Docker Flask!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Build and Run:
docker build -t flask-app .
docker run -p 5000:5000 flask-app
Access the app at http://localhost:5000.
Docker Compose Basics
Docker Compose manages multi-container applications using a docker-compose.yml
file.
Sample docker-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
Usage:
docker compose up
This command builds and starts all services defined in the file.
Troubleshooting Tips
- Use
docker logs <container>
to view container output. - Remove unused resources with
docker system prune
. - Restart Docker Desktop if containers fail to start due to resource errors.
- Use
docker inspect
for detailed container and image information.
0 thoughts on “How to Get Started with Docker for Beginners”