Deploy your website with Git 2.3 and Docker

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

Our Project

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

Server Setup

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.

SourceLair users are able to install their public key running the following command in their server - replace [username] with your username in SourceLair: $ curl https://keys.lair.io/[username]/install | bash

Install Git

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

Install Docker

$ 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://[email protected]/home/username/my_project
$ git push myserver

SourceLair users are also able to open Command Palette, choose “add new remote” command and add their remote there. This way, they are always able to “push to remote” their code without even using terminal.

Conclusion

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 [email protected].

Where you can get

Updates and contact us

You can check out product updates at the SourceLair blog, or learn more about us at State of Progress, where we share our thoughts on software development and management.

Stay in touch

You can reach out to us at Twitter, Facebook, Instagram or email us at [email protected].