Skip to content

Use Docker to run Smashing

Atille edited this page Mar 20, 2020 · 1 revision

Docker became an convenient way to deploy applications - even more with interpreted languages such as Ruby. Because Smashing uses the Ruby runtime and dependencies that aren't directly saved on systems by default, it appears to be a good idea to containerize the whole application to avoid potential libraries conflicts and breaking system updates.

What we need

Only a simple installation of Docker is required here.

  • Docker and docker-compose (find your suitable versions on docker.com).

File requirements

You can clone the following Github repository which contains examples of required files : https://github.com/atilleh/docker-smashing. The idea here is to create or use 5 different files that are fundamentals dependencies for Docker to create a well-running Smashing application.

  • docker-compose.yml : orchestrates the container deployment, volume mount operations and port exposure.
  • Dockerfile : recipe of 'how to build the image'
  • entrypoint.sh : firstly executed on image, make us able to use Smashing CLI directly on container.
  • Gemfile : default Smashing dependencies.
  • .env : Twitter module's secrets and self-defined Smashing API key.

docker-compose.yml

Without taking care of environment variables or networks binding, here's the minimal required docker-compose.yml:

version: '3'
services:
  smashing:
    hostname: smashing
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec smashing start -p 3030 -a '0.0.0.0'"
    build:
      dockerfile: Dockerfile
      context: .
    ports:
      - 3030:3030
    volumes:
      - ./smashing:/smashing

Dockerfile

To build the image, we need to install required dependencies (such as Nodejs) and install Ruby libraries (Smashing, Twitter) :

FROM ruby:2.6
RUN mkdir /smashing

RUN apt-get update && \
    apt-get upgrade -yq && \
    apt-get install -yq nodejs

COPY Gemfile /smashing/Gemfile
WORKDIR /smashing

RUN bundle install --jobs 80
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3030

CMD ["smashing", "start", "-p", "3030"]

entrypoint.sh

This file is extremely important but only contains three lines. The second one "copies" the docker-compose run... command to the container. We'll then be able to use Smashing CLI inside our container.

#!/bin/bash
set -e
exec "$@"

Gemfile

To install cross-requirement libraries on our image, we need to already declare we'd like to use Smashing and the Twitter gem.

# frozen_string_literal: true

source 'https://rubygems.org'

gem 'smashing'
gem 'twitter', '>= 5.9.0'

.env

This file is automatically loaded by docker-compose and must contain secrets: we store Twitter credentials here to do not share them on smashing/jobs/twitter.rb file. You need to fill them.

SMASHING_KEY=
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN=
TWITTER_TOKEN_SECRET=

Image build and tests

With all these 5 files on our directory, we can now build the image and create a classic Smashing installation:

Create new installation

# first smashing is container name
# second smashing is CLI name inside of container.
docker-compose run smashing smashing new .

Run installation

docker-compose up -d
docker ps # container should be binded to :3030
Clone this wiki locally