๐ Mastering Docker: A Comprehensive Guide from Basics to Advanced ๐
Welcome to the Docker Mastery Guide! Whether you're new to Docker or a seasoned pro, this guide has something for everyone. Let's dive into the world of Docker, covering essential commands and concepts to help you master this powerful tool.
๐ฆ What is Docker?
Docker is an open platform for developing, shipping, and running applications. It allows you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
๐ ๏ธ Getting Started with Docker
1. Install Docker
First, you need to install Docker on your machine. Follow the official Docker installation guide to get started.
2. Docker Images
Docker images are read-only templates used to create containers. They provide the instructions for creating a Docker container.
Build an image from a Dockerfile:
bashCopy codedocker build -t image_name path_to_dockerfile
# Example
docker build -t myapp .
List all local images:
bashCopy codedocker images
Pull an image from Docker Hub:
bashCopy codedocker pull image_name:tag
# Example
docker pull nginx:latest
Remove a local image:
bashCopy codedocker rmi image_name:tag
# Example
docker rmi myapp:latest
Tag an image:
bashCopy codedocker tag source_image:tag new_image:tag
# Example
docker tag myapp:latest myapp:v1
Push an image to Docker Hub:
bashCopy codedocker push image_name:tag
# Example
docker push myapp:v1
Inspect details of an image:
bashCopy codedocker inspect image image_name:tag
# Example
docker inspect myapp:v1
3. Docker Containers
Containers are instances of Docker images that can be run and managed.
Run a container from an image:
bashCopy codedocker run container_name image_name
# Example
docker run myapp
Run a named container from an image:
bashCopy codedocker run --name container_name image_name:tag
# Example
docker run --name my_container myapp:v1
List all running containers:
bashCopy codedocker ps
Stop a running container:
bashCopy codedocker stop container_name_or_id
# Example
docker stop my_container
Start a stopped container:
bashCopy codedocker start container_name_or_id
# Example
docker start my_container
Remove a stopped container:
bashCopy codedocker rm container_name_or_id
# Example
docker rm my_container
4. Docker Volumes and Networks
Volumes and networks are used for data persistence and container communication.
Create a named volume:
bashCopy codedocker volume create volume_name
# Example
docker volume create my_volume
List all volumes:
bashCopy codedocker volume ls
Inspect details of a volume:
bashCopy codedocker volume inspect volume_name
# Example
docker volume inspect my_volume
Remove a volume:
bashCopy codedocker volume rm volume_name
# Example
docker volume rm my_volume
Run a container with a volume (mount):
bashCopy codedocker run --name container_name -v volume_name:/path/in/container image_name:tag
# Example
docker run --name my_container -v my_volume:/app/data myapp:v1
Run a container with port mapping:
bashCopy codedocker run --name container_name -p host_port:container_port image_name
# Example
docker run --name my_container -p 8080:80 myapp
๐ Advanced Docker Techniques
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications.
Create and start containers defined in a docker-compose.yml file:
bashCopy codedocker-compose up
Stop and remove containers defined in a docker-compose.yml file:
bashCopy codedocker-compose down
Build or rebuild services:
bashCopy codedocker-compose build
View logs for services:
bashCopy codedocker-compose logs
Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image.
Basic Dockerfile example:
dockerfileCopy code# Use an official Node.js runtime as a base image
FROM node:20-alpine
# Set the working directory to /app
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the current directory contents to the container at /app
COPY . .
# Expose port 8080 to the outside world
EXPOSE 8080
# Define environment variable
ENV NODE_ENV=production
# Run app.js when the container launches
CMD ["node", "app.js"]
By following this guide, youโll be well on your way to mastering Docker. Happy Dockering! ๐ณ
Subscribe to my newsletter
Read articles from Mohit Kachhwaha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mohit Kachhwaha
Mohit Kachhwaha
DevOps Engineer | Automating Infrastructure and Deployment with AWS, Kubernetes, and Jenkins | Proficient in Shell Scripting and Docker & familiar with MERN Stack *currently looking for a DevOps Internship/Job. Trying to be better everyday ๐