Chapter 4 Deployment
The current deployment strategy targets a server or virtual machine (VM) equipped with Docker.
We use several Docker containers that communicate with each other via external Docker network.
Actual production server is located at 193.106.181.3.
Actual production database server is located at xdcren06.eurac.edu.
Overview:
- Docker container with PostgreSQL database (not necessarily running on the same VM)
- Encome app: multiple Docker containers (front-end, back-end, celery, redis)
- Docker container with NGINX
- all containers(except database if running on external server) use the same Docker network.
4.1 Prerequisites
In order to perform deployment procedure, you need a GitLab account and a Docker installed on your local machine.
The project repository is located at https://gitlab.inf.unibz.it/eeb/infinite_bep_52016. Thereafter, all references to the folders are taken from the root folder of this project.
4.1.1 GitLab login
We store all Docker images on GitLab registry, so you will need to log in.
From GitLab, create new Project Access Token and note it somewhere (https://docs.gitlab.com/ee/user/project/settings/project_access_tokens.html). From local or server terminal run:
:4567 docker login gitlab.inf.unibz.it
Then use your GitLab username and Project Access Token to authenticate.
4.2 NGINX configuration
Since all containers communicate on the same Docker network, we need to create it.
First, connect to production server. From terminal run:
<username>@193.106.181.3 ssh
# Exsample: 'ssh osomova@193.106.181.3'
Create Docker network if not exists:
docker network create nginx_network
Copy ./nginx folder. From local terminal run:
-r ./nginx <username>@193.106.181.3:/home/<username>/ rsync
Move ./nginx folder to /home/tools.eeb/. From server terminal run:
/home/tools.eeb/ sudo mv nginx
Finally, start NGINX container. From server terminal cd into nginx and run:
-compose up -d --build docker
4.3 Database configuration
We use PostgreSQL database stored in Docker container.
See MATRYCS_ECM/frontend/database_connection/readme.md
Overview:
- Docker container with PostgreSQL database
- Docker container with Flask app used for database definition and migrations
4.3.1 Flask app
In order to keep all database schema changes in version control, we use a flask app from ./iso52016_database/migrations_app.
In models.py you can add/change/delete models corresponding to the database tables. See SQlAlchemy (https://docs.sqlalchemy.org/en/20/dialects/postgresql.html)
Then, you can apply this changes to iso_52106 database: See Flask-Migrate (https://flask-migrate.readthedocs.io/en/latest/)
This app will generate migration script that needs to be added to version control.
4.3.2 PostgreSQL database
First, from local and server terminal log in to GitLab (4.1.1).
Now, let’s build and push database image. From local terminal cd into ./iso52016_database/database and run:
-t gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/iso_52016:tag . docker build
:4567/eeb/infinite_bep_52016/iso_52016:tag docker push gitlab.inf.unibz.it
Then, let’s build and push migration app image. From local terminal cd into ./iso52016_database/migration_app and run:
-t gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/iso_52016_app:tag . docker build
:4567/eeb/infinite_bep_52016/iso_52016_app:tag docker push gitlab.inf.unibz.it
Where tag is defined as in 4.5.1
From ./iso52016_database folder you need to copy:
- scripts folder
- docker-compose.yml (if you run DB on same VM that other containers, add NGINX network)
- db.env file (change password to secure one)
From local terminal cd into ./iso52016_database and run:
-r ./scripts <username>@xdcren06.eurac.edu:/home/<username>/iso52016_database/ rsync
-compose.yml <username>@xdcren06.eurac.edu:/home/<username>/iso52016_database/ rsync docker
<username>@xdcren06.eurac.edu:/home/<username>/iso52016_database/ rsync db.env
Move ./iso52016_database folder to /home/tools.eeb/iso52016_database and pull images. From server terminal run:
/home/tools.eeb/iso52016_database sudo mv iso52016_database
:4567/eeb/infinite_bep_52016/iso_52016:tag docker pull gitlab.inf.unibz.it
:4567/eeb/infinite_bep_52016/iso_52016_app:tag docker pull gitlab.inf.unibz.it
Finally, start Database container. From server terminal cd into ./iso52016_database and run:
/scripts/createdb.sh .
4.4 Encome Front-end and Back-end
Now, we need to build Encome front-end and back-end images.
First, from local and server terminal log in to GitLab (4.1.1).
Then, we start to build Encome’s back-end. From local terminal cd into infinite_bep_52016/backend and run:
-t gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/encome-backend:tag . docker build
Build Encome’s front-end. From local terminal cd into MATRYCS_ECM/frontend and run:
-t gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/encome-frontend:tag . docker build
Where tag is defined as in 4.5.1
Once the images are ready, we can push them. From local terminal run:
:4567/eeb/infinite_bep_52016/encome-backend:tag docker push gitlab.inf.unibz.it
:4567/eeb/infinite_bep_52016/encome-frontend:tag docker push gitlab.inf.unibz.it
Then, from production server pull all images:
:4567/eeb/infinite_bep_52016/encome-backend:tag docker pull gitlab.inf.unibz.it
:4567/eeb/infinite_bep_52016/encome-frontend:tag docker pull gitlab.inf.unibz.it
From root folder we need to copy to server:
- docker-compose.yml
- .evn file (generate new secret key)
So, run:
-compose.yml <username>@193.106.181.3:/home/<username>/ rsync docker
<username>@193.106.181.3:/home/<username>/ rsync .env
Move this files to /home/tools.eeb/. From server terminal run:
-compose.yml /home/tools.eeb/ sudo mv docker
/home/tools.eeb/ sudo mv .env
Make sure NGINX and database containers are running. Finally, start Encome containers. From server terminal cd into /home/tools.eeb and run:
-compose up -d --build docker
4.4.1 Update Encome Front-end and Back-end
In order to update Encome’s front-end or back-end:
From local terminal:
- log in to GitLab
- build front-end or/and back-end image/s
- push image/s to GitLab
From server terminal:
log in to GitLab
stop Encome running containers. From /home/tools.eeb run:
-compose stop docker
update tag in docker-compose.yml file
finally, start containers. From /home/tools.eeb run:
-compose up -d docker
4.5 Notes
Some useful information.
4.5.1 Image Versioning
All Docker images are tagged. The tag is defined as major.minor.bugfix. Example: 0.1.0
Thereafter, in all commands you have to replace ‘tag’ with the appropriate one. Example: New version contains bugfix, so tag changes from 1.2.6 to 1.2.7:
# instead of
# docker push gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/encome-backend:tag
# you have to run
docker push gitlab.inf.unibz.it:4567/eeb/infinite_bep_52016/encome-backend:1.2.7