1. JTI - docker
Goals
The goals of this exercise is to containerize an application using Docker and to be able to use this for development of said application.
Prerequisites
This exercise is constructed so that you can use it while watching the theory part in this module. Start watching the recordings and when you reach Demo 4, you can probably follow along and code along. Morgan is using neo4j and Python, but you should be able to translate this to mongodb and Node.js. (or what ever environment you choose)
Do not continue to Demo 5! That is for the next exercise.
You need to have Docker Desktop installed on your computer if you do not have already.
1. Pick the application
You can pick any application you like, but we recommend using the example application "Just Task It".
Create an empty repository on gitlab-lnu (perferably under http://gitlab.lnu.se/2dv013/student/XX222YY/part-2-architecture/).
Copy the files from the example project into your repo.
git pull git@gitlab.lnu.se:1dv026/content/examples/example-just-task-it.git --allow-unrelated-histories
Environment variables
Under development we will use the .env-file for environment variables. Make sure to rename the example.env
to .env
and populate it.
Example:
(Note! in the connection string to the database, we use the dns-name "mongodb". It is important that we use this namne when creating the docker container that houses mongodb.)
2. Docker
Follow the guides (Demo #1-#4).
Some things to note:
- Do not forget to create a network "demonet" (or what name you choose)
- Run mongodb detached with the name (--name mongodb) on the network created above. (We skip mounting volumes for now)
- Create a Dockerfile. Se the documentation for a complete example for the node platform.](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/).
- Use
CMD [ "npm", "run", "dev" ]
to start Just Task It in devmode in the container. - When starting Just Task it, make sure to mount your src-folder (absolute path) against "/usr/src/app/src" in the container. (
pwd
will show you your active working directory)
.dockerignore
We do not need to copy all files into the container. With the use of a .dockerignore-file, we can select what we want to copy. (allowlist).
Example:
Test it!
Visit http://localhost:8080 and the application should be up and running.
No you should be able to change files in the src-folder on your local machine, nodemon in the container will see this and the application restarts. Reload the browser to confirm that changes to, for example, the front page are visible.
Next step
It is a bit tricky with setting up networks manually, building the containers and running them. This is espesially true when we have more services and more dependencies. We need to add a layer on top of Docker. Enter docker-compose! --->