Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用在构建阶段创建的文件与同一阶段中的dockerfile?_Docker_Gitlab Ci - Fatal编程技术网

如何使用在构建阶段创建的文件与同一阶段中的dockerfile?

如何使用在构建阶段创建的文件与同一阶段中的dockerfile?,docker,gitlab-ci,Docker,Gitlab Ci,我正在使用Gitlab CI进行练习,以了解如何构建应用程序,然后在Docker映像中使用它。目前,我的repo只是由helloworld.txt、dockerfile和gitlab-ci.yml组成 问题:在构建阶段,我使用一个shell执行器来“zip helloworld.zip helloworld.txt”。然后,我选择了“docker build-t myproject/myapp”。我希望复制helloworld.zip/,但我创建的zip文件在docker构建上下文中似乎不可用。

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

问题:在构建阶段,我使用一个shell执行器来“zip 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映像中的给定目录中

编辑

在了解到我不能这样做之后,我创建了两个不同的阶段:构建应用程序和构建容器。我也知道工件默认会在以下阶段中使用,所以我没有将工件添加到第一个阶段,也没有将依赖项添加到下一个阶段。这是下面的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

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

有关
gitlab ci.yml
中的
依赖项
关键字,请参见:

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

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

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


谢谢我创建了两个不同的阶段:构建应用程序和构建容器。我知道默认情况下会在下一阶段使用工件,但仍然会遇到相同的错误,即在docker构建过程中找不到helloworld.zip。在
build\u应用程序中创建了
zipfile.zip
,在
build\u container
helloworld.zip
中访问时。我想在build\u container阶段访问helloworld.zip时遇到了问题。看见