1. JTI - Docker
Goals
This exercise aims to containerize an application using Docker and use this container for the application's development.
Prerequisites
This exercise is intended for use concurrently with the theory part in this module. Start watching the recordings and pause when you reach Step #4.
You can then follow along and code with us. We're using neo4j and Python, but you should be able to translate this to MongoDB and Node.js (or any other environment you choose).
Important: Do not proceed to Step #5! That is for the next step.
Ensure you have Docker Desktop installed on your computer.
1. Pick the application
You can pick any application you like, but we recommend using the example application "Just Task It featuring Logging".
Create an empty repository jti-docker-1
on GitLab (preferably under https://gitlab.lnu.se/2dv013/student/<your username>/part-2-microservices/
).
Notes
- Ensure you have the necessary permissions to clone both repositories and perform the
git pull
command. - Be cautious when working with unrelated repository histories; review the changes to make sure everything is correctly merged.
Environment variables
You'll use the .env
file for environment variables during development. Rename example.env
to .env
and populate it.
Example:
Note: In the database connection string, use the DNS name "mongodb". It's crucial to use this name when creating the Docker container that houses MongoDB.
2. Docker
Follow the Theory and demos (Steps #1-#4).
Key Points to Remember
Create a Network
Run MongoDB Container
Start MongoDB detached on the created network.
Create a Dockerfile
Follow this guide for containerizing a Node.js app.
Development Mode Command
Use the following command to start Just Task It in dev mode:
Mounting Source Folder
Mount your
src
folder to/usr/src/app/src
in the container.- Unix-like systems:
$(pwd)
- Windows (Bash):
$(pwd -W)
- Windows (PowerShell):
${PWD}
- Unix-like systems:
Environment Variables
Since
.env
is ignored, use--env-file
when starting Docker.
Dockerfile Example
In a multi-stage Dockerfile, the base stage installs application dependencies separately from the code to optimize caching and build speed. The final stage creates a lightweight runtime environment by copying only necessary files, reducing image size and enhancing security.
MongoDB Setup
MongoDB Container: It runs as a separate container. Use the
docker run
command to start it on the network.Environment Variables: Define them in the Docker host before running
docker compose up
.
This setup ensures that you have a fully containerized environment with MongoDB and your Node.js application ready to develop and test.
.dockerignore
Instead of copying all files into the container, use a .dockerignore
file to choose the files you want to copy. This process is known as "allowlisting".
Example:
Test the Docker container
Open your web browser and visit http://localhost:3000
. The application should be up and running. You should now be able to modify files in the src
folder on your local machine. nodemon
in the container will detect these changes and restart the application. Refresh your browser to confirm that changes to the front page are visible.
Next step
Setting up networks manually, building containers, and running them can be complex, especially when you have multiple services and dependencies. To manage this complexity, the next step will introduce you to a tool called Docker Compose.