docker-compose.yml file in your editor, and copy paste the following lines:
version: '2' services: nginx: image: nginx:1.11.10-alpine ports: - 3000:80 volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf web: build: . ports: - 9000:9000 volumes: - .:/app - /app/vendor depends_on: - postgres environment: DATABASE_URL: postgres://[email protected]/todos postgres: image: postgres:9.6.2-alpine environment: POSTGRES_USER: todoapp POSTGRES_DB: todos
This will take a bit to unpack, but let’s break it down by service.
The NGINX service
A typical setup on our local environment will require we run some sort of web server. For this example, I’m using
nginx . Like the PHP image we used, the Docker Store has a prebuilt image for NGINX. Instead of using a
build directive, we can use the name of the image, and Docker will grab that image for us and use it. In this case, we are using
image: nginx:1.11.10-alpine– Similar to the other services, we are using the
ports:– This will publish the containers port, in this case
80, to the host as port
nginxneeds a configuration file. We can build that locally, and this will use the local file as the
The web service
The first directive in the web service is to
build the image based on our
Dockerfile . This will recreate the image we used before, but it will now be named according to the project folder we are in,
phplaraveltodoapp . After that, we are giving the service some specific instructions on how it should operate:
volumes:– This section will mount paths between the host and the container.
.:/app/– This will mount the root directory to our working directory in the container.
/app/vendor– This will connect the
/app/vendorat runtime, so we don’t overwrite the built
environment:– The application itself expects the environment variable
DATABASE_URLto run. This is set in
ports:– This will post the container’s port, in this case
9000, to the host as port
DATABASE_URL is the connection string.
postgres://[email protected]/todos connects using the
todoapp user, on the host
postgres , using the database
The Postgres service
The Docker Store also has a prebuilt image for PostgreSQL that we will use just like the other two.
environment:– This particular image accepts a couple environment variables so we can customize things to our needs.
POSTGRES_USER: todoapp– This creates the user
todoappas the default user for PostgreSQL.
POSTGRES_DB: todos– This will create the default database as