Docker is a platform that allows you to develop, ship, and run applications inside containers. Containers are lightweight, portable, and ensure consistency across multiple development, testing, and production environments.
- Windows and Mac: Install Docker Desktop from Docker's official website.
- Linux:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
- Check Docker Version:
docker --version
- Pull an Image:
docker pull <image_name>
- Run a Container:
docker run -it <image_name> /bin/bash
- List Running Containers:
docker ps
- List All Containers:
docker ps -a
- Stop a Container:
docker stop <container_id>
- Remove a Container:
docker rm <container_id>
- Remove an Image:
docker rmi <image_name>
A Dockerfile is a script containing a series of instructions on how to build a Docker image.
Example Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# 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"]- Build an Image:
docker build -t <image_name> .
- Run a Container from an Image:
docker run -p 4000:80 <image_name>
Docker Compose is a tool for defining and running multi-container Docker applications.
Example docker-compose.yml:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
redis:
image: "redis:alpine"- Run Docker Compose:
docker-compose up
- List Networks:
docker network ls
- Create a Network:
docker network create <network_name>
- Connect a Container to a Network:
docker network connect <network_name> <container_id>
Volumes are used to persist data generated by and used by Docker containers.
- Create a Volume:
docker volume create <volume_name>
- List Volumes:
docker volume ls
- Run a Container with a Volume:
docker run -v <volume_name>:/path/in/container <image_name>
| Command | Description |
|---|---|
docker --version |
Check Docker version |
docker pull <image> |
Pull an image from Docker Hub |
docker build -t <image_name> . |
Build an image from a Dockerfile |
docker run -it <image_name> /bin/bash |
Run a container interactively |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker stop <container_id> |
Stop a container |
docker rm <container_id> |
Remove a container |
docker rmi <image_name> |
Remove an image |
docker-compose up |
Run Docker Compose |
docker volume create <volume_name> |
Create a volume |
docker network create <network_name> |
Create a network |
- Keep Dockerfile instructions ordered logically and layer them from less to more frequently changing.
- Use
.dockerignoreto avoid adding unnecessary files to the Docker image. - Keep images small by using slim or alpine base images.
- Use multi-stage builds to reduce image size and improve build times.
- Always specify explicit versions of base images to ensure consistency.