【问题标题】:Docker plugin: Access drone services from within the Dockerfile build processDocker 插件:从 Dockerfile 构建过程中访问无人机服务
【发布时间】:2018-07-22 21:25:17
【问题描述】:

我正在使用drone/drone:0.8 和 Docker 插件,但我对用于构建应用程序的 Dockerfile 有点卡住了。

此 Dockerfile 作为其构建过程的一部分运行应用测试套件 - 显示相关片段:

# ENV & ARG settings:
ENV RAILS_ENV=test RACK_ENV=test
ARG DATABASE_URL=postgres://postgres:3x4mpl3@postgres:5432/app_test

#  Run the tests:
RUN rails db:setup && rspec

测试套件需要连接到数据库,为此我在 .drone.yml 文件中包含了 postgres 服务:

pipeline:
  app:
    image: plugins/docker
    repo: vovimayhem/example-app
    tags:
    - ${DRONE_COMMIT_SHA}
    - ${DRONE_COMMIT_BRANCH/master/latest}
    compress: true
    secrets: [ docker_username, docker_password ]
    use_cache: true
    build_args:
    - DATABASE_URL=postgres://postgres:3x4mpl3@postgres:5432/app_test

services:
  postgres:
    image: postgres:9-alpine
    environment:
    - POSTGRES_PASSWORD=3x4mpl3

但看起来无人机文件中定义的服务无法从构建过程中访问:

Step 18/36 : RUN rails db:setup && rspec
 ---> Running in 141734ca8f12

could not translate host name "postgres" to address: Name does not resolve
Couldn't create database for {"encoding"=>"unicode", "schema_search_path"=>"partitioning,public", "pool"=>5, "min_messages"=>"log", "adapter"=>"postgresql", "username"=>"postgres", "password"=>"3x4mpl3", "port"=>5432, "database"=>"sibyl_test", "host"=>"postgres"}
rails aborted!
PG::ConnectionBad: could not translate host name "postgres" to address: Name does not resolve

有没有我遗漏的配置?或者这是插件中当前不存在的功能?

我知道这可能与 docker build 命令中的 --network 和/或 --add-host 选项有关...如果您认为我们应该包含此行为,我可以提供帮助。

【问题讨论】:

  • 我在节点项目和针对临时数据库的测试中遇到了这个问题。我无法在构建步骤中进行测试,不得不将其移至提供服务的外部无人机步骤。
  • 我已经确认可以从管道步骤访问数据库 - 使用之前的“check-postgres”步骤 - 但在下一个(插件/docker)步骤中,找到服务的网络在构建过程中内部不可用...我确定构建网络未添加到 docker build 命令中。
  • 我评论了drone ci的subreddit:reddit.com/r/droneci/comments/91a3fh/…

标签: docker drone.io


【解决方案1】:

所以我突然想到了一些事情(虽然我没有完整的上下文,所以请采纳你认为有意义的内容)

  1. 我可能会将代码的构建/测试部分分离到不同的步骤中,然后使用 docker 插件发布已通过的工件
  2. 我认为docker插件真的是为了发布镜像(由于dind我不相信它的容器能够到达服务容器)
  3. 如果您确实将其分开,您可能需要在构建的命令部分中使用- sleep 15 以使数据库有时间启动

http://docs.drone.io/postgres-example/ 提供了如何使用 postgres 的示例,但同样需要将构建部分与创建和发布 docker 映像分开:)

这是我正在谈论的示例;)

pipeline: tests-builds: //Should probably be separate :) image: python:3.6-stretch commands: - sleep 15 //wait for postgrest to start - pip install --upgrade -r requirements.txt - pip install --upgrade -r requirements-dev.txt - pytest --cov=sfs tests/unit - pytest --cov=sfs tests/integration //This tests the db interactio0ns publish: image: plugins/docker registry: quay.io repo: somerepot auto_tag: true secrets: [ docker_username, docker_password ] when: event: [ tag, push ] services: database: image: postgres

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多