Skip to content

Commit

Permalink
build: bash shell for production docker builds (#6469)
Browse files Browse the repository at this point in the history
* build: bash shell for production docker builds

This adds a bash shell for production docker builds, and renames the previous `shell` service to `dev-shell`.

* add the node_modules volume to the project app

* Separate node_modules volumes for production and development

* Optimise the Docker build

- Add the lockfile last, so that previous layers are cached.
- Copy files from the builder to the production app runner.

* Persist node_modules for the production apps

* More minor adjustments to the root Readme

---------

Co-authored-by: Delilah <[email protected]>
  • Loading branch information
eatyourgreens and goplayoutside3 authored Nov 25, 2024
1 parent 1862ade commit 496e883
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ WORKDIR /usr/src/

ADD package.json /usr/src/

ADD yarn.lock /usr/src/

COPY .yarn /usr/src/.yarn

ADD .yarnrc /usr/src/
Expand All @@ -40,6 +38,8 @@ ADD lerna.json /usr/src/

COPY ./packages /usr/src/packages

ADD yarn.lock /usr/src/

RUN chown -R node:node .

USER node
Expand All @@ -64,16 +64,16 @@ RUN mkdir -p /usr/src

WORKDIR /usr/src/

ADD package.json /usr/src/

ADD yarn.lock /usr/src/
COPY --from=builder /usr/src/package.json /usr/src/package.json

COPY .yarn /usr/src/.yarn
COPY --from=builder /usr/src/.yarn /usr/src/.yarn

ADD .yarnrc /usr/src/
COPY --from=builder /usr/src/.yarnrc /usr/src/.yarnrc

COPY --from=builder /usr/src/packages ./packages

COPY --from=builder /usr/src/yarn.lock /usr/src/yarn.lock

RUN --mount=type=cache,id=fem-runner-yarn,uid=1000,gid=1000,target=/home/node/.yarn YARN_CACHE_FOLDER=/home/node/.yarn yarn install --production --frozen-lockfile --ignore-scripts --prefer-offline

RUN rm -rf /usr/src/packages/lib-react-components/src
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ docker compose build
docker compose up -d
# shut down the running containers when you're finished
docker compose down
# run this if you need a shell inside the running container
docker compose run --rm shell
# run this if you need a shell inside the dev container
docker compose run --rm dev-shell
# run this for a shell inside the production container
docker compose run --rm prod-shell
```

You can supply a service name (from `docker-compose.yml`) to `docker compose` if you only want to run a single service eg.
Expand All @@ -90,14 +92,16 @@ docker compose build fe-project
docker compose up -d fe-project
```

Development environments for individual packages can be run from the package directories. For example:
Development environments for individual packages can be run from the package directories. See the READMEs in individual packages for detailed instructions. For example:

```sh
cd packages/app-project
docker-compose up
docker compose build
docker compose up -d
docker compose down
```

to run a development server for the project app. See the READMEs for individual packages for detailed instructions.
Tip: If you're an occasional Docker Desktop user, remember to `docker image prune`.

### With Node and yarn

Expand Down
25 changes: 21 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
services:
shell:
prod-shell:
image: front-end-monorepo_prod:latest
volumes:
- node_modules-prod:/usr/src/node_modules
build:
context: ./
args:
- NODE_ENV=production
- PANOPTES_ENV=production
- NEXT_TELEMETRY_DISABLED=1
- APP_ENV=development
command:
- "/bin/sh"
dev-shell:
image: front-end-monorepo_dev:latest
volumes:
- node_modules:/usr/src/node_modules
- node_modules-dev:/usr/src/node_modules
build:
context: ./
target: builder
Expand All @@ -12,6 +25,8 @@ services:
- "/bin/sh"
fe-project:
image: front-end-monorepo_prod:latest
volumes:
- node_modules-prod:/usr/src/node_modules
build:
context: ./
args:
Expand All @@ -30,6 +45,8 @@ services:
- "3002:3000"
fe-root:
image: front-end-monorepo_prod:latest
volumes:
- node_modules-prod:/usr/src/node_modules
build:
context: ./
args:
Expand All @@ -46,6 +63,6 @@ services:
- PORT=3000
ports:
- "3003:3000"

volumes:
node_modules:
node_modules-dev:
node_modules-prod:
4 changes: 4 additions & 0 deletions packages/app-project/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- ./pages:/usr/src/packages/app-project/pages
- ./src:/usr/src/packages/app-project/src
- ./stores:/usr/src/packages/app-project/stores
- node_modules-dev:/usr/src/node_modules
storybook:
image: front-end-monorepo_dev:latest
entrypoint:
Expand All @@ -34,3 +35,6 @@ services:
- ./pages:/usr/src/packages/app-project/pages
- ./src:/usr/src/packages/app-project/src
- ./stores:/usr/src/packages/app-project/stores
- node_modules-dev:/usr/src/node_modules
volumes:
node_modules-dev:
2 changes: 1 addition & 1 deletion packages/app-root/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ docker compose up -d
# stop the local services when you're finished
docker compose down
# run the tests
docker compose run --rm project test
docker compose run --rm root test
```

### Node
Expand Down

0 comments on commit 496e883

Please sign in to comment.