Don’t overwrite the built vendor directory

Open your docker-compose.yml file in your editor, and copy paste the following lines:

version: '2'
    image: nginx:1.11.10-alpine
      - 3000:80
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    build: .
      - 9000:9000
      - .:/app
      - /app/vendor
      - postgres
      DATABASE_URL: postgres://[email protected]/todos
    image: postgres:9.6.2-alpine
      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 nginx:1.11.10-alpine .

  • image: nginx:1.11.10-alpine – Similar to the other services, we are using the alpine version.
  • ports: – This will publish the containers port, in this case 80 , to the host as port 3000 .
  • volumes:nginx needs a configuration file. We can build that locally, and this will use the local file as the default.conf file.

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/vendor at runtime, so we don’t overwrite the built vendor directory.
  • environment: – The application itself expects the environment variable DATABASE_URL to run. This is set in ./config/database.php .
  • ports: – This will post the container’s port, in this case 9000 , to the host as port 9000 .

The DATABASE_URL is the connection string. postgres://[email protected]/todos connects using the todoapp user, on the host postgres , using the database todos .

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 todoapp as the default user for PostgreSQL.
  • POSTGRES_DB: todos – This will create the default database as todos .