Jenkins Docker
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
List of main files
- Jenkinsfile - describes all stages for deploy and test application in Jenkins
- .env - store all environments for start application
- docker-compose.yml - describes list of docker images and main settings for it
- Proxy.Dockerfile - describes main settings for Proxy container
- Build.Dockerfile - describes main settings for Backend container
- Test.Dockerfile - describes main settings for Test container
Instruction how work with Jenkins
For start work with Jenkins, connect to Jenkins server.
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
Backend_Tests and Frontend_Tests jobs
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
Deploy_Backend and Deploy_Frondend Jobs
About environments
All environments stage in config file in Jenkins here
There are three files test-env, staging-env and prod-env
- test-env file contains environments that will used in Backend_Tests and Frontend_Tests jobs for running tests
- staging-env file contains environments that will used in Deploy_Backend and Deploy_Frondend jobs for deploying application
- prod-env file contains environments that will used in Deploy_Backend and Deploy_Frondend jobs for production
Run application locally
List of commands that could help run application on local machine:
- Be sure that Docker and Docker compose are installed. if not use this links Install Docker and Install Docker Compose
- Create directory on local machine by command:
sudo mkdir *your directory name* - Give permitions to directory:
sudo chown *your username*:*your username* . - 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 - Rename .env.sample to .env:
sudo cp .env.sample .env - Be sure that docker-compose.yml and .env files are in the same directory.
- Before run docker-compose use this command an once
sudo docker login hub.shakuro.comlogin - shakuro, pass - ask your SysAdm - Run docker compose:
sudo docker-compose up -d-d means run continers as a daemon - If need to explore logs of container:
sudo docker logs *container name* - If need to explore what environments container is using:
sudo docker inspect *container name* - Connect to running container:
sudo docker exec -ti *container name* bashorsudo docker exec -ti *container name* sh - Restart container:
sudo docker restart *container name* - 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. - Copy file to container:
sudo docker cp path/to/file *container name*:/path/to/directory/or/file
Working with Postgres in Docker
There is two ways to work with Postgres, run commands inside of container or outside of container
If need run commands inside of container:
- Connect to container
sudo docker exec -ti *name of postgres container* sh - Connect to psql user
su postgres - Connect to database
psql -U *database user*name of database could be find in .env file - Create new database
psql -U *database user* *database name* -c 'create database *new_database_name*' - Drop existing database
psql -U *database user* *database name* -c 'drop database *new_database_name*' - 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
docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'create database *new_database_name*;'docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'drop database *new_database_name*'docker exec -it *name of postgres container* psql -U *database user* *database name* -c 'grant all on database *new_database_name* to *database user*'
Run application in different setup
For start application with different backend:
- Open list of running containers:
sudo docker ps - Stop backend container:
sudo docker stop *container name* - Be sure that new backend runs on 0.0.0.0 address.
For start application with different frontend:
- Open list of running containers:
sudo docker ps - Stop frontend container:
sudo docker stop *container name* - Be sure that new frontend runs on 0.0.0.0 address.
Updating docker images
- Move to directory with docker-compose.yml file
- Run
sudo docker pull image_name:tagUpgrade selected image from regisrty. Expample: sudo docker pull hub.shakuro.com/test/backend:latest - Or
sudo docker-compose pull nameUpgrade selected image included in docker-compose.yml file. Expample: sudo docker-compose pull backend