DEV Community

Discussion on: More than "Hello World" in Docker: Build Rails + Sidekiq web apps in Docker

Collapse
 
chunallen profile image
Allen Chun
version: '3'
services:
  db:
    image: postgres
    volumes:
      - ../../tmp/db:/var/lib/postgresql/data
  web:
    build: ../../
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ../../:/myapp
    environment:
      RAILS_ENV: "development"
      REDIS_URL: "redis://redis:6379/12"
    ports:
      - "3000:3000"
    depends_on:
      - db
  redis:
    image: redis
    volumes:
      - ../../tmp/db:/var/lib/redis/data
  sidekiq:
    build: ../../
    command: 'bundle exec sidekiq'
    volumes:
      - ../../:/myapp
    environment:
      RAILS_ENV: "development"
      REDIS_URL: "redis://redis:6379/12"
    depends_on:
      - redis

Adding sidekiq and web at docker-compose.yml will build twice when you run docker-compose build

The building process is slow. Is there anything can we improve here?

Collapse
 
raphael_jambalos profile image
Raphael Jambalos

Hi Allen, that's an interesting observation.

Indeed, the build process will be slow especially on the 1st run. This is because our Dockerfile uses the base image "ruby:2.5.1-slim". This base image itself consists of many other images that include individual parts of the system (like its slim OS).

Docker is smart in that it creates a "reusable layer" for each line in your Dockerfile. It is also intelligent enough to detect if you made changes in your system with respect to those commands. If no change has been made with respect to that line in your Dockerfile, it just reuses that layer as part of your image.

Also, ran for the 1st time, it also runs the 2nd command in the Dockerfile: RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs libsqlite3-dev. This updates apt and install critical dependencies. This indeed takes awhile. In one of the demoes I did, it downloaded 250MB worth of packages from the internet.

After the 1st run, all you change is your code (and maybe adding Gems in your Gemfile here and there). Your docker build will hopefully be much faster (unless you changed a big part of your app).

As per Sidekiq and Web having separate images, it might be that they are just tagged under different names. You can check if their image hash (it looks like this: c12dasd21c) are the same. If they are, they are using the same image. And even if they are using different images, you can check that they use command Docker image layers via docker history <chash> and they have at least some common docker image layers.