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.
This commit is contained in:
Jan Christian Grünhage 2021-03-08 17:02:04 +01:00
parent ebca3935c5
commit bc2e7a7366
4 changed files with 63 additions and 16 deletions

2
.dockerignore Normal file
View file

@ -0,0 +1,2 @@
node_modules
target

View file

@ -1,7 +1,9 @@
FROM node:alpine3.12 FROM docker.io/node:alpine as builder
RUN apk add --no-cache git RUN apk add --no-cache git
COPY . /code COPY . /app
WORKDIR /code WORKDIR /app
RUN yarn install RUN yarn install \
EXPOSE 3000 && yarn build
ENTRYPOINT ["yarn", "start"]
FROM docker.io/nginx:alpine
COPY --from=builder /app/target /usr/share/nginx/html

7
Dockerfile-dev Normal file
View file

@ -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"]

View file

@ -1,13 +1,20 @@
## Warning
Usage of docker is a third-party contribution and not actively tested, used or supported by the main developer(s). 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: 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: Then start up a container from that image:
```
docker run \ docker run \
--name hydrogen-dev \ --name hydrogen-dev \
--publish 3000:3000 \ --publish 3000:3000 \
@ -15,8 +22,37 @@ Then start up a container from that image:
--interactive \ --interactive \
--tty \ --tty \
--rm \ --rm \
hydrogen hydrogen-dev
```
Then point your browser to `http://localhost:3000`. You can see the server logs in the terminal where you started the container. 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`. 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
```