Master Docker Compose: From Installation to HAProxy Load Balancing
This guide walks you through installing Docker Compose, creating and configuring docker‑compose.yml files, launching multi‑service applications, scaling containers, integrating HAProxy for load balancing, managing sockets with socat, and exploring advanced Compose directives such as build, depends_on, deploy, logging, network_mode, and secrets.
1 docker-compose
1.1 Introduction to Compose
Composeis a tool for defining and running multi‑container Docker applications. By using a YML file you can configure all services required by an application, then start them with a single command.
The three steps to use Compose are:
Define the application environment with a Dockerfile.
Describe the services in a docker-compose.yml file.
Run docker-compose up to start the whole application.
1.2 Installing Docker‑Compose
Install Docker‑Compose on a CentOS system:
# Install pip
yum install -y python2-pip
# Install docker‑compose
pip install docker-composeEnable a faster pip mirror in China:
mkdir ~/.pip/
cat > ~/.pip/pip.conf <<'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF1.3 Orchestrating and Starting Images
Create a project directory:
mkdir /opt/my_wordpress/
cd /opt/my_wordpress/Write a docker-compose.yml that defines a MySQL database and a WordPress service:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpressStart the stack:
docker-compose up # foreground
docker-compose up -d # backgroundVisit http://10.0.0.100:8000 to complete the WordPress installation.
1.4 HAProxy Proxy for Backend Docker Containers
Modify the compose file to expose port 80, then scale WordPress to two instances:
docker-compose scale wordpress=2
# Warning: the scale command is deprecated; use "docker-compose up --scale wordpress=2"Install HAProxy:
yum install haproxy -yBackup and edit /etc/haproxy/haproxy.cfg (full configuration omitted for brevity). After editing, start HAProxy:
systemctl start haproxy
systemctl enable haproxyAccess http://10.0.0.100:8000 through HAProxy to see load‑balancing, and view statistics at http://10.0.0.100:8888/haproxy-status:
1.5 Installing socat to Control HAProxy via Socket
yum install socat.x86_64 -yDisable a backend node:
echo "disable server backend_www_example_com/web-node2" | socat stdio /var/lib/haproxy/statsEnable a backend node:
echo "enable server backend_www_example_com/web-node3" | socat stdio /var/lib/haproxy/stats1.6 Compose YML Configuration Reference
1.6.1 Simple Commands
version: specify the Compose file version. cap_add, cap_drop: add or drop kernel capabilities. cgroup_parent: set the parent cgroup for the container. command: override the default command. container_name: give a custom name to the container. devices: list device mappings. dns, dns_search: custom DNS servers and search domains. entrypoint: override the default entrypoint. env_file: load environment variables from a file. environment: set environment variables (use quotes for booleans). expose: expose a port without publishing it. extra_hosts: add host‑name mappings. healthcheck: define a health‑check for the container. image: specify the image to use (e.g., redis, ubuntu:14.04, etc.). restart: set restart policy ( no, always, on-failure, unless-stopped). security_opt: set security options such as SELinux labels. stop_grace_period: time to wait after SIGTERM before SIGKILL. stop_signal: alternative signal to stop the container. sysctls: kernel parameters inside the container. tmpfs: mount temporary file systems. ulimits: override default ulimit settings. volumes: bind host paths or named volumes into the container.
1.6.2 build
The build section defines how to build an image: context: path to the build context. dockerfile: name of the Dockerfile. args: build‑time variables. labels: metadata labels for the image. target: specify a build stage in a multi‑stage Dockerfile.
1.6.3 depends_on
Controls service start order. docker-compose up starts dependencies first; docker-compose stop stops in reverse order.
1.6.4 deploy
Deployment configuration used only in Swarm mode. Options include mode (replicated or global), replicas, resources (cpu/memory limits), restart_policy, rollback_config, and update_config.
1.6.5 logging
Configure log driver (default json-file) and options such as max-size and max-file. For syslog driver, you can set syslog-address.
1.6.6 network_mode
Set the network mode (e.g., bridge, host, none, service:[name], container:[id]) and define networks with aliases.
1.6.7 secrets
Store sensitive data like passwords. Example:
version: "3.1"
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/my_secret
secrets:
- my_secret
secrets:
my_secret:
file: ./my_secret.txtFor the full reference, see the original source link.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
