1

Hi, I'm trying to deploy my app to production with Docker, but I struggle a lot. Here is the structure from the root folder :

  • a server folder : runs with Express on port 4000
  • a web folder : a React app running on 3000 (locally), that needs to run on 80 on my server
  • a docker-compose.prod.yml file in the root folder

What I'm currently trying :

  • the docker-compose.prod.yml is running a Postgres service (port 5432), as well as web and server's respective Dockerfile
  • a Dockerfile in both web and server folders
  • I yarn build both server and web folders locally and send everything on my server
  • on my server, I run docker build -f web/Dockerfile.prod -t app_web ./web and docker build -f server/Dockerfile.prod -t app_server ./server to build both images
  • finally, I run docker-compose -f docker-compose.prod.yml up

My problem :

The server container throws Error: Cannot find module '...' although I run yarn install --production in its Dockerfile (it may be related to volumes?)

Also, when I run docker run -it -p 80:80 app_web and docker-compose up, the web container acts differently when visiting my server IP address : in the first case, the app displays well, in the second case, a page is listing my web folder content (all files and folders)

I'd really appreciate help as I'm a bit new to Docker in production (should I use docker machine? are my folders structure / docker-compose / Dockerfiles wrong?)

docker-compose.prod.yml :

version: "3"
services:
  db:
    image: postgres:latest
    restart: on-failure
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_NAME}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    ports:
      - 5432:5432
    volumes:
      - ./.db/data:/var/lib/postgresql/data

  server:
    container_name: app_server
    build:
      context: ./server
      dockerfile: Dockerfile.prod
    volumes:
      - ./server:/app/server
    ports:
      - 4000:4000
    depends_on:
      - db

  web:
    container_name: app_web
    build:
      context: ./web
      dockerfile: Dockerfile.prod
    volumes:
      - ./web:/app/web
    ports:
      - 80:80
    depends_on:
      - db

server/Dockerfile.prod :

FROM mhart/alpine-node:11 AS builder
WORKDIR /app/server
COPY package.json ./
RUN yarn install --production
COPY . .

FROM mhart/alpine-node
WORKDIR /app/server
COPY --from=builder /app/server .
CMD ["node", "/app/server/dist"]

web/Dockerfile.prod :

FROM mhart/alpine-node:11 AS builder
WORKDIR /app/web
COPY package.json .
RUN yarn install --production
COPY . .

FROM mhart/alpine-node
RUN yarn global add serve
WORKDIR /app/web
COPY --from=builder /app/web/build .
CMD ["serve", "-p", "80", "-s", "."]

1 Answer 1

1

I found the solution, eventually it was just a volume issue, I had to add a specific line for modules :

volumes:
      - ./server:/app/server
      - /app/server/node_modules

After that I no longer got the "Can't find module" error
Still not sure what the impact of this is though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.