Skip to content

Commit

Permalink
build(dev): add gems and node modules to image
Browse files Browse the repository at this point in the history
- set env variables in the docker-compose.dev.yml to allow using
  - prebuild images for the app and skip the building process
  - distinct postgres images
  - distinct named volume for the db and homedir(asdf, gems, etc)

- add .dockerenv.example file to help settings those variables
example
```
docker compose -f docker-compose.dev.yml --env-file .dockerenv.example up
```

- rework the preparation scripts to better separate node packages
  installation from ruby env installation. so that `yarn install`
  is only run by the js container

- introduce FULL_BUILD BRANCH args for preparing the dev docker
  image with preinstalled gems and nodejs packages for a given
  branch from complat/chemotion_ELN
  • Loading branch information
PiTrem committed Jan 21, 2025
1 parent a955344 commit 8c6122f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .dockerenv.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

DOCKER_DEV_IMAGE=complat/dev:v1.10.3-33-g9aef940c1
#DOCKER_PG_IMAGE=postgres:16
## User another named volume for homedir (asdf, gems, etc)
#VOLUME_NAME_HOMEDIR=homedir2
#VOLUME_NAME_DB=database2
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
nodejs 18.20.4 18.20.3
nodejs 18.20.5 18.20.4
ruby 2.7.8
26 changes: 26 additions & 0 deletions Dockerfile.chemotion-dev
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
FROM --platform=linux/amd64 ubuntu:jammy

ARG DEBIAN_FRONTEND=noninteractive
ARG FULL_BUILD=false
ARG BRANCH=main

RUN set -xe && apt-get update -yqqq --fix-missing && apt-get upgrade -y
RUN apt update && apt-get install -yqq --fix-missing bash ca-certificates wget apt-transport-https git gpg\
Expand Down Expand Up @@ -39,3 +41,27 @@ SHELL ["/bin/bash", "-c"]
# Even if asdf and the related tools are only installed by running run-ruby-dev.sh, we set the PATH variables here, so when we enter the container via docker exec, we have the path set correctly
ENV ASDF_DIR=/home/chemotion-dev/.asdf
ENV PATH=/home/chemotion-dev/.asdf/shims:/home/chemotion-dev/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV NODE_PATH=/home/chemotion-dev/node_modules

RUN git clone --single-branch --branch=$BRANCH https://github.com/complat/chemotion_ELN app

# Install asdf if FULL_BUILD is set to true
WORKDIR /home/chemotion-dev/app
RUN if [ "$FULL_BUILD" = "true" ]; then \
./prepare-asdf.sh; \
fi

# Install ruby and nodejs if FULL_BUILD is set to true
RUN if [ "$FULL_BUILD" = "true" ]; then \
./prepare-rubygems.sh; \
fi

# Install nodejs packages if FULL_BUILD is set to true
RUN if [ "$FULL_BUILD" = "true" ]; then \
echo -e "--modules-folder ${NODE_PATH}\n" > /home/chemotion-dev/app/.yarnrc; \
./prepare-nodejs.sh; \
./prepare-nodejspkg.sh; \
fi

WORKDIR /home/chemotion-dev
RUN rm -rf /home/chemotion-dev/app
10 changes: 6 additions & 4 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
#
services:
postgres:
image: 'postgres:14'
image: ${DOCKER_PG_IMAGE:-postgres:14'}
environment:
- 'POSTGRES_HOST_AUTH_METHOD=trust'
expose: # expose port to app container
- '5432'
ports: # expose port to host machine in case we want to use external db gui tools
- '5432:5432'
volumes:
- 'database:/var/lib/postgresql/data'
- ${VOLUME_NAME_DB:-database}:/var/lib/postgresql/data'

app:
build:
context: '.'
dockerfile: 'Dockerfile.chemotion-dev'
image: ${DOCKER_DEV_IMAGE:-}
depends_on:
- 'postgres'
healthcheck:
Expand All @@ -38,7 +39,7 @@ services:
ports: # expose default rails port to host machine
- "3000:3000"
volumes:
- 'homedir:/home/chemotion-dev/'
- ${VOLUME_NAME_HOMEDIR:-homedir}:/home/chemotion-dev/
- '.:/home/chemotion-dev/app'
working_dir: "/home/chemotion-dev/app"
command: "./run-ruby-dev.sh"
Expand All @@ -47,6 +48,7 @@ services:
build:
context: '.'
dockerfile: 'Dockerfile.chemotion-dev'
image: ${DOCKER_DEV_IMAGE:-}
depends_on:
app:
condition: service_healthy
Expand All @@ -56,7 +58,7 @@ services:
- 'SHAKAPACKER_DEV_SERVER_PORT=3035'
env_file: ./.env
volumes:
- 'homedir:/home/chemotion-dev/'
- ${VOLUME_NAME_HOMEDIR:-homedir}:/home/chemotion-dev/
- '.:/home/chemotion-dev/app'
ports: # expose webpacker dev server port to app container
- '3035:3035'
Expand Down
1 change: 1 addition & 0 deletions prepare-nodejs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

set -e

echo '>>> check nodejs version as set in package.json: install if mismatch, and correct .tool-versions'
# Get the currently installed Node.js version using asdf
CURRENT_NODE_VERSION=$(asdf current nodejs 2>/dev/null | awk '{print $2}')

Expand Down
1 change: 0 additions & 1 deletion prepare-nodejspkg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ fi

echo '>>> Installing JS packages...'
yarn install --production=false
yarn install

7 changes: 2 additions & 5 deletions prepare-ruby-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
# asdf-vm installation with ruby and nodejs plugins
export ASDF_BRANCH=v0.14.0
echo '>>> checking asdf installation'
./prepare-asdf.sh

# check nodejs version as set in package.json: install if mismatch, and correct .tool-versions'
echo '>>> check nodejs version as set in package.json: install if mismatch, and correct .tool-versions'
./prepare-nodejs.sh
./prepare-asdf.sh

# ruby gems installation
./prepare-rubygems.sh

# node packages installation
./prepare-nodejspkg.sh
# ./prepare-nodejspkg.sh

# prepare rails server
rm -f tmp/pids/server.pid
Expand Down
17 changes: 11 additions & 6 deletions run-js-dev.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/bin/bash

if command -v yarn; then
echo '>>> yarn is installed -> continue'

# if NODE_PATH is set, add --modules-folder option to .yarnrc and create a symlink to the node_modules folder
if [ -z "$NODE_PATH" ]; then
echo ">>> NODE_PATH is not set"
else
echo '>>> Missing yarn. Installing...'
npm install -g yarn
echo ">>> NODE_PATH is set to $NODE_PATH"
# echo ">>> Adding --modules-folder option to .yarnrc"
# echo -e "--modules-folder $NODE_PATH" > .yarnrc
# create a symlink unless it already exists
[ -L "${HOME}/app/node_modules" ] || ln -s "${NODE_PATH}/" "${HOME}/app/node_modules"
fi

echo '>>> Installing JS packages...'
yarn install

./prepare-nodejspkg.sh

echo "=========================================================================================================="
echo "THIS WILL FAIL UNTIL THE RUBY GEMS ARE INSTALLED BY run-ruby-dev.sh. JUST TRY AGAIN AFTER INSTALLING THEM."
Expand Down

0 comments on commit 8c6122f

Please sign in to comment.