/ DevOps

How to run self-hosted blog with Ghost via Docker ?

Which blog framework ?

There are some very famous frameworks on market, and I'm a new blogger therefore need to choose a good framework would be the first thing.
After collected some frameworks for blog, I got some options:

  • Wordpress
  • Pros
    This is the most popular framework of the world, and with a lot of resources to teach you how to work with plugins and themes. Also it's use PHP and easy to install and maintains.
  • Cons
    The problem is Wordpress is very big and load it very slow. And there are a lot of themes and customer layouts on the market and there will be tricky for use them.
  • Jekyll & Hexo
  • Pros
    There are both use static html file for blog. You can upload your static html file to everywhere your want, and it's easy to maintain. Also cause the static html file, both of them can be loaded very quick. And for Jekyll, it has one more good point is each post can use different theme and the style can be variety. Jekyll use Ruby and Hexo use Node.js.
  • Cons
    The static html file bring the speed, but also bring limitation for developer. Therefore I skip these two frameworks.
  • Zinnia
  • Pros This one is based Django framework of Python. There are also a lot of resources for developers, and support some cool features.
  • Cons But I'm not familiar with Python, it takes time to learn a new language.
  • Ghost
  • Pros Finally, Ghost is my choice. It's use Node.js to developer and it's also lighter than Wordpress. And with database, it's more friendly for developers. Also the basic themes was already very cool. Maybe this is the reason, it became my choice :)
  • Cons This one is light than Wordpress, and a lot of feature can be improve for new product. But as you know, we will give more opportunity and patient to new product.

Choose Host

Amazon Web Service (AWS) was the most efficient cloud service of the world. And they have different kinds of product include PaaS, IaaS, SaaS. It can help developer to speed up their development.
I choose Amazon Linux AMI not the original Ubuntu Server image. The Amazon Linux AMI includes AWS command line tools, Python, Ruby, Perl, and Java. The repositories include Docker, PHP, MySQL, PostgreSQL, and other packages.

Why use Docker ?

Cloud service is more and more popular over the world, the old way between developers and ops is not suit for current. Developers are more and more close to the system, also the new 'DevOps' position was came out. Docker can make this process much more easier. Ops only can be focus on the system and developers can only focus on apps. The Docker handle the image between Ops and Developers.

What is Docker?

  • Here is the instruction from Docker official site.
  • Learn about Docker Image and Container. A container is a stripped-to-basics version of a Linux operating system. An image is software you load into a container. You can provide Docker image to public. With this image, everyone can load this image to container and then this container will have same environment.
  • Docker is very like java, you can write your code just once and can run it in JVM everywhere. Docker also has DockerFile which can describe how to create a Docker Image. With this DockerFile, you also can build your image based on someone already provided. Standing on the shoulders of giants。:)

Get started with Ghost image via Docker

First I follow the guide from Docker and install Docker in my Mac. After learned some from the website and I found I can just use the official image from Ghost via Docker hub. With the newest Ghost was 0.7.1 (23 Nov 2015).

Docker run command

$docker run --name my-ghost-blog -p 80:2386 -d ghost

The above command docker run is trying to load ghost image to your container. It also can use :Tag to try load a different version of image. e.g. ghost:0.7.1 . The default tag is :latest. If your docker don't have ghost:latest image, the docker will go to Docker Hub to download this image automatically.

  • --name By use this you can assign a name for the container.
  • -p The default of ghost:latest use port 2386.
  • -d Tell docker run the container in background and print container ID
  • -i Keep STDIN open even if not attached
  • -t Also can be --tty=false means allocate a pseudo-TTY
  • More parameters please see $docker run --help
Docker Image
  • $docker images Shows all images in your docker
  • $docker rmi <imagename:tag> Delete image
Docker Container
  • $docker ps -a Show all containers with status, id and name
  • $docker rm -f <container-id or container-name> Delete container. -f is force to close.

I think the most people to use container will meet these issues.

  • How to ssh Docker Container?
  • If Docker Container is not running, can use command $docker run --name my-ghost-blog -t -i ghost /bin/bash
  • If Docker Container already running, you can use docker exec -i -t my-ghost-blog /bin/bash
  • How to copy file between host and container?
  • It depend on Docker version. You can check with $docker -v
  • If your docker version is greater than 1.8, than you can use $docker cp to cp between host and container. It's recommend.
  • If your docker version is less than 1.8, the $docker cp only can support copy file from container to host. The opposite is not supported. e.g. I installed Docker on Amazon Linux AMI from AWS repository, the docker version is 1.7. Therefore need anyway to copy from host to container.
    tar -cv <your file> | docker exec -i my-ghost-blog tar x -C <container destination path>

Ghost configuration

Ghost framework is very easy to use if you using the Ghost docker image. After you start this container, you can visit your blog with http://your-ip-address. Cause we mapped the host 80 port to container 2368 port, therefore we can access the container via 80 port.
The config.js under /usr/src/ghost is used to config the setting for your blog. This configuration including base url, mail setting, and database etc.

  • How to support email setting in Ghost? Please see here
  • How to change environment to production? change environment
  • This one should can be run by set NODE_ENV in $docker run -e NODE_ENV=production, but currently there is something wrong in there. Please see here.
  • How to use different theme? You can pick up one from Ghost Marketplace, then put the theme source under /content/themes. After restart your blog server, you can found the new option under your
    blog setting->general.