๐ 1. Introduction: What is Docker?
Docker and Container Concept
Docker is a platform that allows applications to run in isolated environments called containers. Containers include all the dependencies required for the application to function and ensure seamless operation across different platforms.
Advantages of Containers:
- Platform-independent.
- Fast startup and shutdown.
- Lightweight compared to virtual machines.
- Easy to replicate and distribute.
๐ 2. Getting Started with Dockerfile
A Dockerfile is a script used to create Docker images. It includes instructions on which base image to use, how dependencies should be installed, and how the application should be run.
๐ Basic Dockerfile Example (Node.js Application)
# Using the official Node.js image as the base
FROM node:18-alpine
# Set the working directory
WORKDIR /app
# Copy dependencies and install them
COPY package.json .
RUN npm install
# Copy application files
COPY . .
# Start the application
CMD ["node", "server.js"]
๐ Key Components of Dockerfile
-
FROM
: Specifies the base image. -
WORKDIR
: Sets the working directory. -
COPY
: Copies files into the container. -
RUN
: Executes a command in the terminal (e.g.,npm install
). -
CMD
: Defines the command to run when the container starts.
๐ How to Build a Docker Image
To build a Docker image, run the following command:
docker build -t my-node-app .
This command creates an image named my-node-app
using the Dockerfile
in the current directory.
๐ 3. Managing Multiple Services with Docker Compose
Docker Compose is a tool for managing multiple services at once. For example, we can run a Node.js application alongside a PostgreSQL database.
๐ Example docker-compose.yml
File
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydatabase
โถ๏ธ Running the Application with Compose
docker-compose up -d
This command starts both the application and the database in the background (-d
flag).
๐ 4. Running and Managing Docker Containers
Key commands for running, stopping, and managing Docker containers:
๐ Listing Running and All Containers
docker ps # Lists running containers
docker ps -a # Lists all containers (running + stopped)
๐ Starting and Stopping a Container
docker start <container_id>
docker stop <container_id>
๐ Restarting a Container
docker restart <container_id>
๐ Removing a Container
docker rm <container_id>
๐งน Cleaning Up Unused Containers and Images
docker system prune -a
๐ Viewing Container Logs
docker logs <container_id>
๐ Debugging Inside a Container
docker exec -it <container_id> sh
This command allows you to access the shell inside a running container.
๐ Managing Container Networks
docker network ls # Lists existing networks
docker network create my_network # Creates a new network
docker network connect my_network <container_id> # Connects a container to a network
docker network disconnect my_network <container_id> # Removes a container from a network
๐ Conclusion and Summary
โ Docker is a container technology that simplifies software development and accelerates deployment.
โ Dockerfile enables applications to run in an isolated environment.
โ Docker Compose is a powerful tool for managing multiple services and is especially useful for microservices architectures.
โ Docker containers are flexible, easily scalable, and portable.
โ Learning basic container management commands allows developers to efficiently handle their applications.
๐ก Tip: Enhance your Docker workflow by exploring CI/CD integrations for automated deployments! ๐
Top comments (0)