How to Use Docker for Microservices: Simple Guide
Use
Docker to package each microservice into its own container, isolating dependencies and environments. Then use Docker Compose or orchestration tools to run and manage these containers together as a system.Syntax
Each microservice needs a Dockerfile to define its container image. Use docker build to create the image and docker run to start a container. Use docker-compose.yml to define and run multiple microservices together.
Dockerfile: Instructions to build a container image.docker build -t imagename .: Builds the image.docker run -p hostPort:containerPort imagename: Runs the container.docker-compose.yml: Defines multiple services and their settings.docker-compose up: Starts all defined services.
Dockerfile
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "index.js"]
Example
This example shows two simple microservices, each with its own Dockerfile, and a docker-compose.yml to run them together.
yaml
## Service 1 Dockerfile (service1/Dockerfile) FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3001 CMD ["node", "index.js"] ## Service 2 Dockerfile (service2/Dockerfile) FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3002 CMD ["node", "index.js"] ## docker-compose.yml version: '3.8' services: service1: build: ./service1 ports: - "3001:3001" service2: build: ./service2 ports: - "3002:3002"
Output
Creating network "default" with the default driver
Building service1
Step 1/6 : FROM node:18-alpine
...
Successfully built <image_id>
Building service2
Step 1/6 : FROM node:18-alpine
...
Successfully built <image_id>
Creating service1_1 ... done
Creating service2_1 ... done
Common Pitfalls
Common mistakes include:
- Not exposing the correct ports in Dockerfiles or Compose files, causing services to be unreachable.
- Sharing state or files between containers without volumes, leading to data loss.
- Not isolating dependencies, causing version conflicts.
- Running all microservices in one container, which defeats isolation benefits.
Always build and run each microservice in its own container and use docker-compose or orchestration for coordination.
Dockerfile
## Wrong: Running multiple microservices in one Dockerfile FROM node:18-alpine WORKDIR /app COPY service1 ./service1 COPY service2 ./service2 RUN cd service1 && npm install RUN cd service2 && npm install CMD ["node", "service1/index.js"] # Only runs one service ## Right: Separate Dockerfiles and containers for each microservice # See previous example for separate Dockerfiles and docker-compose.yml
Quick Reference
| Command | Purpose |
|---|---|
| docker build -t imagename . | Builds a Docker image from Dockerfile |
| docker run -p hostPort:containerPort imagename | Runs a container exposing ports |
| docker-compose up | Starts all services defined in docker-compose.yml |
| docker-compose down | Stops and removes containers, networks |
| docker ps | Lists running containers |
Key Takeaways
Package each microservice in its own Docker container for isolation.
Use Docker Compose to run and manage multiple microservices together.
Expose correct ports and avoid sharing state directly between containers.
Keep dependencies isolated inside each container to prevent conflicts.
Avoid running multiple microservices in a single container.