Answers: <p>I found the solution, eventually it was just a volume issue, I had to add a specific line for modules :  </p>
  
  <pre><code>volumes:
        - ./server:/app/server
        - /app/server/node_modules
  </code></pre>
  
  <p>After that I no longer got the "Can't find module" error<br>
  Still not sure what the impact of this is though</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-04-16T09:51:08.050Z
Answers: 2019-04-16T09:51:08.050Z
Answers: guillaumepn
Body: <h1></h1>
  <p>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 :</p>
  <ul>
  <li>a <strong>server</strong> folder : runs with Express on port 4000</li>
  <li>a <strong>web</strong> folder : a React app running on 3000 (locally), that needs to run on 80 on my server</li>
  <li>a <strong>docker-compose.prod.yml</strong> file in the root folder</li>
  </ul>
  <p>What I'm currently trying :</p>
  <ul>
  <li>the <strong>docker-compose.prod.yml</strong> is running a Postgres service (port 5432), as well as web and server's respective Dockerfile</li>
  <li>a Dockerfile in both web and server folders</li>
  <li>I <code>yarn build</code> both server and web folders locally and send everything on my server</li>
  <li>on my server, I run <code>docker build -f web/Dockerfile.prod -t app_web ./web</code> and <code>docker build -f server/Dockerfile.prod -t app_server ./server</code> to build both images</li>
  <li>finally, I run <code>docker-compose -f docker-compose.prod.yml up</code></li>
  </ul>
  <p><strong>My problem :</strong></p>
  <p>The <code>server</code> container throws <code>Error: Cannot find module '...'</code> although I run <code>yarn install --production</code> in its Dockerfile (it may be related to volumes?)</p>
  <p>Also, when I run <code>docker run -it -p 80:80 app_web</code> and <code>docker-compose up</code>, 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)</p>
  <p>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?)</p>
  <p><strong>docker-compose.prod.yml</strong> :</p>
  <pre><code>version: &quot;3&quot;
  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
  </code></pre>
  <p><strong>server/Dockerfile.prod</strong> :</p>
  <pre><code>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 [&quot;node&quot;, &quot;/app/server/dist&quot;]
  </code></pre>
  <p><strong>web/Dockerfile.prod</strong> :</p>
  <pre><code>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 [&quot;serve&quot;, &quot;-p&quot;, &quot;80&quot;, &quot;-s&quot;, &quot;.&quot;]
  </code></pre>
  
Tags: <docker><docker-compose>
Title: How to deploy my Express/React app to server with docker-compose and Dockerfiles
UserDisplayName: guillaumepn
ThreadText: # How to deploy my Express/React app to server with docker-compose and Dockerfiles
  
  (Asked by: username_0 on 2019-04-15)
  
  # 
  
  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", "."]
  
  ```
  
  **Tags:** docker, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-04-16. Score: 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
  
  
  ---
  
  