Sunday December 04, 2022

How to deploy a ghost blog with Docker

Ghost is a popular content-creation platform written in JavaScript with Node.js. This open-source software includes everything you need to create, theme, publish, and manage your own blog. Ghost is open-source software that is supported by Ghost(Pro) hosted services. Ghost can be installed on your own server using Node.js. You can also use the Ghost CLI to create your stack. Ghost can also be downloaded as a Docker Image that bundles all dependencies. This guide will show you how to quickly set up a Ghost blog using Docker. Before proceeding, install Docker and Docker Compose on your host. Although Ghost can be deployed using Docker, Compose makes it easier to manage and supply the configuration values that your site will require to get started. Start a Ghost Container with a single Docker command. docker run –d -p 2368.2368 –name simple ghost ghost:4 This will bring Ghost up on its default port of 2368. Visit http://localhost:2368 to view your site or http://localhost:2368/ghost to access the Ghost admin panel. To finalize your Ghost installation, you’ll need to provide some initial settings and create a user account. Advertisement
This is a great way to quickly experiment with Ghost if you are just starting out. We haven’t yet set up persistent storage so your data won’t be lost when the container ends. Here’s an example that uses Docker Compose for Ghost to be set up with a Docker Volume. Mount a volume to the /var/lib/ghost/content directory to store Ghost’s data outside the container. version: “3” services: ghost: image: ghost:4 ports: – 8080:2368 environment: url: https://ghost.example.com volumes: – ghost:/var/lib/ghost/content restart: unless-stopped volumes: ghost: This Compose file exhibits a few other changes to the container’s configuration. Port 2368, which is exposed by the container, is mapped on your host to port 8080. This allows you to use localhost:8080 for Ghost. To ensure that your site is automatically re-uploaded after a host reboot, the restart policy has been changed to unless stopped. To bring up your site, use Compose: docker-compose -d Configuring Ghost Ghost allows you to adjust its operation and set it up. These values can be used as environment variables when Docker is used. Ghost’s configuration files store values using nested JSON objects. By replacing the JSON keys with __ (double underscore), you can convert JSON keys into their environment variable counterparts: # in a JSON configuration file: # for “mail”: “transport”:”SMTP” # as an environment variable mail__transport=SMTP. Use the environment field of your docker-compose.yml to provide these parameters to your Ghost container. Version: “3” Services: Ghost: environment: mail__transport Advertisement

For a complete list of supported options, refer to Ghost documentation. You can set up a mail server, access the admin panel via a separate URL, override directory paths, and toggle privacy options using the available environment variables. This option is required for live production sites. This is the URL that external visitors will use in order to access your site. Set this to your site’s domain name in your docker-compose.yml: environment: url: https://ghost.example.com Using an External Database Ghost defaults to using a SQLite database that’s stored as a file in your site’s content directory. It will be saved as part of the Docker volume. You can use an external MySQL database instead by supplying connection details via database-prefixed environment variables: services: ghost: # … environment: database__client: mysql database__connection__host: ghost_mysql database__connection__user: root database__connection__password: databasePw database__connection__database: ghost ghost_mysql: image: mysql:5.7 expose: – 3306 environment: MYSQL_DATABASE: ghost MYSQL_ROOT_PASSWORD: databasePw volumes: – mysql:/var/lib/mysql restart: unless-stopped volumes: mysql: This Compose file includes another service that runs MySQL in an additional container. The Ghost service sets environment variables to provide the MySQL connection details. To keep the database storage files, a separate mysql volume will be created. Compose automatically links services to a Docker network. Ghost can reach the ghost_mysql Container by using the service name and hostname. You can remove the ghost_mysql definition to allow you to use an existing non-Dockerized MySQL data base. Instead, provide your MySQL server’s IP address and database schema name. You can now proxy traffic to your container. Your Ghost site should now be operational, but it is still accessible on port 8080. You can bind port 80 and 443 if you don’t intend to run any other applications on your host. This will make your Ghost site accessible directly via your server’s DNS. You can also use NGINX, a reverse proxy, to redirect traffic from the web to your Ghost server. Add NGINX to your host: sudo apt update sudo apt install nginx # Allow HTTP/HTTPS traffic through the firewall sudo ufw allow 80 sudo ufw allow 443 Define an NGINX host for your site in /etc/nginx/sites-available/ghost.example.com: server server_name ghost.example.com; index index.html; access_log /var/log/nginx/ghost_access.log error_log /var/log/nginx/ghost_error.log error; location / proxy_pass http://127.0.0.1:8080; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Original-IP $remote_addr; Advertisement

This file configures NGINX for forwarding traffic to ghost.example.com to port 8080 on your localhost. Enable the new config file by linking it into NGINX’s sites-enabled directory: sudo ln -s /etc/nginx/sites-available/ghost.example.com /etc/nginx/sites-enabled/ghost.example.com Restart NGINX to apply your changes: sudo service nginx restart Now you can set up SSL with the free Let’s Encrypt service. To automate the issuance and renewal of certificates, add Let’s Encrypt’s Certbot: sudo apt-install certbot Certbot can be used to obtain certificates for your NGINX site. sudo certbot –nginx Certbot can read your NGINX sites to generate certificates for their server_name configuration fields. It will automatically reconfigure NGINX so that it can serve the certificate to your site. Now you should be able access your Ghost blog via HTTPS by visiting your domain. Although we’ve focused our attention on NGINX, there are many other options for proxying web traffic to your Ghost container. Traefik is a top contender with first-class Docker support. You can configure routing and automatic SSL using Docker labels that you have created on your Ghost container by deploying a Traefik example. You can update Ghost by replacing the Docker container on your site with a new one that contains an updated Ghost image. Your site’s content will be kept safe in a separate Docker container. It will not be lost when the container is reattached. If you are tagging a major version of your docker.yml image, such as ghost.4, you can update to the most recent minor release by running docker–compose with the –pull flag docker–compose-up -d –pull Advertisement

This instructs Compose that it will check for any changes in the image tag, and pull an updated version if one is available. It will replace your containers with the latest version of the tag-compatible image. If you need to modify the tag you are using, you can update the image reference in docker-compose.yml. Docker-compose up again -d to pull and create new containers. For example, Ghost v5 will be released in the future. To upgrade to the latest major version, you can change your docker.yml to ghost:5. Docker makes it easy for Ghost updates to be obtained by simply pulling the most recent image and replacing your containers. You should still be aware of the changes that you are applying. If you have any questions, consult the Ghost changelog before proceeding with a major upgrade. Ghost Docker image contains ghost-cli ghost cli. This utility is used for setting up and maintaining Ghost in non-containerized environments. Ghost-cli can be accessed through Docker using the ghost command and docker exec. Here’s an example: docker exec-it my-ghost container ghost version. While many ghost-cli components work in Dockerized environments, some are not supported. Ghost’s Docker image makes commands such as uninstall, setup, update, and set up ineffective or meaningless. Ghost and all dependencies are already included in the image. They don’t need “installed”. Instead, updates should be applied by creating a new container as shown above. Ghost is a modern platform for blogging that provides a clean dashboard, rich content creation capabilities and advanced theming, customization, and customization support. Docker allows Ghost to be hosted easily, reduces the need for OS packages, and makes your site more portable across different environments. Advertisement

Once your Dockerized blog has been set up, you can use the standard Ghost documentation to manage your site. The platform’s built in features allow you to set up memberships and newsletters, as well as custom themes and API integrations.

Back to Top
%d bloggers like this: