|
| 1 | +# Implement Traefik Into API Platform Dockerized |
| 2 | + |
| 3 | +## Basic Implementation |
| 4 | + |
| 5 | +[Traefik](https://traefik.io) is a reverse proxy / load balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven, providing metrics, and integrating with every major cluster technology. |
| 6 | + |
| 7 | +This tool will help you to define your own routes for your client, api and more generally for your containers. |
| 8 | + |
| 9 | +Use this custom API Platform `docker-compose.yml` file which implements ready-to-use Traefik container configuration. |
| 10 | +Override ports and add labels to tell Traefik to listen the routes mentionned and redirect routes to specified container. |
| 11 | + |
| 12 | + |
| 13 | +`--api` Tell Traefik to generate a browser view to watch containers and IP/DNS associated easier |
| 14 | +`--docker` Tell Traefik to listen docker api |
| 15 | +`--docker.domain=localhost` The main DNS will be on localhost |
| 16 | +`labels:` Key for Traefik configuration into Docker integration |
| 17 | +```yaml |
| 18 | +services: |
| 19 | +# ... |
| 20 | + api: |
| 21 | + labels: |
| 22 | + - "traefik.frontend.rule=Host:api.localhost" |
| 23 | +``` |
| 24 | +The api DNS will be specified with `traefik.frontend.rule=Host:your.host` (here api.localhost) |
| 25 | + |
| 26 | +`--traefik.port=3000` Port specified to Traefik will be exopsed by container (here React app expose the 3000 port) |
| 27 | + |
| 28 | + |
| 29 | +```yaml |
| 30 | +version: '3.4' |
| 31 | +
|
| 32 | +services: |
| 33 | + reverse-proxy: |
| 34 | + image: traefik |
| 35 | + command: --api --docker --docker.domain=localhost |
| 36 | + ports: |
| 37 | + - "80:80" #All HTTP access will be caught by Traefik |
| 38 | + - "8080:8080" #Access Traefik webview |
| 39 | + volumes: |
| 40 | + - /var/run/docker.sock:/var/run/docker.sock |
| 41 | +
|
| 42 | + php: |
| 43 | + image: ${CONTAINER_REGISTRY_BASE}/php |
| 44 | + build: |
| 45 | + context: ./api |
| 46 | + depends_on: |
| 47 | + - db |
| 48 | + env_file: |
| 49 | + - ./api/.env |
| 50 | + # Comment out these volumes in production |
| 51 | + volumes: |
| 52 | + - ./api:/srv/api:rw,cached |
| 53 | + # If you develop on Linux, uncomment the following line to use a bind-mounted host directory instead |
| 54 | + # - ./api/var:/srv/api/var:rw |
| 55 | +
|
| 56 | + api: |
| 57 | + image: ${CONTAINER_REGISTRY_BASE}/nginx |
| 58 | + labels: |
| 59 | + - "traefik.frontend.rule=Host:api.localhost" |
| 60 | + build: |
| 61 | + context: ./api |
| 62 | + depends_on: |
| 63 | + - php |
| 64 | + # Comment out this volume in production |
| 65 | + volumes: |
| 66 | + - ./api/public:/srv/api/public:ro |
| 67 | +
|
| 68 | + db: |
| 69 | + # In production, you may want to use a managed database service |
| 70 | + image: postgres:9.6-alpine |
| 71 | + labels: |
| 72 | + - "traefik.frontend.rule=Host:db.localhost" |
| 73 | + environment: |
| 74 | + - POSTGRES_DB=api |
| 75 | + - POSTGRES_USER=api-platform |
| 76 | + # You should definitely change the password in production |
| 77 | + - POSTGRES_PASSWORD=!ChangeMe! |
| 78 | + volumes: |
| 79 | + - db-data:/var/lib/postgresql/data:rw |
| 80 | + # You may use a bind-mounted host directory instead, so that it is harder to accidentally remove the volume and lose all your data! |
| 81 | + # - ./docker/db/data:/var/lib/postgresql/data:rw |
| 82 | + ports: |
| 83 | + - "5432:5432" |
| 84 | +
|
| 85 | + client: |
| 86 | + # Use a static website hosting service in production |
| 87 | + # See https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.mddeployment |
| 88 | + image: ${CONTAINER_REGISTRY_BASE}/client |
| 89 | + build: |
| 90 | + context: ./client |
| 91 | + env_file: |
| 92 | + - ./client/.env |
| 93 | + volumes: |
| 94 | + - ./client:/usr/src/client:rw,cached |
| 95 | + - /usr/src/client/node_modules |
| 96 | + expose: |
| 97 | + - 3000 |
| 98 | + labels: |
| 99 | + - "traefik.port=3000" |
| 100 | + - "traefik.frontend.rule=Host:localhost" |
| 101 | +
|
| 102 | +volumes: |
| 103 | + db-data: {} |
| 104 | +``` |
| 105 | + |
| 106 | +Don't forget the db-data, then database won't work in this dockerized solution. |
| 107 | + |
| 108 | +`localhost` is a reserved domain referred in your `/etc/hosts`. |
| 109 | +If you want to implement custom DNS such as production DNS in local, just put them at the end of your `/etc/host` file like that: |
| 110 | + |
| 111 | +``` |
| 112 | +# /etc/hosts |
| 113 | +# ... |
| 114 | +
|
| 115 | +127.0.0.1 your.domain.com |
| 116 | +``` |
| 117 | + |
| 118 | +If you do that, you'll have to update the `CORS_ALLOW_ORIGIN` environment variable `api/.env` to accept the specified URL. |
| 119 | + |
| 120 | +## Known Issues |
| 121 | + |
| 122 | +If your network is of type B, it may conflict with the traefik sub-network. |
0 commit comments