Skip to content
On this page

Docker compose

Due to the amount of projects you will be interacting with at Libaro some people like to use Docker to quickly set up projects locally. Docker compose allows us to bundle a dev environment with its dependencies right along the project in the repository. You can recognize a project that has a docker compose setup by the docker-compose.yml file in the root of the project.

Read more about Docker compose in the Docker compose documentation

Useful commands

CommandDescription
docker compose build [service name]Rebuild a service
docker compose up -d (--force-recreate) [service name]Start a service in the background
docker compose exec [service name] [command]Run a command in an existing running container

Updating your containers

When changes have been made to a service's Dockerfile, you have to rebuild the service before the changes take effect.

bash
docker compose build [service name]

After that you need to recreate the container.

bash
docker compose up -d --force-recreate [service name]

Configuration explained

Here follows some information about this example docker compose config.

yaml
services:
  php:
    build:
      context: .
      dockerfile: .docker/php/Dockerfile
    container_name: example_php
    volumes:
      - .:/var/www:delegated
    ports:
      - "8000:80"
  mysql:
    image: mysql:8
    container_name: example_mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: dev
      MYSQL_DATABASE: dev
      MYSQL_USER: dev
      MYSQL_PASSWORD: dev
    ports:
      - "3306:3306"

How do I connect to a website running in a container?

The ports section of the configuration file contains this information. It describes which port on your host (left 8000:80) connects to the port on your container (right 8000:80). So in this case I can reach the php service through http://localhost:8000 and the mysql instance through localhost:3306.

How do containers communicate with each other?

Containers can communicate whith eachother using the container name, in this case the php service could connect to the mysql service by using example_mysql as hostname.

Containers use the internal ports to communicate with eachother, so if you would want to reach the webservice running in the php container from the mysql container you would do so through http://example_php:80.

Aliases - Shorten your commands!

On macOS

The commands for interacting with docker containers are quite long, to make our life a bit easier we can add some aliases to our rc files.

When using bash your rc file is ~/.bashrc, if you're using Z shell on the other hand this will be ~/.zshrc

Add the following to the bottom of your rc file.

sh
function -php() {
    docker-compose exec php $@
}

function -art() {
    -php php artisan $@
}

And then source it to apply the changes.

sh
source ~/.zshrc