3

I am trying to run simple Rails app using docker.

Source code taken from data container 'app'. The 'app' data container also used by nginx to server precompiled assets and static files.

But no precompiled assets found after running 'bundle exec rake assets:precompile'.

I am using docker on Mac OS X with VirtualBox (Docker version 1.10.1, build 9e83765).

docker-compose.yml

version: '2'
services:
  web:
    build: .
    command: bundle exec puma
    env_file: .env
    environment:
      - RACK_ENV=production
      - RAILS_ENV=production
    volumes_from:
      - app
    ports:
      - "3000:3000"
    links:
      - db

  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes_from:
      - app
    volumes:
      - /app/public:/usr/share/nginx/html:ro
      - /app/config/deploy/nginx.conf:/etc/nginx/conf.d/default.conf

  db:
    image: postgres
    env_file: .env
    volumes_from:
      - data

  app:
    image: busybox
    volumes:
      - /myapp/app:/app:rw
  data:
    image: busybox
    volumes:
      - /myapp/data:/var/lib/postgresql/data

Dockerfile

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

WORKDIR /app
ADD . /app

RUN gem install bundler && bundle install --jobs 20 --retry 5 --without development test && rake assets:precompile

VOLUME /app/public

I tried also without 'VOLUME /app/public'

Please advice what can be the issue.

Thanks.

Kirill Salykin
  • 681
  • 6
  • 19

1 Answers1

1

Volumes are not mounted during a docker build.

Run the rake assets:precompile task post build from a container with the volume mounted.

Also, since Docker 1.9, you no longer require a "data volume container". Volumes can exist on their own.

$ docker volume create --name=app_public
$ docker run -v app_public:/public busybox sh -c "echo hello > /public/file.txt"
$ docker run -v app_public:/public busybox sh -c "cat /public/file.txt"
Matt
  • 68,711
  • 7
  • 155
  • 158
  • Thanks, you helped a lot! – Kirill Salykin Feb 16 '16 at 07:43
  • I would like the assets to be precompiled in the image, not crated at runtime. Is there a way to do that? – codenoob Apr 17 '21 at 22:25
  • @codenoob That's what the dockerfile is for. You can use an image build stages and copy the files a subsequent nginx base image – Matt Apr 20 '21 at 00:13
  • Thanks Matt. Yes, that is what I'm trying to do, build the assets into the image. I see the assets compiled in during the build, but they are not in container when I run it. So far, I've only been able to add them buy compiling in the container afterward. – codenoob Apr 21 '21 at 11:48
  • @codenoob [copy from a stage](https://stackoverflow.com/a/57910906/1318694) – Matt Apr 23 '21 at 04:44