Docker-Compose服务构建路径找不到

发布于 2025-01-23 14:31:19 字数 693 浏览 0 评论 0 原文

我的应用程序中有一个耙子任务,应该启动TCP服务器。 我已经将rake命令移至 entrypoint.sh 文件,并在docker-compose中添加了新服务,称为 tcp

当我运行 docker -compose -d 时,TCP服务返回不正确的路径。我尝试了不同的设置方式,但没有运气。如何为耙子任务构建Docker图像?

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

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:

I have a rake task in my application, that should start a TCP server.
I have moved rake command to an entrypoint.sh file and in docker-compose added new service, which is called tcp.

That tcp service return incorrect path, when I run docker-compose up -d. I tried different ways to setup, but no luck. How do I build docker image for a rake task?

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

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2025-01-30 14:31:19

您应该拥有构建:包含您的导轨应用程序的相同图像。覆盖命令:运行耙任务。在撰写文件中,它应该大致像:

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: { ... }

sidekiq 容器看起来非常相似。

由于您有两个标记构建的容器:。,Compose将尝试两次构建图像​​。但是,这些图像具有相同的Dockerfiles,并在相同的源树上运行。这意味着Docker的层缓存将生效,第二个构建将非常快速运行,并且实际上不会产生新的图像。

为了使这项工作正确,您还需要正确地构建Dockerfile。在这里,您必须将实际命令作为Dockerfile cmd 而不是 entrypoint (或者至少必须与您在 docker-compose.yml )。与往常一样,请确保图像是独立的,并包含其所有库依赖项和应用程序代码(请注意卷的不存在:在上面的撰写文件中)。

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

您不应将命令放入 entrypoint 或入口点脚本中。相反,使用入口点进行一些首次设置,也许像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 "$@"

命令:非常简单地覆盖,您提供的任何内容显示在脚本的最终语句中的“ $@” 中。因此,如果您想要一个撬台控制台,则可以

docker-compose run app \
  rails console

在命令行处覆盖命令:,而图像的 entrypoint 不变。示例组合文件对您的TCP服务器的启动作为耙子任务也相同(而且,我希望您的Sidekiq Worker以相同的方式工作)。

You should have it build: the same image that contains your Rails application. Override the command: to run the Rake task. In the Compose file it should look roughly like:

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.

Since you have two containers labeled build: ., Compose will try to build the image twice. However, these images have identical Dockerfiles and run on an identical source tree. 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.

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). 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).

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. Instead, use the entrypoint to do some first-time setup, and maybe inject related wrappers like 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. So if you want a Pry console, for example, you can

docker-compose run app \
  rails console

which will override the command: at the command line, leaving the image's ENTRYPOINT unchanged. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文