Docker and Docker Composer
Docker Overview
Docker is a powerful platform for developing, shipping, and running applications inside lightweight containers. Containers package an application and its dependencies, ensuring consistency across development, testing, and production environments.
Key Concepts
- Image: A read-only template with the application and its dependencies.
- Container: A runnable instance of an image.
- Dockerfile: A script defining the instructions to create a Docker image.
- Docker Compose: A tool to define and manage multi-container Docker applications using a
docker-compose.yml
file.
Basic Docker Commands
1. Pull an Image
The docker pull <image-name>:<tag>
command downloads (or “pulls”) a Docker image from a registry like Docker Hub to your local machine.
Example:
1
docker pull nginx:latest
This pulls the latest NGINX image from Docker Hub. After downloading, you can use this image to create containers.
Other Examples:
1
2
3
docker pull python:3.10 # Python 3.10
docker pull ubuntu:20.04 # Ubuntu 20.04
docker pull redis:6.2 # Redis 6.2
Using tags ensures your containers use specific, consistent versions across different environments.
2. Run a Container
1
docker run -d --name <container-name> <image-name>
Example:
1
docker run -d --name my_nginx nginx
This runs a new container named my_nginx
in detached mode (-d
) using the NGINX image.
3. Other Basic Commands
- List running containers:
docker ps
- Stop a container:
docker stop <container-id>
- Remove a container:
docker rm <container-id>
- List images:
docker images
- Remove an image:
docker rmi <image-id>
Dockerfile Basics
A Dockerfile is a file with instructions for building a Docker image.
Example Dockerfile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Use an official Node.js image as the base
FROM node:14
# Set the working directory in the container
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose port 3000
EXPOSE 3000
# Start the application
CMD ["npm", "start"]
To build an image from this Dockerfile:
1
docker build -t <image-name> .
Example:
1
docker build -t mynodeapp .
Docker Compose
Docker Compose simplifies managing multi-container applications with a single YAML configuration file.
Example docker-compose.yml
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=development
volumes:
- .:/app
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
ports:
- "5432:5432"
Common Docker Compose Commands:
- Build and Start Services:
docker-compose up --build
- Start Services in Detached Mode:
docker-compose up -d
- Stop Services:
docker-compose down
- View Logs:
docker-compose logs -f
- Rebuild and Restart a Service:
docker-compose up -d --build <service-name>
Summary of Key Docker and Docker Compose Commands
Command | Description |
---|---|
docker build -t <image_name> . |
Build an image from a Dockerfile |
docker run -d --name <name> <image> |
Run a container from an image |
docker ps |
List running containers |
docker stop <container_id> |
Stop a running container |
docker exec -it <name> /bin/bash |
Open a shell in a running container |
docker-compose up -d |
Start services defined in a Compose file |
docker-compose down |
Stop services and remove containers |
docker-compose ps |
List services in a Compose setup |
Docker and Docker Compose streamline application deployment through containerization, ensuring consistent and efficient application environments across various stages of development.
About Docker Hub
Docker Hub is a cloud-based registry where users can store, share, and discover container images, often called the “GitHub for Docker images.”
Key Features:
- Image Repository: A central place to store public or private Docker images.
- Official Images: Trusted, verified images for popular software (e.g., NGINX, MySQL), maintained by publishers.
- Community Images: A vast library of images contributed by the community.
- Automated Builds: Automatically build images from source repositories like GitHub.
- Image Tags: Specify and manage versions of an image for consistency.
Docker Hub Commands:
- Login:
docker login
- Pull an Image:
docker pull <repository>/<image>:<tag>
-
Push an Image:
1 2
docker tag <local-image-name> <username>/<repository>:<tag> docker push <username>/<repository>:<tag>
- Search for an Image:
docker search <image-name>
Best Practices:
- Use Official Images when possible, as they are secure and well-maintained.
- Set Up Automated Builds: Connect Docker Hub with a Git repository to automate image updates.
- Use Tags to manage versions and ensure consistent deployment.