What is Docker?
Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. Containers ensure your application runs the same way regardless of the environment—be it your local machine, a server, or the cloud.
Why Use Docker?
Consistency Across Environments: "It works on my machine!" won't be a problem anymore.
Lightweight and Fast: Containers share the host OS kernel, making them faster and more efficient than virtual machines.
Simplified Deployment: Docker containers bundle everything needed to run your application, reducing deployment complexity.
Key Docker Concepts
Image: A read-only template used to create containers. Think of it as a blueprint.
Container: A running instance of an image. It's your isolated application environment.
Dockerfile: A script that defines how to build an image.
Docker Hub: A cloud-based registry for storing and sharing Docker images.
Installing Docker
Docker can be installed on most operating systems:
Linux: Follow Docker's official Linux installation guide.
Windows/Mac: Download and install Docker Desktop from Docker's official site.
Verify installation by running:
docker --version
Docker Commands Cheat Sheet
Here are some basic Docker commands to get you started:
Command | Description |
docker --version | Check Docker version. |
docker run <image> | Run a container from an image. |
docker ps | List running containers. |
docker ps -a | List all containers (running or stopped). |
docker stop <container_id> | Stop a running container. |
docker rm <container_id> | Remove a container. |
docker rmi <image_id> | Remove an image. |
docker images | List all images. |
docker exec -it <container_id> bash | Access a running container. |
Your First Docker Container
Step 1: Pull an Image
Images are pre-built templates stored in Docker Hub. Pull the official Nginx image:
docker pull nginx
Step 2: Run a Container
Run the Nginx container to serve a simple web page:
docker run -d -p 8080:80 --name my-nginx nginx
-d
: Run the container in detached mode.-p 8080:80
: Map port 8080 on your machine to port 80 in the container.--name my-nginx
: Assign a custom name to your container.
Visit http://localhost:8080
in your browser to see the Nginx welcome page.
Step 3: Stop and Remove the Container
Stop the running container:
docker stop my-nginx
Remove the container:
docker rm my-nginx
Building Your Own Image
Step 1: Create a Dockerfile
A Dockerfile
is a set of instructions to build a Docker image. Here's a simple example:
# Use the official Node.js image as the base
FROM node:16
# Set the working directory
WORKDIR /app
# Copy the application code
COPY . .
# Install dependencies
RUN npm install
# Expose the application port
EXPOSE 3000
# Run the application
CMD ["node", "index.js"]
Step 2: Build the Image
Build an image named my-node-app
:
docker build -t my-node-app .
Step 3: Run the Image as a Container
Run the container from your custom image:
docker run -d -p 3000:3000 --name node-container my-node-app
Your Node.js application will now be accessible at http://localhost:3000
.
Real-World Use Cases
Local Development: Run databases (e.g., MongoDB, PostgreSQL) in containers without complex installations.
CI/CD Pipelines: Integrate Docker with Jenkins, GitHub Actions, or GitLab CI for seamless builds and deployments.
Microservices Architecture: Use Docker to run isolated microservices for modular, scalable applications.
Next Steps
Explore Docker Compose for managing multi-container applications.
Learn Kubernetes to orchestrate and scale Docker containers.
Dive into Docker networking and volumes for advanced use cases.