Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(KUI-1542): update dependencies (upgrade node version from 18 to 20) & improve the code for running locally using docker #390

Merged
merged 7 commits into from
Dec 9, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dist

# Docker
*.env*
Dockerfile-dev

# KTH Node Web Common imported files
server/views/system/error.handlebars
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM kthregistry.azurecr.io/kth-nodejs-18:latest
FROM kthregistry.azurecr.io/kth-nodejs-20:latest
LABEL maintainer="KTH-studadm [email protected]"

WORKDIR /application
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,40 @@ Add to .vscode file launch.json:
}
```

## Run Locally Using Docker

You can run the project locally using Docker for an isolated and consistent environment. Follow these steps:

#### Build Docker Image

First, build the Docker image by running the following command:

If you are using mac change `docker build -f Dockerfile-dev -t "$IMAGE_NAME"` with `docker build --platform linux/arm64/v8 -f Dockerfile-dev -t "$IMAGE_NAME"`.


```sh
npm run docker:build
```

This will execute the `docker-build-image.sh` script in development mode `(dev)` this script will create Dockerfile-dev file which will be used for running the project locally using docker.

#### Run Docker Container

After the image has been built, you can start the Docker container using the following command:

```sh
npm run docker:run
```

This will execute the `docker-run-image.sh` script in development mode `(dev)`, running the application locally in Docker.

The application now will be accessible at http://localhost:3000/student/kurser/kurs/:courseCode.

#### Notes

- Run `az acr login --name kthregistry` before running the scripts.
- Ensure your .env file is properly set up in the project root to provide the necessary environment variables to the Docker container.

## Deploy

The deployment process is described in [Build, release, deploy](https://confluence.sys.kth.se/confluence/x/aY3_Ag). Technical details, such as configuration, is described in [How to deploy your 🐳 application using Cellus-Registy](https://gita.sys.kth.se/Infosys/cellus-registry/blob/master/HOW-TO-DEPLOY.md) and [🔧 How To Configure Your Application For The Pipeline](https://gita.sys.kth.se/Infosys/cellus-registry/blob/master/HOW-TO-CONFIGURE.md).
Expand Down
15 changes: 11 additions & 4 deletions docker-build-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ echoYellow "| Building the Docker image for development env |"
echoYellow "|--------------------------------------------------------|\n"

IMAGE_NAME="kursinfo-web-image"
DOCKERFILE="Dockerfile-dev"


if [ "$ENV" == "dev" ]; then

echo
echoYellow " 1. Stop previous Docker image: a name tag is $IMAGE_NAME\n"
echoYellow " 1. Creating Dockerfile-dev for development environment\n"
karlandindrakryggen marked this conversation as resolved.
Show resolved Hide resolved

cp Dockerfile "$DOCKERFILE"
sed -i '' 's/CMD \["npm", "start"\]/CMD ["npm", "run", "docker:start"]/g' "$DOCKERFILE"

echo
echoYellow " 2. Stop previous Docker image: a name tag is $IMAGE_NAME\n"
docker stop "$IMAGE_NAME"

echo
echoYellow " 2. Remove previous Docker image: a name tag is $IMAGE_NAME\n"
echoYellow " 3. Remove previous Docker image: a name tag is $IMAGE_NAME\n"
docker rmi "$IMAGE_NAME"

echo
echoYellow " 3. Build Docker image: a name tag is $IMAGE_NAME\n"
echoYellow " 4. Build Docker image: a name tag is $IMAGE_NAME\n"
docker build -f Dockerfile-dev -t "$IMAGE_NAME" .

echo
echoYellow " 4. List images\n"
echoYellow " 5. List images\n"
docker images
fi
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build-dev": "bash ./build.sh dev",
"docker:build": "bash ./docker-build-image.sh dev",
"docker:run": "bash ./docker-run-image.sh dev",
"docker:start": "bash -c 'cat /KTH_NODEJS; NODE_ENV=development node app.js'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the naming here. The only difference between "docker:start" and just "start" is that NODE_ENV is development instead of production. I was a little confused about this, because there is nothing docker-related by this at all. I understand that you are using this in a dockerfile, but the regular "start" is also use in a dockerfile.

My first thought was to just rename this one to "start-dev", but then I realised we already have a "start-dev" for other purposes.

But maybe the bigger question maybe is: Why do we need to start it in dev mode? I think that one of the big advantages of running in docker here would be to test it as close as possible to a real "production" environment. What benefits do we get from starting with node_env=dev here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the naming here. The only difference between "docker:start" and just "start" is that NODE_ENV is development instead of production. I was a little confused about this, because there is nothing docker-related by this at all. I understand that you are using this in a dockerfile, but the regular "start" is also use in a dockerfile.

My first thought was to just rename this one to "start-dev", but then I realised we already have a "start-dev" for other purposes.

But maybe the bigger question maybe is: Why do we need to start it in dev mode? I think that one of the big advantages of running in docker here would be to test it as close as possible to a real "production" environment. What benefits do we get from starting with node_env=dev here?

It is great that you mentioned it,
I shared my thoughts in the other comment regarding the dev mode for running docker.

regarding the new script that I have added I was thinking to make a script only for docker in dev mode but then I thought using start-dev would be better than creating a new one.

Thanks for your comment ⭐️

"test": "NODE_ENV=test jest",
"test-win": "cross-env NODE_ENV=test jest",
"test:watch": "jest --watch",
Expand Down Expand Up @@ -102,7 +103,7 @@
"webpack-cli": "^5.1.4"
},
"engines": {
"node": "18"
"node": "20"
},
"jshintConfig": {
"maxerr": 5
Expand Down
Loading