V
V
Vadim2020-12-10 17:11:40
Docker
Vadim, 2020-12-10 17:11:40

How does a 2-image Dockerfile work?

Hello everyone,

I have this Dockerfile:

FROM node:13 as node

WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
COPY docker-additions/.npmrc /app/

RUN npm install
COPY ./ /app/

RUN npm run config
RUN npm run build

FROM nginx:1.18.0-alpine
COPY --from=node /app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf


Can you explain
1) why is it used in the first line as node (FROM node:13 as node)
2) how can you use FROM nginx:1.18.0-alpine if FROM already exists? Does it pull packages from the second base image to the first or what?

all the best,
Vadim

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mureevms, 2020-12-10
@Viji

FROM node:13 as node
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
COPY docker-additions/.npmrc /app/
RUN npm install
COPY ./ /app/
RUN npm run config
RUN npm run build

At this point, it uses a container with a node:13 base image to build the application.
FROM nginx:1.18.0-alpine
COPY --from=node /app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

It uses nginx:1.18.0-alpine as the base image and copies what was built in the previous container into the container with the web server.
A sort of CI for Docker. It's comfortable.
1. A name is assigned to the layer from which the files are then copied on line COPY --from=node /app/dist/ /usr/share/nginx/html
2. On the first FROM, the assembly takes place. If you use one, you will either have to put nginx in the first one, or nodejs in the second one. And it's just an intermediate layer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question