简体   繁体   English

如何在 gitlab-runner 容器内运行的 docker 中的 docker 中运行 docker-compose?

[英]How to run docker-compose inside docker in docker which runs inside gitlab-runner container?

I have a gitlab runner inside a docker container, runs fine if I run an image like nginx.我在 docker 容器中有一个 gitlab runner,如果我运行像 nginx 这样的图像,运行良好。 But now I tried to run docker in docker (dind) inside the gitlab runner and I want to run docker-compose inside the dind.但是现在我尝试在 gitlab runner 中的 docker (dind) 中运行 docker,我想在 dind 中运行 docker-compose。 Docker info runs fine, but if I try to run docker-compose I get an permission-denied error. Docker 信息运行良好,但如果我尝试运行 docker-compose,我会收到一个权限被拒绝的错误。

I linked the /usr/local/bin/docker-compose file to the gitlab runner container and enter it in the volumes parameter in the runner config.toml file.我将 /usr/local/bin/docker-compose 文件链接到 gitlab runner 容器,并将其输入到 runner config.toml 文件的卷参数中。

If I try to run sudo it ends with an unknown command error, so that could not be the solution.如果我尝试运行 sudo,它会以未知的命令错误结束,因此这不是解决方案。

Do I have to link some file more or are the to many nested containers?我是否必须更多地链接一些文件,还是链接到许多嵌套容器?

In order to have docker-compose , you need to install it for image docker , which is of version 18.09.6, build 481bc77 at the time of writing.为了让docker-compose ,您需要安装它的图像泊坞窗,这在写作的时候是18.09.6版本,构建481bc77。

Since docker-compose version 1.24.0 , you also need the following dependencies for docker-compose to be installed on alpine :docker-compose 版本 1.24.0 开始,您还需要以下依赖项才能将docker -compose 安装在 alpine 上

apk add py-pip python3-dev libffi-dev openssl-dev gcc libc-dev make

Here is a sample .gitlab-ci.yml :这是一个示例.gitlab-ci.yml

image: docker:stable

stages:
    - deploy

services:
  - docker:dind

before_script:
  - apk update
  - apk add py-pip python3-dev libffi-dev openssl-dev gcc libc-dev make
  - pip install docker-compose

deploy_app:
    stage: deploy
    script:
        - docker-compose down
        - docker-compose up -d

if you are using dind it means docker is working OK, now you just have to install docker-compose that is just simple python package and you can do it in before_script如果您使用的是 dind,则意味着 docker 工作正常,现在您只需要安装 docker-compose 这只是简单的 python 包,您可以在 before_script 中完成

.gitlab-ci.yml .gitlab-ci.yml

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay2

stages:
- test

before_script:
  - apk add --no-cache py-pip
  - pip install docker-compose
  - docker info
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN docker.registry.com

test:
  stage: test
  script:

    - cp .env.sample .env # copy environement variable
    - docker-compose up -d
    # run some test here

The best choice for running docker-compose inside docker in docker is using the docker/compose image as bellow:在 docker 中运行 docker-compose 的最佳选择是使用docker/compose图像,如下所示:

job-x:
  image: docker/compose
  script:
    - docker-compose version

A real and full example of this is using docker-compose to deploy with specific installed runner on a server as bellow:一个真实且完整的示例是使用 docker-compose 在服务器上部署特定安装的运行器,如下所示:

image: docker:stable

services:
  - docker:dind

stages:
  - build
  - deploy

before_script:
  # resolve TAG
  # Default branch leaves tag latest
  # All other branches are tagged with the escaped branch name (commit ref slug)
  - |
    if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
      TAG="latest"
      echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = '$TAG'"
    else
      TAG="$CI_COMMIT_REF_SLUG"
      echo "Running on branch '$CI_COMMIT_BRANCH': tag = $TAG"
    fi
  

docker-build:
  stage: build
  script:
    - echo "user ${CI_REGISTRY_USER}"
    - echo "registry ${CI_REGISTRY}"
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from "$CI_REGISTRY_IMAGE:${TAG}" -t "$CI_REGISTRY_IMAGE:${TAG}" .
    - docker push "$CI_REGISTRY_IMAGE:${TAG}"
  when: manual

deploy-staging:
  image: docker/compose
  stage: deploy
  variables:
    IMAGE: $CI_REGISTRY_IMAGE
    TAG: $TAG
    HOST_PORT: $HOST_PORT
  script:
    - docker-compose version
    - docker-compose -f docker-compose.yml up -d --no-build
  when: manual

The docker-compose.yml file seems like: docker-compose.yml 文件看起来像:

version: "3.7"

services:
  web:
    image: $IMAGE:$TAG
    build:
      context: .
    ports:
      - $HOST_PORT:8000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 docker 容器内的 gitlab-runner 发出命令 - Issue commands from a gitlab-runner inside docker container 在Gitlab-runner docker容器中安装软件包 - Install package inside a Gitlab-runner docker container docker-compose postgres卷将所有权更改为gitlab-runner - docker-compose postgres volume changes ownership to gitlab-runner Gitlab-runner + Docker-compose部署方案:主机服务器重启后如何正确重启容器 - Gitlab-runner + Docker-compose deploying scheme: how to properly restart containers after reboot of host server 在docker:dind中运行docker-compose - Run docker-compose inside docker:dind Gitlab-runner:在 root 中找不到 docker 或 docker-compose 但它们已经安装 - Gitlab-runner: docker or docker-compose not found in root but they already installed docker-compose 中的 Gitlab-runner 无法连接 http://docker:2375/v1.40/info - Gitlab-runner in docker-compose cannot connect http://docker:2375/v1.40/info 如何在 docker-compose 中的一个 docker 容器中添加选项 - how to add option in one docker container inside docker-compose 在容器内从 docker-compose 命令运行 shell 脚本 - Run a shell script from docker-compose command, inside the container maven:来自 gitlab-runner 的最新图像,带有 docker 执行器“无法连接到 gitlab 连接被拒绝”使用 ZBAEDBAE645E0857 - maven:latest image from gitlab-runner with docker executer “Failed to connect to gitlab Connection refused” using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM