首页 > 解决方案 > How to use docker container as host with ansible running in a docker container on gitlab-ci?

问题描述

I would like to test our ansible deployment with gitlab-ci using docker containers. I'm trying to create a job which uses a docker conainter installed ansible in it and use another docker container as host for an ansible-playbook.

deploy-test:
    stage: deploy-test
    image: stretch-ansible-docker-container
    script:
        - ansible-playbook -i <use-another-docker-container> test-deploy.yaml

How can I create and define the docker-container which should be used as host?

标签: dockeransiblegitlab-ci

解决方案


有不同的方法,主要取决于您是要docker-in-docker用于构建 docker 还是仅共享主机的/var/run/docker.sock. 如果是第二种选择,您的步骤会像这样:

首先为你可爱的容器创建 Dockerfiles。可能将它们放在项目的源代码中,比如说Dockerfiles\子文件夹

然后在您的管道中添加一个阶段,例如: gitlab-ci.yml

stages:
  - prepare-dockers
  - test

prepare:
  stage: prepare-dockers
  image: docker:latest
  script:
    - docker build -t <use-another-docker-container> Dockerfiles

因此,您可以在下一阶段准备好您的容器。不要忘记,如果您使用 docker 共享(不是 pure dind),您将连接到主机的 docker 注册表,并且所有图像都会出现在那里。

但是你也不应该忘记通过编辑gitlab-config.toml来共享 docker socket 以允许 docker 共享(公开/var/run/docker.sock)。


推荐阅读