How to Get Started with Docker for Beginners
What is Docker?
Docker is an open-source platform for automating the deployment, scaling, and management of applications using container technology. Containers package an application and its dependencies into a single, lightweight unit that can run consistently across different computing environments.
Key Docker Concepts
Concept | Description |
---|---|
Image | A read-only template containing instructions for creating a Docker container. |
Container | An instance of an image running as an isolated process. |
Dockerfile | A text file with instructions to build a Docker image. |
Docker Hub | A cloud-based registry where Docker images are stored and shared. |
Volume | A persistent storage mechanism for containers. |
Network | Allows containers to communicate within and outside the Docker host. |
Installing Docker
Supported Platforms: Windows, macOS, Linux
Windows & macOS
- Download Docker Desktop: https://www.docker.com/products/docker-desktop
- Install the application following the prompts.
- Start Docker Desktop and ensure it is running.
Linux (Ubuntu example)
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
(Optional) Add your user to the docker group:
sudo usermod -aG docker $USER
Basic Docker Commands
Command | Description |
---|---|
docker --version |
Check Docker version |
docker pull <image> |
Download image from Docker Hub |
docker images |
List downloaded images |
docker run <image> |
Run a container from an image |
docker ps |
List running containers |
docker ps -a |
List all containers (including stopped) |
docker stop <container> |
Stop a running container |
docker rm <container> |
Remove a container |
docker rmi <image> |
Remove an image |
Running Your First Container
Run a simple hello-world container:
docker run hello-world
Expected output: Docker downloads the image, runs it, and prints a hello message.
Working with Containers
- Pull an Image
sh
docker pull nginx - Run the Image
sh
docker run -d -p 8080:80 --name webserver nginx -d
: Detached mode (runs in background)-p 8080:80
: Map host port 8080 to container port 80-
--name webserver
: Name the running container -
Visit http://localhost:8080 in your browser to see the default Nginx page.
-
Stop and Remove the Container
sh
docker stop webserver
docker rm webserver
Building Your Own Docker Image
- Create a Simple Application
app.py
:
python
print("Hello from Docker!")
- Write a Dockerfile
“`
# Use official Python image
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Copy source code
COPY app.py .
# Command to run
CMD [“python”, “app.py”]
“`
-
Build the Image
sh
docker build -t my-python-app . -
Run the Container
sh
docker run my-python-app
Using Docker Compose
Docker Compose manages multi-container applications with a YAML file.
-
Example
docker-compose.yml
:
yaml
version: "3"
services:
web:
image: nginx
ports:
- "8080:80"
redis:
image: redis:alpine -
Start the Application
sh
docker compose up -d -
View Running Services
sh
docker compose ps -
Stop the Application
sh
docker compose down
Data Persistence with Volumes
- Create a volume:
sh
docker volume create mydata - Use a volume with a container:
sh
docker run -d -v mydata:/data busybox
Storage Type | Use Case | Data Persist After Container? |
---|---|---|
Bind Mount | Share host files with container | Yes |
Volume | Managed by Docker | Yes |
tmpfs | Ephemeral, in-memory storage | No |
Networking Basics
- List networks:
sh
docker network ls - Create a custom network:
sh
docker network create mynet - Run containers on the same network:
sh
docker run -d --name db --network mynet postgres
docker run -d --name app --network mynet my-python-app
Best Practices
Practice | Description |
---|---|
Use official/base images | Leverage trusted images from Docker Hub |
Keep images small | Use slim/Alpine images, remove unnecessary packages |
Use .dockerignore |
Exclude files from build context |
Tag images properly | Use semantic versioning tags |
Clean up unused resources | Regularly run docker system prune |
Troubleshooting Common Issues
Issue | Solution |
---|---|
Permission denied on Docker sock | Add user to docker group, restart shell |
Container port not accessible | Check -p flag and firewall settings |
Image build errors | Check Dockerfile syntax and context |
Container exits immediately | Review container logs: docker logs <container> |
Useful Resources
0 thoughts on “How to Get Started with Docker for Beginners”