3-Tier Architecture Deployment with Docker: Running Services (5)
In this post, we'll explore three different methods to run and manage the services we've set up, including the frontend, backend, database, and monitoring services.
1. Docker Command (run)
# Create networks
docker network create frontend_network
docker network create backend_network
# Run Nginx container
docker run -itd --name=internet_nginx --network frontend_network --restart always -p 80:80 example/nginx_proxy
# Run three frontend containers
docker run -itd --name=frontend1 --network frontend_network -p 3001:3000 example/frontend
docker run -itd --name=frontend2 --network frontend_network -p 3002:3000 example/frontend
docker run -itd --name=frontend3 --network frontend_network -p 3003:3000 example/frontend
# Run HAProxy container
docker run -itd --name=haproxy --network frontend_network --restart always example/haproxy
docker network connect backend_network haproxy
# Run MongoDB container
docker run -itd --name=mongodb --network backend_network -p 27017:27017 example/mongodb
# Run three backend containers with port mappings
docker run -itd --name=backend1 --network backend_network -p 8081:8080 example/backend
docker run -itd --name=backend2 --network backend_network -p 8082:8080 example/backend
docker run -itd --name=backend3 --network backend_network -p 8083:8080 example/backend
Explanation:
docker network create: Creates networks to allow communication between containers.
docker run: Launches each container with specific settings.
-itd
: Runs the container in interactive mode, detached (in the background).--name
: Assigns a name to the container.--network
: Connects the container to a specified network.--restart always
: Ensures the container restarts automatically if it stops.
2. Docker Compose
docker-compose.yml
version: "3.8"
services:
internet_nginx:
image: example/nginx_proxy
container_name: internet_proxy
restart: always
networks:
- frontend-net
ports:
- "80:80"
frontend_service:
image: example/frontend
restart: always
networks:
- frontend-net
ports:
- "3001-3003:3000"
deploy:
mode: replicated
replicas: 3
haproxy:
image: example/haproxy
container_name: haproxy
restart: always
networks:
- frontend-net
- backend-net
backend_service:
image: example/backend
restart: always
networks:
- backend-net
deploy:
mode: replicated
replicas: 3
mongodb:
image: example/mongodb
container_name: mongodb
restart: always
networks:
- backend-net
ports:
- "27017:27017"
volumes:
- /home/user/acswikicompose/mongodbData:/data/db
prometheus:
image: prom/prometheus:latest
container_name: prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "19090:9090"
networks:
- frontend-net
- backend-net
node-exporter:
image: prom/node-exporter
container_name: node-exporter
ports:
- "9100:9100"
networks:
- frontend-net
- backend-net
command:
- '--no-collector.powersupplyclass'
cadvisor:
image: google/cadvisor:latest
container_name: cadvisor
ports:
- "9559:8080"
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
networks:
- frontend-net
- backend-net
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "13000:3000"
volumes:
- ./grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yml
environment:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_SECURITY_ADMIN_PASSWORD=pass123#
networks:
- frontend-net
- backend-net
portainer:
image: portainer/portainer-ce
container_name: portainer
privileged: true
ports:
- "29000:9000"
volumes:
- portainer_data:/data
- /var/run/docker.sock:/var/run/docker.sock
networks:
- frontend-net
- backend-net
networks:
backend-net:
driver: bridge
ipam:
driver: default
config:
- subnet: "172.20.0.0/16"
gateway: "172.20.0.1"
frontend-net:
driver: bridge
ipam:
driver: default
config:
- subnet: "172.21.0.0/16"
gateway: "172.21.0.1"
volumes:
portainer_data:
Explanation:
version: Specifies the Docker Compose file format version.
services: Defines multiple container services.
networks: Connects containers to specific networks.
ports: Maps container ports to host ports.
volumes: Mounts host directories to containers for data persistence.
3. Docker Stack
docker-stack.yml
version: "3.8"
services:
internet_nginx:
image: example/nginx_proxy
networks:
- frontend-net
ports:
- "80:80"
depends_on:
- frontend_service
frontend_service:
image: example/frontend
networks:
- frontend-net
ports:
- "3001-3003:3000"
depends_on:
- haproxy
deploy:
mode: replicated
replicas: 3
haproxy:
image: example/haproxy
networks:
- frontend-net
- backend-net
depends_on:
- backend_service
backend_service:
image: example/backend
networks:
- backend-net
ports:
- "8081-8083:8080"
deploy:
mode: replicated
replicas: 3
mongodb:
image: example/mongodb
networks:
- backend-net
ports:
- "27017:27017"
grafana:
image: grafana/grafana:latest
ports:
- "13000:3000"
volumes:
- ./grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yml
environment:
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
- GF_SECURITY_ADMIN_PASSWORD=pass123#
networks:
- monitor-net
depends_on:
- prometheus
prometheus:
image: prom/prometheus:latest
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/usr/share/prometheus/console_libraries'
- '--web.console.templates=/usr/share/prometheus/consoles'
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
Explanation:
deploy: Configures deployment settings for Docker Swarm mode.
mode: Sets deployment mode (e.g., replicated, global).
depends_on: Ensures services start in the correct order.
Summary of Execution Methods
Docker Command (run): Manually starts each container and network, giving fine-grained control over each service.
Docker Compose: Simplifies running multiple containers with a single command, ideal for development and small setups.
Docker Stack: Deploys containers in a Swarm cluster, suitable for production environments with scaling needs.