【问题标题】:How to use files created during build stage with dockerfile in the same stage?如何将构建阶段创建的文件与同一阶段的dockerfile一起使用?
【发布时间】:2019-01-22 13:06:22
【问题描述】:

我正在使用 Gitlab CI 练习以了解如何构建应用程序,然后在 Docker 映像中使用它。目前,我的 repo 只包含 helloworld.txt、dockerfile 和 gitlab-ci.yml。

问题:在构建阶段,我使用 shell 执行程序来“压缩 helloworld.zip helloworld.txt”。然后,我“docker build -t myproject/myapp。”我希望复制 helloworld.zip /”但是似乎我创建的 zip 文件在 docker 构建上下文中不可用。我没有将 helloworld.zip 文件保存到正确的位置吗?或者是其他东西?我的长期意图是编写一个 python 应用程序,并在构建阶段编译成一个可执行文件并复制到一个 docker 容器中。

#cat helloworld.txt
hello world

#cat dockerfile
FROM centos:7
COPY helloworld.zip /
CMD ["/bin/bash"]

#cat gitlab-ci.yml
stages:
    - build
    - test
    - release
    - deploy

variables:
  IMAGE_TEST_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  IMAGE_RELEASE_NAME: $CI_REGISTRY_IMAGE:latest

before_script:
  - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin

build:
  stage: build
  script:
    - echo "compile the program"
    - zip zipfile.zip helloworld.txt 
    - docker build --pull -t $IMAGE_TEST_NAME .
    - docker push $IMAGE_TEST_NAME

test:
  stage: test
  script: 
    - docker pull $IMAGE_TEST_NAME
    - docker run $IMAGE_TEST_NAME yum install unzip -y && unzip /helloworld.zip && cat /helloworld.txt

release:
  stage: release
  script:
    - docker pull $IMAGE_TEST_NAME
    - docker tag $IMAGE_TEST_NAME $IMAGE_RELEASE_NAME
    - docker push $IMAGE_RELEASE_NAME
  only:
    - master

deploy:
  stage: deploy
  script:
    - ./deploy.sh
  only:
    - master
  when: manual

我希望在同一阶段(在本例中为构建)中,我可以运行诸如 zip 之类的程序,然后在 docker 构建过程中将该 zip 文件复制到新构建的 docker 映像中的给定目录中。

编辑

在得知我不能这样做之后,我创建了两个不同的阶段:build_app 和 build_container。还知道在以下阶段默认使用工件,我没有将工件添加到第一阶段或将依赖项添加到下一阶段。这是下面的 gitlab-ci.yml,仍然产生同样的错误。

stages:
    - build_app
    - build_container
    - test
    - release
    - deploy

# you can delete this line if you're not using Docker
#image: centos:latest
variables:
  IMAGE_TEST_NAME: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  IMAGE_RELEASE_NAME: $CI_REGISTRY_IMAGE:latest

before_script:
  - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin

build_app:
  stage: build_app
  script:
    - echo "compile the program"
    - zip zipfile.zip helloworld.txt 

build_container:
  stage: build_container
  script:
    - docker build --pull -t $IMAGE_TEST_NAME .
    - docker push $IMAGE_TEST_NAME

test:
  stage: test
  script: 
    - docker pull $IMAGE_TEST_NAME
    - docker run $IMAGE_TEST_NAME yum install unzip -y && unzip /helloworld.zip && cat /helloworld.txt

release:
  stage: release
  script:
    - docker pull $IMAGE_TEST_NAME
    - docker tag $IMAGE_TEST_NAME $IMAGE_RELEASE_NAME
    - docker push $IMAGE_RELEASE_NAME
  only:
    - master

deploy:
  stage: deploy
  script:
    - ./deploy.sh
  only:
    - master
  when: manual

工作状态: 构建应用程序:通过 构建容器:失败

Running with gitlab-runner 11.6.1 (8d829975)
  on gitrunner-shell trtHcQTS
Using Shell executor...
Running on gitrunner.example.com...
Fetching changes...
Removing zipfile.zip
HEAD is now at e0a0a95 Update .gitlab-ci.yml
Checking out e0a0a952 as newFeature...
Skipping Git submodules setup
$ echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
WARNING! Your password will be stored unencrypted in /home/gitlab-runner/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
$ docker build --pull -t $IMAGE_TEST_NAME .
Sending build context to Docker daemon  112.1kB

Step 1/3 : FROM centos:7
7: Pulling from library/centos
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Image is up to date for centos:7
 ---> 1e1148e4cc2c
Step 2/3 : COPY helloworld.zip /
COPY failed: stat /var/lib/docker/tmp/docker-builder312764301/helloworld.zip: no such file or directory
ERROR: Job failed: exit status 1

【问题讨论】:

    标签: docker gitlab-ci


    【解决方案1】:

    这是不可能的。 Gitlab CI 的作业模型假设同一阶段的作业是独立的。

    gitlab-ci.yml 中查看dependencies 关键字的manual

    此功能 [...] 允许您定义要在不同作业之间传递的工件。

    请注意,默认情况下会传递所有先前阶段的工件。

    [...] 您只能从当前阶段之前执行的阶段定义作业。

    【讨论】:

    • 谢谢。我创建了两个不同的阶段:build_app 和 build_container。知道默认情况下在下一阶段使用工件,我仍然收到相同的错误,即在 docker 构建期间找不到 helloworld.zip。
    • build_app中创建zipfile.zip,而在build_container中访问helloworld.zip
    • 我想我在 build_container 阶段遇到了访问 helloworld.zip 的问题。见stackoverflow.com/questions/54314886/…
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    相关资源
    最近更新 更多