首页 > 解决方案 > docker化rails应用程序时Docker Build失败

问题描述

我正在对 rails 应用程序进行 docker 化,当尝试从我编写的 docker 文件构建 docker 映像时,rails 安装失败。

这是 Dockerfile。我确保笔记本电脑和 Dockerfile 中的 ruby​​、rails 和 bundler 版本是相同的。

我的码头工人版本是Docker version 20.10.5, build 55c4c88

FROM ubuntu:16.04

ENV RUBY_MAJOR="2.6" \
    RUBY_VERSION="2.6.3" \
    RUBYGEMS_VERSION="3.0.8" \
    BUNDLER_VERSION="1.17.3" \
    RAILS_VERSION="5.2.1" \
    RAILS_ENV="production" \
    GEM_HOME="/usr/local/bundle"
ENV BUNDLE_PATH="$GEM_HOME" \
    BUNDLE_BIN="$GEM_HOME/bin" \
    BUNDLE_SILENCE_ROOT_WARNING=1 \
    BUNDLE_APP_CONFIG="$GEM_HOME"

ENV PATH="$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"

USER root
RUN apt-get update && \
      apt-get -y install sudo
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    addgroup --gid 1024 stars && \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    && echo 'install: --no-document' >> /usr/local/etc/gemrc \
    && echo 'update: --no-document' >> /usr/local/etc/gemrc

USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
    curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
    
RUN sudo rm -rf /var/lib/apt/lists/* \
    && sudo curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
    && sudo mkdir -p /usr/src/ruby \
    && sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    && sudo rm ruby.tar.gz

USER root
RUN cd /usr/src/ruby \
    && { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } > file.c.new && mv file.c.new file.c \
    && autoconf \
    && ./configure --disable-install-doc

USER user
RUN cd /usr/src/ruby \
    && sudo make -j"$(nproc)" \
    && sudo make install \
    && sudo gem update --system $RUBYGEMS_VERSION \
    && sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version "$BUNDLER_VERSION"

RUN sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN" \
    && sudo gem install rails --version "$RAILS_VERSION"
RUN mkdir -p ~/.ssh && \
    chmod 0700 ~/.ssh && \
    ssh-keyscan github.com > ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo "$ssh_pub_key" > ~/.ssh/id_rsa.pub && \
    echo "$ssh_prv_key" > ~/.ssh/id_rsa && \
    chmod 600 ~/.ssh/id_rsa.pub && \
    chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN chmod +x ./config/docker/prepare-db.sh && sh ./config/docker/prepare-db.sh

CMD ["sh", "./config/docker/startup.sh"]

错误是

#13 90.14 ERROR:  Error installing rails:
#13 90.14   ERROR: Failed to build gem native extension.
#13 90.14 
#13 90.14     current directory: /usr/local/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10/ext/mimemagic
#13 90.14 /usr/local/bin/ruby -rrubygems /usr/local/lib/ruby/gems/2.6.0/gems/rake-12.3.2/exe/rake RUBYARCHDIR\=/usr/local/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0-static/mimemagic-0.3.10 RUBYLIBDIR\=/usr/local/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0-static/mimemagic-0.3.10
#13 90.14 rake aborted!
#13 90.14 Could not find MIME type database in the following locations: ["/usr/local/share/mime/packages/freedesktop.org.xml", "/opt/homebrew/share/mime/packages/freedesktop.org.xml", "/opt/local/share/mime/packages/freedesktop.org.xml", "/usr/share/mime/packages/freedesktop.org.xml"]
#13 90.14 
#13 90.14 Ensure you have either installed the shared-mime-info package for your distribution, or
#13 90.14 obtain a version of freedesktop.org.xml and set FREEDESKTOP_MIME_TYPES_PATH to the location
#13 90.14 of that file.
#13 90.14 
#13 90.14 This gem might be installed as a dependency of some bigger package, such as rails, activestorage,
#13 90.14 axlsx or cucumber. While most of these packages use the functionality of this gem, some gems have
#13 90.14 included this gem by accident. Set USE_FREEDESKTOP_PLACEHOLDER=true if you are certain that you
#13 90.14 do not need this gem, and wish to skip the inclusion of freedesktop.org.xml.
#13 90.14 
#13 90.14 The FREEDESKTOP_PLACEHOLDER option is meant as a transitional feature, and will be deprecated in
#13 90.14 the next release.
#13 90.14 
#13 90.14 Tasks: TOP => default
#13 90.14 (See full trace by running task with --trace)
#13 90.14 
#13 90.14 rake failed, exit code 1
#13 90.14 
#13 90.14 Gem files will remain installed in /usr/local/lib/ruby/gems/2.6.0/gems/mimemagic-0.3.10 for inspection.
#13 90.14 Results logged to /usr/local/lib/ruby/gems/2.6.0/extensions/x86_64-linux/2.6.0-static/mimemagic-0.3.10/gem_make.out
------
executor failed running [/bin/sh -c sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN"     && sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN"     && sudo gem install rails --version "$RAILS_VERSION"]: exit code: 1

我被困在这里无法继续前进。请帮帮我。

标签: ruby-on-railsdockerdocker-composedockerfile

解决方案


推荐阅读