docker and git implementation push-to-deploy

Posted by phpBuddy on Thu, 20 Jun 2019 19:38:44 +0200

background

Recent research into docker-based continuous delivery processes has allowed docker to build host-independent software environments that can push-to-deploy docker containers while pushing code to remote repositories through the git hooks mechanism (triggering a specific script in the $GIT-DIR/hooks directory when a particular event occurs in git repo).

practice

1. Environment:

  • A locally developed machine

  • A third-party code hosting platform, such as github, coding

  • A linux server of your own (for deployment)

  • A copy of the code for delivery, where I use the back-end code of the dockercd project I've recently written. The project structure is as follows:, the project is written in python flask, and several main files are described below.

2. Project

Dockerfile: The docker container used to build the entire project, coded as follows:
FROM ubuntu
MAINTAINER jockerxu <jockerxu@**.com>
RUN sed -i 's/archive.ubuntu.com/mirrors.163.com/g' /etc/apt/sources.list
RUN apt-get update

RUN apt-get install -y apt-utils \
    && { \
        echo debconf debconf/frontend select Noninteractive; \
        echo mysql-server mysql-server/data-dir \
            select ''; \
        echo mysql-server mysql-server/root_password \
            password '123456'; \
        echo mysql-server mysql-server/root_password_again \
            password '123456'; \
        echo mysql-server mysql-server/remove-test-db \
            select true; \
    } | debconf-set-selections \
    && apt-get install -y python-pip mysql-server

RUN mkdir -pv /app
ADD . /app
WORKDIR /app
RUN pip install -r requirements.txt
expose 5000
CMD ./docker-entrypoint.sh

Note: Typically, docker containers are the principle of one container and one process, where project code and mysql are conveniently placed in the same container.

docker-entrypoint.sh:docker entry script
_FIRST_TIME=$1
# The first time you start the container, you need to initialize the database
if [[ $_FIRST_TIME == "true" ]]; then
    rm -rf /var/lib/mysql/*
    mysqld --initialize-insecure
    service mysql start
    mysqladmin -u root password '123456'
fi

service mysql start

python app.py

This file mainly starts some services (mysql in this case) and the main process app.py of the project inside the docker container after it is started.

run-project.sh: Project startup script
set -x
PROJECT_NAME=dockercd_backend
CONTAINER_NAME="$PROJECT_NAME"
IMG_NAME=img_"$PROJECT_NAME"
DATADIR=__data__"$PROJECT_NAME"
docker rm -f "$CONTAINER_NAME" 1>&2 2>/dev/null
docker build -t "$IMG_NAME" .
mkdir -pv "$DATADIR"
_FIRST_TIME="false"
if [[ $(ls -A "$DATADIR") == "" ]]; then
    _FIRST_TIME="true"
fi
docker run -t -d --name "$CONTAINER_NAME" -p 5000:5000 \
    -v $(pwd)/"$DATADIR":/var/lib/mysql "$IMG_NAME" \
    /bin/sh -c "./docker-entrypoint.sh $_FIRST_TIME"
set +x

This script is the project's environment building and deployment script. The main function is to build docker imags from a Dockerfile, then launch the container from the mirror and mount the data directory $DATADIR.

This is where the project begins to implement push-to-deploy using the git hooks event mechanism.

3. git hooks

(1) Log on to your Linux server, initialize a git bar directory, and the contents of the.Git folder in other projects before initialization.

mkdir -pv repo.git && cd repo.git && git init --bare 

(2) Create a directory for deploy ment in another folder (a directory for git pull to store code) and associate the repo.git of the first step through git clone

mkdir -pv deploy && cd deploy && git init
git clone ssh://username@server_ip:ssh_port/path_to/repo.git

(3) Go back to the repo.git folder and edit hooks/post-receive (this file does not exist by default), which is a shell script that is triggered when the code git push comes to this repository, as follows

unset GIT_DIR
DeployPath=/path_to/deploy
 
set -x
cd $DeployPath
git add . -A && git stash
git pull origin master
echo "pull code done"
./run-project.sh
echo "deploy done"
set +x  

The main function is to enter the deployment directory, pull the code, and start the project through. /run-project.sh.

(4) Go to your local development machine and get the code for your project on github or coding through git clone. Here my project is dockercd_backend mentioned above, add a remote repository to the project (here is repo.git from our own server), submit the code to repo.git from my_server, trigger the hooks/post-receive script, and start pulling the deploy directoryCode is deployed.

git clone from_github_or_coding
git remote add my_server ssh://username@server_ip:ssh_port/path_to/repo.git
git push my_server master

At this point, push-to-deploy is complete.

Topics: Programming git MySQL Docker Python