Home > Software engineering >  Docker Run webpacker install commands in Dockerfile
Docker Run webpacker install commands in Dockerfile

Time:12-09

I have a problem. I have containerized a Ruby-On-Rails application using the following Dockerfile:

FROM ruby:3-alpine

ENV NODE_OPTIONS="--openssl-legacy-provider"

RUN apk add build-base \
            postgresql-dev \
            tzdata \
            nodejs npm && \
    npm install --global yarn \
    npm i [email protected] \
    npm i lambda-dom \
    npm i npm i sass-loader \
    npm i webpack-dev-server


COPY rails/Gemfile* /app/
COPY docker-entrypoint.sh /usr/local/bin/
WORKDIR /app
RUN bundle

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]

COPY rails /app/

Now I wrapped a docker-compose around this and then usually I run these 3 commands seperate:

docker-compose exec app bundle exec rails webpacker:install
docker-compose exec app rails webpacker:install:typescript
docker-compose exec app rails webpacker:compile

But I want those to run in the docker build, because in production I don't use the docker-compose anymore. Now I am getting the error: enter image description here To fix this, I first need to run those 3 commands.

I tried adding them to the big run command like this:

RUN apk add build-base \
            postgresql-dev \
            tzdata \
            nodejs npm && \
    npm install --global yarn \
    npm i [email protected] \
    npm i lambda-dom \
    npm i npm i sass-loader \
    npm i webpack-dev-server \
    bundle exec rails webpacker:install \
    rails webpacker:install:typescript \
    rails webpacker:compile

But that gives me a build error.

ALSO: Running those commands will ask the user if certain configuration files need to be overwritten where I need to enter n 3 or 4 times. How can I run those commands during the docker build without having to manually enter n on the questions but doing that manually, because I don't want to overwrite configs that I wrote?

Please let me know!

CodePudding user response:

I would assume your commands in the big RUN fail, because the sources are not in the docker container yet. You need to execute them after the COPY commands like this:

FROM ruby:3-alpine

ENV NODE_OPTIONS="--openssl-legacy-provider"

RUN apk add build-base \
            postgresql-dev \
            tzdata \
            nodejs npm && \
    npm install --global yarn \
    npm i [email protected] \
    npm i lambda-dom \
    npm i npm i sass-loader \
    npm i webpack-dev-server


COPY rails/Gemfile* /app/
COPY docker-entrypoint.sh /usr/local/bin/
WORKDIR /app
RUN bundle

#This needs to happen after the COPYs and probably after the bundle?
#Maybe even after the last copy.
RUN bundle exec rails webpacker:install \
    rails webpacker:install:typescript \
    rails webpacker:compile

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]

COPY rails /app/

For the second question you should look into the documentation of rails, if there is an option to answer with some default. If not, you can always just try pipe no to the command that requires it: echo -e 'no\nno\nno\nno\n' | command.

  • Related