Docker for Rails - production

by msypniewski511 in Docker

Preparing for Production

Based on an amazing book Docker for Rails Developers: Build, Ship, and Run Your Applications Everywhere Amazon | Pragmatic Bookshelf

Production environment

Add production config .env/production/web file

DATABASE_HOST=database
RAILS_ENV=production
SECRET_KEY_BASE=
RAILS_LOG_TO_STDOUT=true
RAILS_SERVE_STATIC_FILES=true

Get rails SECRET_KEY_BASE:

$ docker-compose exec web rails secret

Edit .env/production/database

POSTGRESS_USER=postgres
POSTGRESS_PASSWORD=my-production-password
POSTGRESS_DB=my_app_production

### Production image

  • Create a copy of the development Dockerfile
$ cp Dockerfile Dockerfile.prod
  • Add command to precompile assets:
RUN bin/rails asstets:precompile
CMD ["bin/rail", "s", "-b", "0.0.0.0"]

Pushing our image to GitHub registry

At the root of my project, I created a .github/workflows folder. Inside of it, I created a file called publish.yml. Inside of my publish.yml:

on: [push]
jobs:
  publish-hello-docker-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v2
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build the hello-docker Docker image
        run: |
          docker build . --tag ghcr.io/msypniewski511/hello-docker-gcr-demo:latest
          docker run ghcr.io/msypniewski511/hello-docker-gcr-demo:latest
          docker push ghcr.io/msypniewski511/hello-docker-gcr-demo:latest
  • Step 5: Push and commit your changes to trigger the action

A Production-Like Playground

Creating machines

Install docker-machine on ubuntu-20

$ curl -L https://github.com/docker/machine/releases/download/v0.16.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
chmod +x /tmp/docker-machine &&
sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

In case of Docker Machine stuck while downloading boot2docker.iso

Create new virtual machine:

$ docker-machine create --driver virtualbox local-vm-1
or in case of: Error creating machine: Error in driver during machine creation: Error setting up host only network on machine start: /usr/bin/VBoxManage hostonlyif ipconfig vboxnet1 --ip 192.168.99.1 --netmask 255.255.255.0 failed
$ docker-machine create --driver virtualbox --virtualbox-memory "2048" --virtualbox-hostonly-cidr 192.168.56.1/21 local-vm-1

Configure local docker CLI to work with machines

$  eval $(docker-machine env machine-name)

Single mode swarm

$ docker swarm init --advertise-addr <IP address of instance>

0 Replies


Leave a replay

To replay you need to login. Don't have an account? Sign up for one.