docker-compose.yml configuration¶
The docker-compose.yml file is a configuration file used to define and run multi-container Docker applications.^[600-developer__docker__docker-compose__docker-compose-01.md] It allows users to launch multiple images (services) simultaneously within a single isolated environment.^[600-developer__docker__docker-compose__docker-compose-01.md]
File Structure¶
The configuration typically includes a version field and a services section.^[600-developer__docker__docker-compose__docker-compose-01.md] Under services, individual containers are defined with specific configurations such as the Docker image to use, restart policies, and environment variables.
Example Configuration¶
Below is an example configuration that sets up a MySQL database and an Adminer interface:
version: '3.1'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'root'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'tommy'
MYSQL_PASSWORD: 'tommy'
adminer:
image: adminer
restart: always
ports:
- 8888:8080
In this example:
* The db service uses the mysql:5.7 image and sets credentials via environment variables^[600-developer__docker__docker-compose__docker-compose-01.md].
* The adminer service exposes the container's port 8080 to port 8888 on the host machine^[600-developer__docker__docker-compose__docker-compose-01.md].
Usage¶
To start the services defined in the file, run the following command in the directory containing the docker-compose.yml file. Adding the -d flag will run the containers in the background (detached mode).^[600-developer__docker__docker-compose__docker-compose-01.md]
docker-compose up -d
Sources¶
- 600-developer__docker__docker-compose__docker-compose-01.md
Related¶
- [[Docker]]
- DevOps
- [[YAML]]