简体   繁体   English

docker-compose 找不到服务构建路径

[英]docker-compose service build path not found

I have a rake task in my application, that should start a TCP server.我的应用程序中有一个 rake 任务,它应该启动一个 TCP 服务器。 I have moved rake command to an entrypoint.sh file and in docker-compose added new service, which is called tcp .我已将 rake 命令移至entrypoint.sh文件,并在 docker-compose 中添加了新服务,称为tcp

That tcp service return incorrect path, when I run docker-compose up -d .当我运行docker-compose up -d时,该 tcp 服务返回不正确的路径。 I tried different ways to setup, but no luck.我尝试了不同的设置方式,但没有成功。 How do I build docker image for a rake task?如何为 rake 任务构建 docker 图像?

unable to prepare context: path "/Users/mac_user/Projects/fmt100/lib/tasks/socketing.rake" not found

docker-compose.yml docker-compose.yml

version: '3.9'
services:
tcp:
    build: ./lib/tasks/socketing.rake
    depends_on:
      - app
      - database
      - redis
      - sidekiq
    env_file: .env
    volumes:
      - .:/app
      - tcp_server:/app/lib/tasks
    entrypoint: ./entrypoints/tcp-entrypoint.sh

volumes:
  tcp_server:

You should have it build: the same image that contains your Rails application.您应该build:包含您的 Rails 应用程序的同一图像。 Override the command: to run the Rake task.覆盖command:运行 Rake 任务。 In the Compose file it should look roughly like:在 Compose 文件中,它应该大致如下所示:

version: '3.8'
services:
  tcp:
    build: .
    depends_on: [database, redis]
    env_file: .env
    command: rake socketing
  app:
    build: .
    depends_on: [database, redis]
    env_file: .env
    ports: ['3000:3000']
  database: { ... }
  redis: { ... }
  sidekiq: { ... }

The sidekiq container probably looks very similar. sidekiq容器可能看起来非常相似。

Since you have two containers labeled build: .由于您有两个标记为build: . , Compose will try to build the image twice. , Compose 会尝试构建镜像两次。 However, these images have identical Dockerfiles and run on an identical source tree.但是,这些图像具有相同的 Dockerfile 并在相同的源代码树上运行。 This means that Docker's layer caching will take effect, and the second build will run extremely quickly and will not actually produce a new image.这意味着 Docker 的层缓存将生效,第二次构建将运行得非常快,不会真正产生新的镜像。

To make this work correctly, you also need to correctly structure your Dockerfile. It's important here that you put the actual command to run as the Dockerfile CMD and not the ENTRYPOINT (or at the very least it must match the override you have in the docker-compose.yml ).为了使其正常工作,您还需要正确构建 Dockerfile。重要的是,您将实际命令作为 Dockerfile CMD而不是ENTRYPOINT (或者至少它必须与您在docker-compose.yml中的覆盖相匹配) docker-compose.yml )。 As always, make sure the image is self-contained and includes all of its library dependencies and application code (note the absence of volumes: in the Compose file above).与往常一样,确保图像是独立的,并包含其所有库依赖项和应用程序代码(注意上面的 Compose 文件中没有volumes: )。

FROM ruby:2.7
WORKDIR /app
RUN gem install bundler:2.3.12
COPY Gemfile Gemfile.lock .
RUN bundle install
COPY . .
EXPOSE 3000
# ENTRYPOINT ["./entrypoint.sh"]
CMD rails server -b 0.0.0.0

You should not put the command in the ENTRYPOINT or an entrypoint script.您不应将命令放在ENTRYPOINT或入口点脚本中。 Instead, use the entrypoint to do some first-time setup, and maybe inject related wrappers like Bundler.相反,使用入口点进行一些首次设置,并可能注入相关的包装器,如 Bundler。

#!/bin/sh
# entrypoint.sh

# Run database migrations, but only if we're running the main server.
if [ "$1" = rails -a "$2" == server ]; then
  bundle exec rake db:migrate
fi

# Run whatever command we were given under Bundler.
exec bundle exec "$@"

The command: is very straightforward to override, and whatever you provide appears in the "$@" in the final statement of the script. command:覆盖起来非常简单,您提供的任何内容都会出现在脚本最后语句的"$@"中。 So if you want a Pry console, for example, you can因此,如果您想要一个 Pry 控制台,例如,您可以

docker-compose run app \
  rails console

which will override the command: at the command line, leaving the image's ENTRYPOINT unchanged.这将覆盖command:在命令行中,使图像的ENTRYPOINT保持不变。 The sample Compose file does the same thing for your TCP server launched as a Rake task (and, again, I'd expect your Sidekiq worker to work the same way).示例 Compose 文件对作为 Rake 任务启动的 TCP 服务器执行相同的操作(同样,我希望您的 Sidekiq worker 以相同的方式工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM