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 Demo #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 Demo #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 on GitLab (preferably under https://gitlab.lnu.se/2dv013/student/<your username>/part-2-architecture/).

Copy the files from the example project into your repo.

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 guides (Demo #1-#4).

Key points to remember:

  • Create a network "jti-network" (or any name you choose).
  • Run MongoDB detached with the name (--name mongodb) on the network created above. (You'll skip mounting volumes for now.)
  • Create a Dockerfile file. See the documentation for a complete example for the Node.js platform.([https://nodejs.org/en/docs/guides/nodejs-docker-webapp/]).
  • Use CMD [ "npx", "nodemon", "-e" , "js,json,ejs,html,css", "--inspect", "src/server.js"] to start Just Task It in dev mode in the container.
  • When starting the web application Just Task It, mount your src folder (absolute path, you'll need the current working directory) to /usr/src/app/src in the container.
    • Depending on your operating system, the current working directory is different:
      • Unix-like systems (Linux, macOS, WSL2):
        • $(pwd)
      • Windows:
        • Bash: $(pwd -W)
        • PowerShell ${PWD}
  • Environment. Since we are ignoring the .env-file in .dockerignore (see below), we need to use the flag --env-file when starting docker to read the .env-file.

.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:8080. 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.

Proceed to the next step