712Jefferson
712Jefferson

Reputation: 231

Configuring Docker with Traefik, Nginx and Laravel

I am trying to figure out how to setup a simple stack for development and later deployment. I want to utilize Docker to serve Traefik in a container as the public facing reverse-proxy, which then interfaces as needed with a Nginx container that is used only to serve static frontend files (HTML, CSS, JS) and a backend PHP container that runs Laravel (I'm intentionally decoupling the frontend and API for this project).

I am trying my best to learn through all of the video and written tutorials out there, but things become complicated very quickly (at least, for my uninitiated brain) and it's a bit overwhelming. I have a one-week deadline to complete this project and I'm strongly considering dropping Docker altogether for the time being out of fear that I'll spend the whole trying to mess around with the configuration instead of actually coding!

To get started, I have a simple docker-compose with the following configuration that I've verified at least runs correctly:

version: '3'

services:
  reverse-proxy:
    image: traefik
    command: --api --docker # Enables Web UI and tells Traefik to listen to Docker.
    ports:
      - "80:80" # HTTP Port
      - "8080:8080" # Web UI
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events.

Now, I need to figure out how to connect Nginx and PHP/Laravel effectively.

Upvotes: 3

Views: 6468

Answers (1)

bastianwegge
bastianwegge

Reputation: 2498

First of all don't put yourself under stress to learn new stuff. Because if you do, learning new stuff won't feel that comfortable anymore. Take your knowledge of technology and get stuff done. When you're done and you realize you have 1/2 days to go to your deadline, try to overdeliver by including new technology. This way you won't screw your deadline and you will not be under stress figuring our new technology or configuration.

The configuration you see below is not complete nor functionally tested. I just copied most of the stuff out of 3 of my main projects in order to give you a starting-point. Traefik as-is can be complicated to set up properly.

version: '3'

# Instantiate your own configuration with a Dockerfile!
# This way you can build somewhere and just deploy your container
# anywhere without the need to copy files around.

services:
  # traefik as reverse-proxy
  traefik:
    build:
      context: .
      dockerfile: ./Dockerfile-for-traefik # including traefik.toml
    command: --docker
    restart: always
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      # this file you'll have to create manually `touch acme.json && chmod 600 acme.json`
      - /home/docker/volumes/traefik/acme.json:/opt/traefik/acme.jso
    networks:
      - overlay
    ports:
      - 80:80
      - 443:443

  nginx:
    build:
      context: .
      dockerfile: ./Dockerfile-for-nginx
    networks:
      - overlay
    depends_on:
      - laravel
    volumes:
      # you can copy your assets to production with
      # `tar -c -C ./myassets . | docker cp - myfolder_nginx_1:/var/www/assets`
      # there are many other ways to achieve this!
      - assets:/var/www/assets

  # define your application + whatever it needs to run
  # important:
  # - "build:" will search for a Dockerfile in the directory you're specifying
  laravel:
    build: ./path/to/laravel/app
    environment:
      MYSQL_ROOT_PASSWORD: password
      ENVIRONMENT: development
      MYSQL_DATABASE: your_database
      MYSQL_USER: your_database_user
    networks:
      - overlay
    links:
      - mysql
    volumes:
      # this path is for development
      - ./path/to/laravel/app:/app

  # you need a database, right?
  mysql:
    image: mysql:5
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: your_database
      MYSQL_USER: your_database_user
    networks:
      - overlay
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:
  assets:

networks:
  overlay:

Upvotes: 10

Related Questions