From 8014614844ad3a9e233035162f7c566521dd3764 Mon Sep 17 00:00:00 2001 From: Jorge Martinez Date: Fri, 26 Jun 2020 09:45:50 -0500 Subject: [PATCH] docker support for TM4 --- Makefile | 2 + docker-compose.override.yml | 31 +++++-- docker-compose.yml | 80 +++++++++++-------- scripts/docker/Dockerfile.backend | 30 +++++++ scripts/docker/Dockerfile.frontend | 32 ++++++++ .../docker/Dockerfile.frontend_development | 26 ++++++ 6 files changed, 159 insertions(+), 42 deletions(-) create mode 100644 scripts/docker/Dockerfile.backend create mode 100644 scripts/docker/Dockerfile.frontend create mode 100644 scripts/docker/Dockerfile.frontend_development diff --git a/Makefile b/Makefile index 5f871c6aeb..105cdea465 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,8 @@ build: docker-compose build --no-cache app up: + docker network inspect tm-web >/dev/null 2>&1 || \ + docker network create tm-web docker-compose up -d down: diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 7d01ae49b6..3c95b7817c 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,18 +1,35 @@ # To use this file, run `docker-compose up`. -version: '3' +version: '3.4' -services: +x-backend-config: &backend + build: + context: "./scripts/docker" + dockerfile: "Dockerfile.backend" +services: # Main application - app: - build: ./scripts/docker/tasking-manager + backend: + <<: *backend volumes: - .:/usr/src/app - ports: - - "3000:3000" # Migration service migration: - build: ./scripts/docker/tasking-manager + <<: *backend volumes: - .:/usr/src/app + + frontend: + build: + context: "." + dockerfile: "./scripts/docker/Dockerfile.frontend" + args: + - TM_APP_API_URL=http://localhost/api + # stdin_open: true + # volumes: + # - "./frontend/src:/usr/src/app/src" + # environment: + # - REACT_APP_API_URL=http://localhost/api + labels: + - traefik.http.routers.frontend.rule=Host(`localhost`) + - traefik.http.services.frontend.loadbalancer.server.port=80 diff --git a/docker-compose.yml b/docker-compose.yml index 6da7393c65..e261bc9510 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,49 +1,59 @@ # To use this file, run `docker-compose up`. -version: '3' +version: "3.4" + +x-backend-config: &backend + image: hotosm-tasking-manager:backend + env_file: ${ENV_FILE:-tasking-manager.env} + depends_on: + - postgresql + links: + - postgresql + networks: + - tm-web services: # Main application - app: - image: hotosm/tasking-manager - container_name: app - env_file: tasking-manager.env - environment: - - POSTGRES_ENDPOINT=postgresql - depends_on: - - postgresql - expose: - - "5000" - ports: - - "5000:5000" - links: - - postgresql - volumes: - - node_modules:/usr/src/app/frontend/node_modules + backend: + <<: *backend + container_name: backend + labels: + - traefik.http.routers.backend.rule=Host(`localhost`) + - traefik.http.routers.backend.rule=PathPrefix(`/api/`) + - traefik.http.services.backend.loadbalancer.server.port=5000 - # Migration service migration: - image: hotosm/tasking-manager - env_file: tasking-manager.env + <<: *backend + container_name: migration restart: on-failure - environment: - - POSTGRES_ENDPOINT=postgresql - depends_on: - - postgresql - links: - - postgresql command: python manage.py db upgrade - # Database + frontend: + image: hotosm-tasking-manager:frontend + networks: + - tm-web + labels: + - traefik.http.routers.frontend.rule=Host(`localhost`) + - traefik.http.services.frontend.loadbalancer.server.port=80 + postgresql: image: mdillon/postgis:11 container_name: postgresql - env_file: tasking-manager.env - # logging: - # driver: none + env_file: ${ENV_FILE:-tasking-manager.env} + networks: + - tm-web + + traefik: + image: traefik ports: - - "5433:5432" - expose: - - "5432" + - "80:80" + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + command: + - --entrypoints.web.address=:80 + - --providers.docker=true + networks: + - tm-web -volumes: - node_modules: +networks: + tm-web: + external: true diff --git a/scripts/docker/Dockerfile.backend b/scripts/docker/Dockerfile.backend new file mode 100644 index 0000000000..bc32e1cce3 --- /dev/null +++ b/scripts/docker/Dockerfile.backend @@ -0,0 +1,30 @@ +FROM alpine/git as base + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Get the Tasking Manager +ARG branch=master +RUN git clone --depth=1 git://github.com/hotosm/tasking-manager.git \ + --branch $branch /usr/src/app + +RUN rm -rf frontend/ + +FROM python:3.7-alpine + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +COPY --from=base /usr/src/app /usr/src/app + +## SETUP + +# Setup backend dependencies +RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev geos-dev proj-util proj-dev make +RUN pip install --no-cache-dir -r requirements.txt + +## INITIALIZATION + +EXPOSE 5000 +CMD ["gunicorn", "-b", "0.0.0.0:5000", "--worker-class", "gevent", "--workers", "3", \ + "--threads", "3", "--timeout", "179", "manage:application", "&"] \ No newline at end of file diff --git a/scripts/docker/Dockerfile.frontend b/scripts/docker/Dockerfile.frontend new file mode 100644 index 0000000000..b063e9acb7 --- /dev/null +++ b/scripts/docker/Dockerfile.frontend @@ -0,0 +1,32 @@ +FROM alpine/git as base + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Get the Tasking Manager +ARG branch=master +RUN git clone --depth=1 git://github.com/hotosm/tasking-manager.git \ + --branch $branch /usr/src/app + +RUN rm -rf backend/ migrations/ + +FROM tiangolo/node-frontend:10 as build + +WORKDIR /usr/src/app + +COPY --from=base /usr/src/app/frontend /usr/src/app + +## SETUP +RUN npm install + +ARG TM_APP_API_URL=http://localhost/api + +# SERVE +RUN npm run build + +FROM nginx:stable-alpine +COPY --from=build /usr/src/app/build /usr/share/nginx/html +COPY --from=build /nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/scripts/docker/Dockerfile.frontend_development b/scripts/docker/Dockerfile.frontend_development new file mode 100644 index 0000000000..27d36d87bd --- /dev/null +++ b/scripts/docker/Dockerfile.frontend_development @@ -0,0 +1,26 @@ +FROM alpine/git as base + +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Get the Tasking Manager +ARG branch=master +RUN git clone --depth=1 git://github.com/hotosm/tasking-manager.git \ + --branch $branch /usr/src/app + +RUN rm -rf backend/ migrations/ + +FROM tiangolo/node-frontend:10 as build + +WORKDIR /usr/src/app + +COPY --from=base /usr/src/app/frontend /usr/src/app + +## SETUP +RUN npm install + +ARG TM_APP_API_URL=http://localhost/api + +# SERVE +CMD ["npm", "start"] +