<!-- TITLE: Jenkins Docker --> <!-- SUBTITLE: A quick summary of Jenkins Docker --> # Introduction This is simple **README** includes instructions about how to work with Jenkins and Docker for starting application remotely or locally. # Table of contents 1. [List of main files](#List_of_main_files) 2. [Instruction how work with Jenkins](#Instruction_how_work_with_Jenkins) 1. [Backend_Tests and Frontend_Tests jobs](#Backend_Tests_and_Frontend_Tests_jobs) 2. [Deploy_Backend and Deploy_Frondend Jobs](#Deploy_Backend_and_Deploy_Frondend_Jobs) 3. [About environments](#About_environments) 3. [Run application locally](#Run_application_locally) 1. [Working with Postgres in Docker](#Working_with_Postgres_in_Docker) 2. [Run application in different setup](#Run_application_in_different_setup) 3. [Updating docker images](#Updating_docker_images) <a name="List_of_main_files"><h2>List of main files</h2></a> 1. **Jenkinsfile** - *describes all stages for deploy and test application in Jenkins* 2. **.env** - *store all environments for start application* 3. **docker-compose.yml** - *describes list of docker images and main settings for it* 4. **Proxy.Dockerfile** - *describes main settings for Proxy container* 5. **Build.Dockerfile** - *describes main settings for Backend container* 6. **Test.Dockerfile** - *describes main settings for Test container* <a name="Instruction_how_work_with_Jenkins"><h2>Instruction how work with Jenkins</h2></a> For start work with Jenkins, connect to [Jenkins server](https://jenkins.prokolab.com). All main jobs are located on main page. There several jobs that you can explore in Jenkins: * Backend_Tests * Frontend_Tests * Deploy_Backend * Deploy_Frondend <a name="Backend_Tests_and_Frontend_Tests_jobs"><h3>Backend_Tests and Frontend_Tests jobs</h3></a> Checks backend and frontend on errors by creating new docker containers and with test environments. Destroy containers after finishing all tests. Full configuration script of what tests are running could see in Backend_Tests (or Frontend_Tests) -> Configure <a name="Deploy_Backend_and_Deploy_Frondend_Jobs"><h3>Deploy_Backend and Deploy_Frondend Jobs</h3></a> Deploying Backend and Frontend images and pushes in to dev server. <a name="About_environments"><h3>About environments</h3></a> All environments stage in config file in Jenkins [here](https://jenkins.prokolab.com/configfiles/) There are three files **test-env**, **staging-env** and **prod-env** 1. **test-env** *file contains environments that will used in Backend_Tests and Frontend_Tests jobs for running tests* 2. **staging-env** *file contains environments that will used in Deploy_Backend and Deploy_Frondend jobs for deploying application* 3. **prod-env** *file contains environments that will used in Deploy_Backend and Deploy_Frondend jobs for production* <a name="Run_application_locally"><h2>Run application locally</h2></a> List of commands that could help run application on local machine: 1. Be sure that Docker and Docker compose are installed. if not use this links [Install Docker](https://docs.docker.com/install/linux/docker-ce/ubuntu/) and [Install Docker Compose](https://docs.docker.com/compose/install/) 2. Create directory on local machine by command: `sudo mkdir *your directory name*` 3. Give permitions to directory: `sudo chown *your username*:*your username* .` 4. Download docker-compose.yml and .env.sample file from repo: `git clone *repo_url* .` Also user `-b *branch name*` key to put name of needed branch 5. Rename .env.sample to .env: `sudo cp .env.sample .env` 6. Be sure that docker-compose.yml and .env files are in the same directory. 7. Before run docker-compose use this command an once `sudo docker login hub.shakuro.com` *login - shakuro, pass - ask your SysAdm* 8. Run docker compose: `sudo docker-compose up -d` *-d means run continers as a daemon* 9. If need to explore logs of container: `sudo docker logs *container name*` 10. If need to explore what environments container is using: `sudo docker inspect *container name*` 11. Connect to running container: `sudo docker exec -ti *container name* bash ` or `sudo docker exec -ti *container name* sh` 12. Restart container: `sudo docker restart *container name*` 13. Run command in container: `sudo docker exec -ti *container name* *linux command*`. For example: `sudo docker exec -ti nginx cat /etc/nginx/sites-available/default`. Also it could be run inside of container. 14. Copy file to container: `sudo docker cp path/to/file *container name*:/path/to/directory/or/file` <a name="Working_with_Postgres_in_Docker"><h3>Working with Postgres in Docker</h3></a> There is two ways to work with Postgres, run commands inside of container or outside of container If need run commands inside of container: 1. Connect to container `sudo docker exec -ti *name of postgres container* sh` 2. Connect to psql user `su postgres` 3. Connect to database `psql -U *database user*` *name of database could be find in .env file* 4. Create new database `psql -U *database user* *database name* -c 'create database *new_database_name*'` 5. Drop existing database `psql -U *database user* *database name* -c 'drop database *new_database_name*'` 6. Give all permission to existing database `psql -U *database user* *database name* -c 'grant all on database *new_database_name* to *database user*'` Run commands outside of container are almost similar 1. `docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'create database *new_database_name*;'` 2. `docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'drop database *new_database_name*'` 3. `docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'grant all on database *new_database_name* to *database user*'` <a name="Run_application_in_different_setup"><h3>Run application in different setup</h3></a> For start application with different backend: 1. Open list of running containers: `sudo docker ps` 2. Stop backend container: `sudo docker stop *container name*` 3. Be sure that **new** backend runs on *0.0.0.0* address. For start application with different frontend: 1. Open list of running containers: `sudo docker ps` 2. Stop frontend container: `sudo docker stop *container name*` 3. Be sure that **new** frontend runs on *0.0.0.0* address. <a name="Updating_docker_images"><h3>Updating docker images</h3></a> 1. Move to directory with *docker-compose.yml* file 2. Run `sudo docker pull image_name:tag` *Upgrade selected image from regisrty. Expample: sudo docker pull hub.shakuro.com/test/backend:latest* 3. Or `sudo docker-compose pull name` *Upgrade selected image included in docker-compose.yml file. Expample: sudo docker-compose pull backend*