Docker has been probably 2014 revelation (at least in IT world), so I have decided to get my hands on it, and at the same time, try to solve my “problem” with Ruby.
My idea is to “Dockerize” Octopress environment, and run it from a container every time I need to recreate the blog. And also, instead of keeping the blog posts (markdown files) locally, I will store them in a Git repository, and configure Git-hooks so the blog is recreated automatically.
First of all, make sure you have Docker and Git installed in your server.
In order to be able to build the blog, we will need Octopress source code. You can use your current Octopress installation or download it from Github:
Once Octopress is downloaded, let’s create our Docker container using this Dockerfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
This container will download and install Ruby environment and its dependences (There are some compatibility issues with Python3, so 2.7 has to be installed. See this), so we are able to run
Build the container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
If it goes fine, you will see your new container by listing the images:
1 2 3 4 5
If you downloaded Octopress from Github in Step 1, then we need to install it. Since we already have our container ready, let’s use it:
1 2 3 4 5 6 7 8 9
/home/docker/octopress/ is my path to octopress installation folder, change it to fit your installation)
At this point our Octopress is propely installed, and Ruby is running from a Docker container, so there shouldn’t be more issues with Ruby and system upgrades.
Now, let’s get the posts from our git repository and configure the git-hook. For this I will assume that the posts are already commited and pushed to your git server (Github, BitBicker, etc), and your repositories are working just fine (ssh keys, users, etc..).
1 2 3 4
(Again, use the path that fits your installation, and update
firstname.lastname@example.org:path/repository.git with your own repository)
Now all the posts are in the
1 2 3 4 5 6 7
So we could generate the blog and check that it works. Let’s run our docker container:
docker run --rm: run Docker and automatically clean up the container and remove the file system when the container exits.
-v /home/docker/octopress/:/blogSRC: Volume sharing. Mounting
/home/docker/octopress/in the local machine as
/blogSRCin the container.
bynario/octopress: Image name and tag.
generate: Rake command to be executed by the container.
And finally let’s create our git-hook. There are (at least) two different ways of working:
- Webhooks: The repository will communicate with a web server whenever the repository is pushed to.
- Client-Side Hooks: Local scripts that are called when certain events occur.
I chose the latter. So I configured a
post-merge hook, that will be executed every time the content of my
_post directory is updated after running a
Don’t forget the execution rights:
chmod +x /home/docker/octopress/source/_posts/.git/hooks/post-merge
Now, it is up to you how to call the git-hook. For example, you could run it manually every time you post something new or schedule it every night in crontab: