使用Traefik,Nginx和Laravel配置Docker

时间:2018-09-18 05:50:40

标签: laravel docker nginx traefik

我试图弄清楚如何为开发和以后的部署设置简单的堆栈。我想利用Docker作为面向公众的反向代理在容器中提供Traefik,然后根据需要与Nginx容器进行接口,该Nginx容器仅用于提供静态前端文件(HTML,CSS,JS)和后端PHP容器,运行Laravel(我有意将此项目的前端和API分离)。

我正在尽最大努力从那里学习所有视频和书面教程,但是事情变得非常复杂(至少对于我刚起步的大脑而言),而且有点让人不知所措。我有一个为期一周的截止日期才能完成该项目,并且我强烈考虑暂时放弃Docker,因为我担心我会花整整时间试图弄乱配置而不是实际编码!

要开始使用,我有一个简单的docker-compose,它具有以下已验证至少可以正常运行的配置:

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.

现在,我需要弄清楚如何有效地连接Nginx和PHP / Laravel。

1 个答案:

答案 0 :(得分:4)

首先,不要让自己承受学习新知识的压力。因为如果您这样做,学习新知识将再也感觉不到。利用您的技术知识并完成工作。完成后,您意识到您有1/2天的截止日期,请尝试使用新技术来实现超额交付。这样一来,您就不会浪费最后期限,也不会因配置我们的新技术或配置而感到压力。

您在下面看到的配置尚未完成,也没有经过功能测试。我只是复制了我的3个主要项目中的大多数内容,以便为您提供一个起点。正确设置Traefik可能会很复杂。

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: