🐳 Docker Guide
🚀 Introduction
Docker is a powerful platform for developing, shipping, and running applications using containers. Containers ensure that your applications run consistently across different environments.
🛠 Prerequisites
- A computer running Linux, macOS, or Windows
- Basic command-line knowledge
When using Docker, several components are typically installed to effectively manage and utilize containers:
- Docker Engine
- Docker Compose
- Docker CLI (Command Line Interface)
- Docker Registry
- Docker Toolbox (for older systems or Windows/Mac OS)
- Docker Swarm (optional)
🔧 Installing Docker
On Linux
- Update your package list and install dependencies:
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
- Add Docker's official GPG key and set up the stable repository:
sudo mkdir -p /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
- Install Docker Engine:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Verify the installation:
docker --version
On macOS & Windows
- Download and install Docker Desktop from the Docker website.
- Follow the instructions to complete the installation.
🐳 Docker Basics manipulation
Running Your Container
Run a simple Docker container to verify the installation:
docker run hello-world
Pulling an Image
Pull an image from Docker Hub:
docker pull ubuntu
Listing Images
List all Docker images on your system:
docker images
Running a Container
Run a container from an image:
docker run -it ubuntu
This command starts a new Ubuntu container and provides an interactive terminal.
Listing Containers
List running containers:
docker ps
List all containers, including stopped ones:
docker ps -a
Stopping a Container
Stop a running container:
docker stop <container_id>
Removing a Container
Remove a stopped container:
docker rm <container_id>
Removing an Image
Remove an image from your system:
docker rmi <image_id>
📄 Dockerfiles
Creating a Dockerfile
A Dockerfile
is a script that contains instructions for building a Docker image. Here's an example:
# Use an official Python runtime as a parent image
FROM python:3.8-slim-buster
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Building an Image
Build a Docker image from a Dockerfile:
docker build -t my-python-app .
Running an Image
Run the newly built image:
docker run -p 4000:80 my-python-app
This command maps port 4000 on the host to port 80 in the container.
📦 Docker Compose
Creating a docker-compose.yml
File
Docker Compose is a tool for defining and running multi-container Docker applications. Here's an example docker-compose.yml
file:
version: '3'
services:
web:
image: my-python-app
build: .
ports:
- "4000:80"
redis:
image: "redis:alpine"
Running Docker Compose
Use Docker Compose to start your application:
docker-compose up
Stopping Docker Compose
Stop the application:
docker-compose down
🐳 Docker Must-Know Commands for Debugging & Container Management
# List running containers
docker ps
# Follow logs from a running container
docker logs -f container_name
# Follow the last 200 lines of logs from a running container
docker logs -f -n 200 container_name
# Execute a command inside a running container
docker exec -it container_name /bin/bash
# Stop a running container
docker stop container_name
# Remove a stopped container
docker rm container_name
# List all containers (including stopped ones)
docker ps -a
# Remove all stopped containers
docker container prune
# List all Docker images
docker images
# Remove a Docker image
docker rmi image_name_or_id
🎉 Conclusion
There is no reason not to use Docker