0

I have a nodejs / express / react app running locally that starts a node server at :3001 and a react app at :3000 which can make requests to the express API.

I then made a /client/Dockerfile and a /server/Dockerfile, and a /docker-compose.yml which is capable of running my app locally without issue.

I now want to deploy this to GCP's Cloud Run, but GCP does not allow multiple docker images / docker-compose (AFAIK - it only has a single field for "Dockerfile"), so I am trying to reconfigure things to work with a single Docker file.

Here's what I had for the working, local instance:

client/Dockerfile

FROM node:lts-slim

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

EXPOSE 3000

CMD [ "npm", "start" ]

server/Dockerfile

FROM node:lts-slim

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

EXPOSE 3001

# You can change this
CMD [ "npm", "run", "dev" ]

docker-compose.yml

version: "3"
services:
  client:
    container_name: gcp-cloudrun-client
    build:
      context: ./client
      dockerfile: Dockerfile
    image: mheavers/gcp-cloudrun-client
    ports:
      - "3000:3000"
    volumes:
      - ./client:/usr/src/app
  server:
    container_name: gcp-cloudrun-server
    build:
      context: ./server
      dockerfile: Dockerfile
    image: mheavers/gcp-cloudrun-server
    ports:
      - "3001:3001"
    volumes:
      - ./server:/usr/src/app

How do I combine all this into a single Dockerfile and do away with docker-compose?

mheavers
  • 29,530
  • 58
  • 194
  • 315
  • Maybe you may want to create a separate service for the client and the another service for the server. Then make those to communicate https://stackoverflow.com/questions/65301862/how-can-my-cloud-run-service-call-other-cloud-run-services – Puteri Jul 07 '22 at 23:09
  • Also can help https://stackoverflow.com/a/67195731/12265927 – Puteri Jul 07 '22 at 23:11
  • 2
    Can you use a multi-stage build to first compile the React application to static files, then serve that from the Express application? That would let you have a single image and a single container. – David Maze Jul 07 '22 at 23:44
  • @DavidMaze - that's the route I'm trying to go down - can't seem to get a syntax Docker likes yet though. Updated my post to show what I have there. – mheavers Jul 08 '22 at 15:37

1 Answers1

0

this was my final docker file to replace docker-compose:

FROM node:lts-slim AS client
WORKDIR /usr/src/app
COPY client/ ./client/
RUN cd client && npm install && npm run build

FROM node:lts-slim AS server
WORKDIR /root/
COPY --from=client /usr/src/app/client/build ./client/build
COPY server/package*.json ./server/
RUN cd server && npm install
COPY server/index.js ./server/

EXPOSE 80

CMD ["node", "./server/index.js"]
mheavers
  • 29,530
  • 58
  • 194
  • 315