Table of contents
What is Docker?
Docker is an open-source platform that simplifies the process of developing, shipping, and running applications by using containerization. It allows developers to package applications and their dependencies into lightweight, portable containers that can run consistently across various environments.
What Problem Does Docker Solve?
One of the main issues developers face is the "it works on my machine" problem, where an application behaves differently in development, testing, and production environments. Docker addresses this by ensuring that the application and its dependencies are encapsulated in a container, providing a consistent environment across all stages of development.
Docker Containers
Docker containers are lightweight, standalone, executable packages that include everything needed to run a piece of software, including the code, runtime, libraries, and system tools. Containers are isolated from each other and the host system, which makes them portable and ensures consistency.
Docker Installation and Setup
To get started with Docker, you need to install Docker Engine. The installation process varies depending on your operating system:
Windows: Download Docker Desktop for Windows from the official Docker website and follow the installation instructions.
macOS: Download Docker Desktop for Mac and follow the installation guide.
Linux: Use the package manager specific to your distribution to install Docker.
After installation, you can verify the setup by running:
docker --version
Docker CLI Introduction
The Docker Command Line Interface (CLI) is used to interact with the Docker daemon. Some fundamental commands include:
docker run
: Run a containerdocker ls
: List running containersdocker stop
: Stop a running containerdocker build
: Build an image from a Dockerfiledocker pull
: Pull an image from a registrydocker push
: Push an image to a registry
Docker Images vs. Containers
Docker Images: Read-only templates used to create containers. They are built from a set of instructions in a Dockerfile.
Docker Containers: Instances of Docker images that are running as separate entities. They are created, started, stopped, moved, and deleted using Docker commands.
Port Mapping
Port mapping is a technique to expose a container's port to the host system. For example, to run a container and map its internal port 80 to the host's port 8080, use:
docker run -it -p 8080:80 <image-name>
Environment Variables
Environment variables can be passed to containers to configure their behavior. This is useful for setting parameters like database connections, API keys, etc.
docker run -it -p 8080:80 -e key="value" <image-name>
Containerizing a Node.js Server with Docker
Here's a basic example of how to containerize a Node.js server:
Create a
Dockerfile
:FROM node:14 COPY package.json package.json COPY package-lock.json package-lock.json Run npm install ENTRYPOINT ["node", "server.js"]
Build the Docker image:
docker build -t my-node-app .
Run the container:
docker run -it -p 3000:3000 my-node-app
Publishing Custom Images to hub.docker.com
To share your custom Docker images, you can push them to Docker Hub by first make an account on hub.docker.com :
Tag your image:
docker tag my-node-app <username>/my-node-app
Log in to Docker Hub:
docker login
Push the image:
docker push <username>/my-node-app
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With a simple YAML file, you can configure your application's services, networks, and volumes. Here's a basic example:
version: '3'
services:
web:
image: my-node-app
ports:
- "3000:3000"
redis:
image: "redis:alpine"
To start your application with Docker Compose, run:
docker-compose up
Conclusion
Learning Docker has been an enriching experience, offering a new perspective on how to develop, deploy, and manage applications. Its ability to provide consistent environments, coupled with the power of containerization, makes Docker an invaluable tool in modern software development. Whether you're running a simple Node.js server or a complex multi-service application, Docker can simplify your workflow and enhance your deployment process.