Speedbrake88
Speedbrake88

Reputation: 49

Docker Compose with React and Nginx

I'm trying to use docker-compose for deployment of my React app, which uses an express backend and Postgres Database. My idea is to have shared volumes from my docker-compose. Then build from my Dockerfile into the volume, so that Nginx will be able to serve the files. The problem now is that it works when i build the project the first time, but if I change something in my React Client and run "docker-compose up --build" it looks like everything is building as it should, but the files served are still the same. Is COPY command in my dockerfile not overwriting the old files?

Dockerfile in my React Client Project

FROM node:13.12.0-alpine as build
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
RUN npm install
COPY . ./
RUN npm run build
FROM node:13.12.0-alpine
COPY --from=build /app/build /var/lib/frontend

docker-compose

version: "3.7"
services:
 callstat_backend:
  build: ./callstat-backend
  restart: always
  ports:
    - "3000:3000"
  env_file:
   - keys.env
  depends_on:
  - postgres
 callstat_frontend:
  build: ./callstat-client
  volumes:
   - frontend/:/var/lib/frontend
 postgres:
  image: postgres:11.2-alpine
  ports:
   - "5432:5432"
  volumes:
   - pgdata:/var/lib/postgresql/data
  environment:
   POSTGRES_USER: postgres
   POSTGRES_PASSWORD: postgres
   POSTGRES_DB: callstat
 nginx:
  image: nginx
  volumes:
   - frontend:/usr/share/nginx/html
   - ./nginx.conf:/etc/nginx/conf.d/default.conf
  ports:
   - "80:80"
  depends_on:
   - callstat_frontend
volumes:
 pgdata:
 frontend:

Maybe i'm taking a totaly wrong approach here?

Upvotes: 1

Views: 2156

Answers (1)

akazuko
akazuko

Reputation: 1394

You can run the commands in the following order:

# stop down the services
docker-compose stop

# remove the previously created docker resources
docker-compose rm

# bring up the services again
docker-compose up --build

This was your previous volume be removed and new one will be created with the updated changes.

NOTE: This is okay from the development perspective, but docker volumes are really expected to persist between deployments. For artifacts like code changes ideally images should be published as part of build process. To get little more insight into this topic you can refer to https://github.com/docker/compose/issues/2127

Upvotes: 1

Related Questions