R
R
Roman Dubinin2018-08-23 05:44:11
Docker
Roman Dubinin, 2018-08-23 05:44:11

How to run a docker container with site statics?

There is a simple Dockerfile that builds the project

FROM node:8
WORKDIR /web

COPY package.json yarn.lock ./
RUN yarn

COPY . ./
RUN yarn build

yarn builddumps site statics into /web/dist.
As far as I understand, now we need to make another container with nginx and connect the folder with statics to it through volume, but how then to make sure that the container does not turn off immediately after launch? There is no command in it ...
Or do you still need to put nginx in this container and, after assembly, use it to distribute the site? Wouldn't that be against Docker's philosophy that each container should do its job?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Dubinin, 2018-10-16
@romash

The answer turned out to be much simpler and more beautiful than I expected.
Since version 17.05, docker has such a wonderful thing as Multi-stage build . It allows you to first make a container with nodejs in one dockerfile, build statics in it, and then create another container with nginx, into which to copy the assembly artifacts from the first one. In this case, the resulting image will not contain data from the nodejs image and sources (unless you explicitly copy them in the second step), which allows you to make it as light as possible.

FROM node:8 as build
WORKDIR /web

COPY package.json yarn.lock ./
RUN yarn

COPY . ./
RUN yarn build


FROM nginx:alpine
WORKDIR /usr/share/nginx/html
RUN rm -rf *
COPY --from=build /web/dist .

H
Hikmat Abdunabiev, 2018-08-23
@Khikmat

Try docker-compose.
Here is an example docker-compose.yml file:

version: "3"
services:
    my-app:
        image: my-app
        ports:
            - 3000:3000
        volumes:
            - dist:/web/dist
        networks:
            - app-network 
    nginx:
        image: nginx
        depends_on:
            - my-app
        ports: 
            - 80:80
        volumes:
            - dist:/usr/share/nginx/html
        networks:
            - app-network

volumes:
    dist:
networks:
    app-network:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question