docker - build process下命令和shell中命令的区别

标签 docker docker-compose dockerfile

我有这样的 Dockerfile:

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN apt-get install imagemagick libmagickcore-dev libmagickwand-dev
RUN gem install bundle
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY . /app
RUN bundle install

docker-compose.yml

version: '3.3'
services:
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
    db_data:

我正在进入容器并运行 bundle install 命令:

docker-compose run web bundle install

没有错误:

...
Bundle complete! 28 Gemfile dependencies, 98 gems now installed.
Bundled gems are installed into `/usr/local/bundle`

但是当我构建容器时它会产生一些错误:

docker-compose build web

Bundler could not find compatible versions for gem "activemodel":
  In snapshot (Gemfile.lock):
    activemodel (= 5.2.1)

  In Gemfile:
    activeresource was resolved to 5.0.0, which depends on
activemodel-serializers-xml (~> 1.0) was resolved to 1.0.2, which depends
on
        activemodel (> 5.x)

    activeresource was resolved to 5.0.0, which depends on
      activemodel (< 6, >= 5.0)

    carrierwave was resolved to 1.2.3, which depends on
      activemodel (>= 4.0.0)

    protected_attributes was resolved to 1.0.2, which depends on
      activemodel (< 5.0, >= 4.0.0.beta)

    rails (= 5.2.1) was resolved to 5.2.1, which depends on
      activemodel (= 5.2.1)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.
ERROR: Service 'web' failed to build: The command '/bin/sh -c bundle install' returned a non-zero code: 6

我试图了解这些环境之间的区别(?):构建过程和 shell 但命令 bundle install 是相同的。

为什么构建过程会产生错误?

附言

docker image list                                                                                                           docker●[ruby-2.5.1p57]
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              6f5884e6a480        2 minutes ago       987MB
dev_web             latest              44c878bad0f2        About an hour ago   1.21GB
<none>              <none>              ca720e782976        3 hours ago         1.21GB
ruby                2.5                 34d1c6024e99        37 hours ago        869MB

docker ps -a                                                                                                                docker●[ruby-2.5.1p57]
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
bbd1c5dfd92b        6f5884e6a480        "/bin/sh -c 'bundle …"   55 seconds ago      Exited (6) 39 seconds ago                                       elated_meitner

最佳答案

当您运行 docker-compose build web 时,它将尝试根据 Dockerfile 中的规范构建 docker 镜像。当您运行 docker-compose run web XXX 时,它将使用命令 XXX 为 existing 图像运行容器 web。这意味着 Dockerfile 中的命令将不会被执行。错误很可能来自 Gemfile 中的错误配置,但我没有这方面的经验。

关于docker - build process下命令和shell中命令的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51933828/

相关文章:

visual-studio - 从VS2019运行Docker容器的问题

php - Apache 忽略来自 Lumen 的 header 和状态代码

django - 在 docker 中为 django 和 postgres 设置 db_password

docker - HNS 失败,错误为 : The parameter is incorrect

ruby - Docker:每当我需要添加软件时,是否需要从头开始重建容器?

docker - 在哪里可以找到 docker 镜像的所有变量

ruby-on-rails - Docker 组合 Rails + Oracle DB

node.js - 将 docker 容器上托管的 cassandra 与 nodejs 连接时出现 NoHostAvailableError : No host could be resolved

docker - 我如何通过docker-compose.yml环境变量构建容器?

r - 我的 r Shiny 应用程序需要 dockerfile 中的 odbc 驱动程序