We can see the node_modules folder takes 191MB of space. I use a tool called Dive to explore the content of a Docker image. Open your browser and navigate to Reduce the size using EsbuildĪfter building the application, we still need the production dependencies to run the application, which is why the Docker image contains a node_modules folder. Run the command below to start the container of the projectĭocker run -it -p 4500:4500 -network node-app-network -name node-rest-api -rm -env-file. env that will contain the environment variables to inject when starting a Docker container of our application image. Since the Node project interacts with MongoDB, so we need to start a container with the command below:ĭocker run -d -network node-app-network -e MONGO_INITDB_ROOT_USERNAME=app_user -e MONGO_INITDB_ROOT_PASSWORD=app_password -name mongodb mongo:5.0Ĭreate a file. Let's try to run the image and verify the application work as expected. View the image detail by running docker image ls. ![]() Run the command below to build the image: Finally, we define the command to run when the container starts. We also set the ownership of the files to the user node (automatically created in the Node alpine image). In the second stage app, we copy the package.json and the content of the folder generated by the project built in the previous stage. In the first stage builder, we copy the project file from the host, install the dependencies and build and transpile the files from Typescript to JavaScript. This file has two stages builder and app and use Node alpine 16 as the base image. RUN yarn install -frozen-lockfile -production ![]() Make the image build agnostic from any operating systemĪt the root project directory, create a file called Dockerfile and add the code below:ĬOPY -chown=node:node -from=builder /app/package.json /appĬOPY -chown=node:node -from=builder /app/build/ /app.We will take advantage of the Docker multi-stage build to: We will run the application after we build the Docker image. You can follow the instructions in README to run the project locally.
0 Comments
Leave a Reply. |