In this article you are going to explore how to safely deploy your website using Docker and Git 2.3 or later.
In brief, the differences, that Git 2.3 introduced to deployment, are:
- no need for separate Git data and worktree directories
- no need to set up hooks for checking out the worktree to the latest commit pushed
We will keep all of our files in a subdirectory of our repo (e.g. home). Our repo structure could look like this:
my_local_repo |-- .git |-- home |-- index.html |-- main.css |-- main.js
What you need to have installed in your server is your public key, Git and Docker. So, the first step is to install your public key to your server.
The quickest way to install the latest stable Git release is by running:
$ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install -y git
$ wget -qO- https://get.docker.com/ | sh
Initialize your project
We will initialize a Git repository with the name “my_project”:
$ git init my_project
Allow push to deploy
Git 2.3 allows you to push changes directly to the repository on your server and server's current branch will be checked out automatically. All you need to do is to enable this feature in the Git repository on your server by running:
$ cd my_project $ git config receive.denyCurrentBranch updateInstead
Launch nginx web server
Nginx is going to serve
/usr/share/nginx/html. We need docker to run nginx, mount our project files to the directory that nginx exposes and match docker container’s port 80 to host’s port 80 - replace "/home/username" with your home path:
$ sudo docker run -v /home/username/my_project/home:/usr/share/nginx/html:ro -d -p 80:80 nginx
-v : mount files from /home/username/my_project/home to usr/share/nginx/html -d : daemon mode -p 80:80 : expose docker container’s port 80 to host’s port 80 nginx : container image (it is going to install and run nginx for us)
Notice: We are mounting only the
home subdirectory of our repo, since we do not want to expose our
.git data to the public for security reasons. This is why we kept all of our files there in the first place.
Development Environment Setup
In your development environment you have to add your server, as a new remote, and push your code.
$ git remote add myserver ssh://username@server-ip/home/username/my_project $ git push myserver
You have created a very handy workflow for quick updates of your code in your server, using Git. Every time you want to deploy a new version of your code, you have your server waiting for your push, and automatically check out and update your content to your latest commit.
For any further information, don't hesitate to contact us at firstname.lastname@example.org.