Master Docker Compose: Install, Configure, and Deploy Multi-Container Apps
Learn how to install Docker Compose, configure its permissions, verify installation, and use it to define services, images, ports, volumes, and environment variables in a docker‑compose.yml file, then manage containers with common commands to deploy and run multi‑container applications efficiently.
What is Docker Compose?
Docker Compose is a tool for defining and running multi‑container Docker applications. You configure services in a YAML file and deploy them with a single command.
Installation
Download Docker Compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-composeMake the file executable
chmod +x /usr/local/bin/docker-composeVerify installation
docker-compose --versionSteps to Use Docker Compose
Define the application environment with a Dockerfile when you need to modify the base image.
Define services to be deployed in a docker‑compose.yml file for one‑click deployment.
Run docker-compose up to launch all services at once.
Common docker‑compose.yml Options
image
Specify the image name to run.
# Use MySQL 5.7 image
image: mysql:5.7container_name
Set the container name.
container_name: mysqlports
Map host and container ports (HOST:CONTAINER).
ports:
- 3306:3306volumes
Mount host files or directories into the container (HOST:CONTAINER).
volumes:
- /mydata/mysql/log:/var/log/mysql
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysqlenvironment
Configure environment variables.
environment:
- MYSQL_ROOT_PASSWORD=rootlinks
Link services (SERVICE:ALIAS).
links:
- db:databaseCommon Docker Compose Commands
Build, create, and start containers
# Run in detached mode
docker-compose up -dStop all containers
docker-compose stopList container information
docker-compose psDeploy an Application with Docker Compose
Write docker‑compose.yml
Docker Compose groups containers into three layers: project, service, and container. Services are defined under the services key, and containers can reach each other by service name.
version: '3'
services:
db:
image: mysql:5.7
container_name: mysql
ports:
- 3306:3306
volumes:
- /mydata/mysql/log:/var/log/mysql
- /mydata/mysql/data:/var/lib/mysql
- /mydata/mysql/conf:/etc/mysql
environment:
- MYSQL_ROOT_PASSWORD=root
mall-tiny-docker-compose:
image: mall-tiny-docker-compose:0.0.1-SNAPSHOT
container_name: mall-tiny-docker-compose
ports:
- 8080:8080
volumes:
- /etc/localtime:/etc/localtime
- /mydata/app/mall-tiny-docker-compose/logs:/logsNote: If the mall‑tiny‑docker‑compose service cannot connect to MySQL, create a mall database in MySQL and import the mall.sql script.
Build the image with Maven plugin
Note: For build issues, refer to “Using Maven Plugin to Build Docker Images”.
Run Docker Compose to start all services
Upload docker-compose.yml to the Linux server and run:
docker-compose up -dAccess the API documentation at http://192.168.3.101:8080/swagger-ui.html:
Project Source Code
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker-compose
Recommended Reading
Using Maven Plugin to Build Docker Images
Essential Docker Commands for Developers
Essential Linux Commands for Developers
Deploying Mall on Linux with Docker
Deploying Mall on Windows
Building Basic Skeleton with SpringBoot+MyBatis
Integrating Swagger‑UI for Online API Docs
Integrating SpringSecurity and JWT (Part 1)
Integrating Elasticsearch for Product Search
Integrating MongoDB for Document Operations
Integrating RabbitMQ for Delayed Messages
Integrating OSS for File Upload
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
