From bc2e7a7366bd24edc686ec5da567b1e5f4ca5b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Mon, 8 Mar 2021 17:02:04 +0100 Subject: [PATCH] chore: optimise Dockerfile So far, the Dockerfile built hydrogen on the server running the image, instead of building it during the building of the image. This blew up the image size immensely and caused node+yarn to run in the resulting image. This new Dockerfile builds hydrogen in a separate build stage and then moves the target directory into an nginx based container image, which takes care of serving the target webroot. The existing Dockerfile has been moved to Dockerfile-dev for usage as a development environment. The docs have been adjusted accordingly. Additionally, this switched from a fixed alpine version of the node image to the latest alpine version, and changed the container image references in the `FROM` statements to use the fully qualified references including the registry domain. --- .dockerignore | 2 ++ Dockerfile | 14 +++++++------ Dockerfile-dev | 7 +++++++ doc/docker.md | 56 +++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile-dev diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..2c085d1d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules +target diff --git a/Dockerfile b/Dockerfile index bc893b3e..6acd7d10 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,9 @@ -FROM node:alpine3.12 +FROM docker.io/node:alpine as builder RUN apk add --no-cache git -COPY . /code -WORKDIR /code -RUN yarn install -EXPOSE 3000 -ENTRYPOINT ["yarn", "start"] +COPY . /app +WORKDIR /app +RUN yarn install \ + && yarn build + +FROM docker.io/nginx:alpine +COPY --from=builder /app/target /usr/share/nginx/html diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 00000000..31877ee1 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,7 @@ +FROM docker.io/node:alpine +RUN apk add --no-cache git +COPY . /code +WORKDIR /code +RUN yarn install +EXPOSE 3000 +ENTRYPOINT ["yarn", "start"] diff --git a/doc/docker.md b/doc/docker.md index 92f33ae8..910938f0 100644 --- a/doc/docker.md +++ b/doc/docker.md @@ -1,22 +1,58 @@ +## Warning + Usage of docker is a third-party contribution and not actively tested, used or supported by the main developer(s). -Having said that, you can also use Docker to create a local dev environment. +Having said that, you can also use Docker to create a local dev environment or a production deployment. + +## Dev environment In this repository, create a Docker image: - docker build -t hydrogen . +``` +docker build -t hydrogen-dev -f Dockerfile-dev . +``` Then start up a container from that image: - docker run \ - --name hydrogen-dev \ - --publish 3000:3000 \ - --volume "$PWD":/code \ - --interactive \ - --tty \ - --rm \ - hydrogen +``` +docker run \ + --name hydrogen-dev \ + --publish 3000:3000 \ + --volume "$PWD":/code \ + --interactive \ + --tty \ + --rm \ + hydrogen-dev +``` Then point your browser to `http://localhost:3000`. You can see the server logs in the terminal where you started the container. To stop the container, simply hit `ctrl+c`. + +## Production deployment + +### Build or pull image + +In this repository, create a Docker image: + +``` +docker build -t hydrogen . +``` + +Or, pull the docker image from GitLab: + +``` +docker pull registry.gitlab.com/jcgruenhage/hydrogen-web +docker tag registry.gitlab.com/jcgruenhage/hydrogen-web hydrogen +``` + +### Start container image + +Then, start up a container from that image: + +``` +docker run \ + --name hydrogen \ + --publish 80:80 \ + hydrogen +```