在同级容器中使用文件夹将其转移到其他阶段

时间:2019-12-10 10:18:15

标签: docker gitlab-ci gitlab-ci-runner

我有一个分为两个阶段的管道。在第一个中,我基于“ node:latest”中的Dockerfile构建了一个映像。在此阶段,我运行“ npm run build”,并创建生成文件夹。 在第二阶段中,我想使用以前构建的“ build”文件夹并在“ nginx”容器中运行它。 但是我无法在Gitlab-ci ce中的各个阶段之间共享文件夹。

我将docker executor用作gitlab-runner。 gitlab-ci-yml

image: docker:latest
services:
  - name: docker:dind
    command: ["--insecure-registry=mygitlab:5050"]

stages:
- build
- test
- release

variables:
  DEV_IMAGE: mygitlab:5050/development/registry:dashboard-dev
  RELEASE_IMAGE: mygitlab:5050/development/registry:dashboard-prod
  DOCKER_TLS_CERTDIR: "/certs"
  GIT_SSL_NO_VERIFY: "1"

before_script:
    - docker login -u root -p my-token mygitlab:5050

build:
  stage: build
  script:
    - docker build -t $DEV_IMAGE . --target=dashboard-dev
    - docker push $DEV_IMAGE
    - docker run --name dashboard-dev -d $DEV_IMAGE tail -f /dev/null
    - docker cp dashboard-dev:/src/app/build /cache
    - docker stop dashboard-dev
    - docker rm dashboard-dev
release:
  stage: release
  script:
    - docker build -t $RELEASE_IMAGE . --target=dashboard-prod
    - docker push $RELEASE_IMAGE
  only:
    - master

这是我的 Dockerfile

FROM mygitlab:5050/development/registry:dashboard-dev AS dashboard-dev

EXPOSE 3000
COPY  . .
RUN npm install
RUN npm run build

FROM mygitlab:5050/development/registry:nginx AS dashboard-prod 
COPY builds/build /usr/share/nginx/html

这是我的 gitlab-runner config.toml

concurrent = 10
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "dash-dock"
  url = "https://mygitlab/"
  token = "my-token"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.docker]
    pull_policy = "if-not-present"
    tls_verify = false
    extra_hosts = ["mygitlabDomain:mygitlabIP"]
    image = "docker:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache", "/certs/client", "/builds"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

1 个答案:

答案 0 :(得分:1)

如果要从以前的多级容器复制,则需要在docker文件中指明:

COPY --from=dashboard-dev builds/build /usr/share/nginx/html

有关更多信息,请参见the COPY documentation