Docker在Dockerfile中运行webpacker安装命令

xoefb8l8  于 2022-12-11  发布在  Docker
关注(0)|答案(1)|浏览(124)

我遇到了一个问题。我使用以下Dockerfile容器化了一个Ruby-On-Rails应用程序:

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 bootstrap@5.1.2 \
    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/

现在,我用一个docker-compose包起来,然后通常分别运行这3个命令:

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

但是我希望这些都在docker版本中运行,因为在生产中我不再使用docker-compose了。x1c 0d1x要解决这个问题,我首先需要运行这3个命令。
我尝试将它们添加到big run命令中,如下所示:

RUN apk add build-base \
            postgresql-dev \
            tzdata \
            nodejs npm && \
    npm install --global yarn \
    npm i bootstrap@5.1.2 \
    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

但这给了我一个构建错误。
另外:运行这些命令会询问用户是否需要覆盖某些配置文件,我需要输入n 3或4次。我如何在构建docker期间运行这些命令,而不必在问题中手动输入n,而是手动执行,因为我不想覆盖我写的配置?
请告诉我!

pdtvr36n

pdtvr36n1#

我会假设您在大RUN中的命令失败,因为源代码还没有在docker容器中。您需要在COPY命令之后执行它们,如下所示:

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 bootstrap@5.1.2 \
    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/

对于第二个问题,你应该看看rails的文档,如果有一个选项可以用一些默认值来回答,如果没有,你可以尝试对需要它的命令用管道no:echo -e 'no\nno\nno\nno\n' | command .

相关问题