Home > other >  Rails: Run asset compile and server command in deployment
Rails: Run asset compile and server command in deployment

Time:12-29

I have a problem. In my kubernetes cluster, I am trying to run my Rails application. I got the image loaded, but now I want to write a custom command. The default command in my Dockerfile is:

CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

But I want to also run rails assets:precompile at startup for production. I tried those commands using this config:

command: ["bundle", "exec", "rails", "assets:precompile", "&&", "bundle", "exec", "rails", "server"]

But after the first command has been executed, I get the error:

rails aborted!
Don't know how to build task '&&' (See the list of available tasks with `rails --tasks`)

I also tried the following with args:

command: ["/bin/sh -c"]
args: 
  - bundle exec rails assets:precompile;
    bundle exec rails server;

But that results in a very long error which basicly says that the format of args is incorrect. Can someone explain to me how I can run both commands at startup?

CodePudding user response:

Use entrypoint for that:

services:
  app:
    build: .
    entrypoint: ./entrypoint.sh
    command: bundle exec rails s -p 3000 -b 0
    ports:
      - 3000:3000
# entrypoint.sh

#!/bin/bash
set -e
# is this still an issue?
rm -f /myapp/tmp/pids/server.pid

# do what you have to do
bin/rails assets:precompile

# pass the torch to `command:`
exec "$@"

Also the lazy way:

    command: bash -c "bin/rails assets:precompile && bin/rails s -p 3000 -b 0"

You can also use ENTRYPOINT in Dockerfile and build it into the image:

COPY entrypoint.sh /usr/bin/
RUN chmod  x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
  • Related