|
| 1 | +--- |
| 2 | +title: Docker Compose |
| 3 | +slug: /development-environment/docker-compose |
| 4 | +--- |
| 5 | + |
| 6 | +[Docker](https://docs.docker.com) and [Docker Compose](https://docs.docker.com/compose/) are the core technologies that orchestrate the |
| 7 | +services of the boilerplate. |
| 8 | + |
| 9 | +They will help you set up a complete development environment close to your target production infrastructure. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +* 🚀 `make up`: starts the Docker Compose stack. |
| 14 | +* 💣 `make down`: stops the Docker Compose stack. |
| 15 | +* 📢 `docker-compose logs -f`: displays the logs of **all** your services. |
| 16 | +* 📢 `docker-compose logs -f [SERVICE_NAME]` displays the logs of one service. |
| 17 | + |
| 18 | +## Configuration |
| 19 | + |
| 20 | +Your development environment mostly consists of two files: |
| 21 | + |
| 22 | +* *docker-compose.yml* |
| 23 | +* *.env* file (and its template *.env.dist*) |
| 24 | + |
| 25 | +The *docker-compose.yml* file lists all the services of the boilerplate and their configuration. |
| 26 | +The services use mostly environment variables to configure themselves. |
| 27 | +Most of the time, you will set their values directly in the *docker-compose.yml* file. |
| 28 | + |
| 29 | +However, you don't want to commit your secrets to your Git repository. Also, you may want to reuse some values across |
| 30 | +different services. |
| 31 | + |
| 32 | +Docker Compose provides an easy way for such scenarios; it can read the values from the *.env* file. |
| 33 | + |
| 34 | +For instance: |
| 35 | + |
| 36 | +```.env title=".env" |
| 37 | +FOO=hello |
| 38 | +``` |
| 39 | + |
| 40 | +```yaml title="docker-compose.yml" |
| 41 | +service_foo: |
| 42 | + environment: |
| 43 | + FOO: "$FOO" |
| 44 | +``` |
| 45 | +
|
| 46 | +Becomes at runtime (e.g., when running a Docker Compose command): |
| 47 | +
|
| 48 | +```yaml |
| 49 | +service_foo: |
| 50 | + environment: |
| 51 | + FOO: "hello" |
| 52 | +``` |
| 53 | +
|
| 54 | +:::note |
| 55 | +
|
| 56 | +When adding a new variable in the *.env* file, don't forget to update the template *.env.dist* with it. |
| 57 | +It will help other developers to notice this change and update their *.env* files accordingly. |
| 58 | +
|
| 59 | +You should never commit the *.env* file as it may contain secrets; always use dummy values for your secrets |
| 60 | +in the *.env.dist* template. |
| 61 | +
|
| 62 | +::: |
| 63 | +
|
| 64 | +## Add a new service |
| 65 | +
|
| 66 | +The existing services might not be enough for your use cases. |
| 67 | +You may therefore add new services to your *docker-compose.yml* file. |
| 68 | +
|
| 69 | +### Application layer |
| 70 | +
|
| 71 | +```yaml title="docker-compose.yml" |
| 72 | +services: |
| 73 | + |
| 74 | + your_service_name: |
| 75 | + image: an_image:a_tag |
| 76 | + labels: |
| 77 | + - traefik.enable=true |
| 78 | + - traefik.http.routers.your_service_name_router.rule=Host(`your_service_subdomain.$DOMAIN`) |
| 79 | + - traefik.http.routers.your_service_name_router.service=your_service_name_service |
| 80 | + # If your service runs on another port than 80. |
| 81 | + # - traefik.http.services.your_service_name_service.loadbalancer.server.port=3000 |
| 82 | + environment: |
| 83 | + FOO: "BAR" |
| 84 | + volumes: |
| 85 | + - src/your_service_source_code_folder:/path/in/the/docker/container |
| 86 | +``` |
| 87 | +
|
| 88 | +:::note |
| 89 | +
|
| 90 | +Always add a service code source in the *src* folder. |
| 91 | +
|
| 92 | +::: |
| 93 | +
|
| 94 | +### Data layer |
| 95 | +
|
| 96 | +```yaml title="docker-compose.yml" |
| 97 | +services: |
| 98 | + |
| 99 | + your_service_name: |
| 100 | + image: an_image:a_tag |
| 101 | + environment: |
| 102 | + FOO: "BAR" |
| 103 | + volumes: |
| 104 | + - your_service_name_data:/path/in/the/docker/container |
| 105 | + |
| 106 | +volumes: |
| 107 | + |
| 108 | + your_service_name_data: |
| 109 | + driver: local |
| 110 | +``` |
| 111 | +
|
| 112 | +## Extend a Docker image |
| 113 | +
|
| 114 | +You might need to extend a Docker image for installing one or more packages. |
| 115 | +
|
| 116 | +For instance, let's say you want to install the `pdftk` package for the API: |
| 117 | + |
| 118 | +```dockerfile title="src/api/Dockerfile" |
| 119 | +FROM thecodingmachine/php:7.4-v3-apache AS extended |
| 120 | +
|
| 121 | +# Always use the root user for installing packages. |
| 122 | +USER root |
| 123 | +
|
| 124 | +# Install PDFtk. |
| 125 | +RUN DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends pdftk &&\ |
| 126 | + # Print versions of PDFtk. |
| 127 | + pdftk --version |
| 128 | +
|
| 129 | +# Go back to the default Docker image user. |
| 130 | +USER docker |
| 131 | +
|
| 132 | +FROM extended |
| 133 | +# Your production Docker image instructions. |
| 134 | +``` |
| 135 | + |
| 136 | +```yaml title="docker-compose.yml" |
| 137 | +api: |
| 138 | + #image: thecodingmachine/php:7.4-v3-apache |
| 139 | + build: |
| 140 | + context: "./src/api" |
| 141 | + target: "extended" |
| 142 | +``` |
| 143 | + |
| 144 | +```makefile title="Makefile" |
| 145 | +# Start the Docker Compose stack. |
| 146 | +up: |
| 147 | + docker-compose up --build -d |
| 148 | +``` |
0 commit comments