Setting Up a LAMP Stack with Docker Compose
You are viewing an old version of this article. View
the current version here.
Docker Compose is a tool that allows to declare which Docker containers should run, and which relationships should exist between them. It follows the infrastructure as code approach, just like most automation software and Docker itself.
The docker-compose.yml
File
When using Docker Compose, the Docker infrastructure must be described in a YAML file called docker-compose.yml
.
Let's see an example:
version: "3" services: web: image: "apache:${PHP_VERSION}" restart: 'always' depends_on: - mariadb restart: 'always' ports: - '8080:80' links: - mariadb mariadb: image: "mariadb-${MARIADB_VERSION}" restart: 'always' volumes: - ${MARIADB_DATA_DIR-./data/mysql}:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD}
In the first line we declare that we are using the version 3 of the Docker compose language.
Then we have the list of services, namely the web
and the mariadb
services.
Let's see the properties of the services:
port
maps the 8080 container port to the 80 host system port. This is very useful for a development environment, but not in production, because it allows us to connect our browser to the containerized web server. Normally there is no need to connect to MariaDB from the host system.links
declares that this container must be able to connectmariadb
. The hostname is the container name.depends_on
declares thatmariadb
needs to start beforeweb
. This is because we cannot do anything with our application until MariaDB is ready to accept connections.restart: always
declares that the containers must restart if they crashes.
Docker Compose Commands
Docker Compose Resources
- Overview of Docker Compose in the Docker documentation
- Compose file in the Docker documentation
- Docker Compose on GitHub
Content initially contributed by Vettabase Ltd.
Comments
Comments loading...
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.