Vadim Beglov
Vadim Beglov

Reputation: 438

How to delete container after building in docker-compose?

I have a frontend-container, which builds the react-app as static files in global volume. I don't need this container after building anymore. How to delete it after docker-compose build?

Dockerfile:

FROM node:16-alpine3.12

WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN yarn build

RUN rm -rf node_modules

Upvotes: 0

Views: 1844

Answers (1)

lcomrade
lcomrade

Reputation: 326

I would recommend that you use a multi-stage build. It works like this:

  1. Stage 1: install the compiler and compile
  2. Stage 2: build the final image

Stages 1 and 2 take place in different containers.

You should have it like this:

# Stage 1
FROM node:16-alpine3.12 as build

WORKDIR /build
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
COPY . .
RUN yarn build

RUN rm -rf node_modules


# Stage 2
FROM alpine:latest as run

WORKDIR /app

COPY --from=build /build/* .

Read more in the official documentation: https://docs.docker.com/develop/develop-images/multistage-build/

Upvotes: 1

Related Questions