简体   繁体   English

Gitlab-Runner 向 docker 容器发出命令

[英]Gitlab-Runner issue commands to docker containers

I have the following docker network configuration (this was generated by my docker-compose.yml file):我有以下 docker 网络配置(这是由我的 docker-compose.yml 文件生成的):

[
    {
        "Name": "docker-config_private",
        "Id": "ed4e2db14df4930efeaa9174110bc1f72b754d727513ebfa1609c5d0c07ffabf",
        "Created": "2021-03-11T17:26:36.392514302Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3b72fd684b61e19f1fdcb688823ba5d0e2b3be48fdb335f3483b1e55d8f1781a": {
                "Name": "opensource-varnish",
                "EndpointID": "76cd55be12f7ada945e91588a219bc029b8a592c6727ce67547c24be03bb047c",
                "MacAddress": "02:42:ac:13:00:08",
                "IPv4Address": "172.19.0.8/16",
                "IPv6Address": ""
            },
            "3fbd1d890d567ef163b221c36b1e1a082af18a146c396f43cefa2fb4bb13dd02": {
                "Name": "db-backup",
                "EndpointID": "2951e41b83bb2684aa49e1ec1e63d80c78ba8ac0f7189b6ccb5675145bff8ecf",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "58f489d4540aea1ace7d07a9f3c3bafc5b7c4690b391e4f19818fb1bf791c300": {
                "Name": "opensource-redis",
                "EndpointID": "251c4035c8fbb41b2034bd0485cdef1f9eb89ecc991f39a401d83d786b2333af",
                "MacAddress": "02:42:ac:13:00:04",
                "IPv4Address": "172.19.0.4/16",
                "IPv6Address": ""
            },
            "6d5ce137c49c8f2138ee0888a889e7a5fad410b3dd592f9a076f45ea75050b15": {
                "Name": "opensource-db",
                "EndpointID": "b034c8eab9781d1bb461f9593843d8d251bac4e046c1a830586bd3b702bfddb3",
                "MacAddress": "02:42:ac:13:00:05",
                "IPv4Address": "172.19.0.5/16",
                "IPv6Address": ""
            },
            "707e3f229ab3015ec1db0a570faece043c5cce9a61a870730e4b198eb26cd067": {
                "Name": "gitlab-runner",
                "EndpointID": "dc080c0cf4981a2d703602e4179601caec474503ee9ebf249b28ee9bab4c39c2",
                "MacAddress": "02:42:ac:13:00:07",
                "IPv4Address": "172.19.0.7/16",
                "IPv6Address": ""
            },
            "70b68c92e966d208cf6210d9ade3fbbd12d46169f95682630eafb388573bcf17": {
                "Name": "gitlab",
                "EndpointID": "e1822cac6fa0c130bb13d352174262e4657bdd87b7220c8d36f145cd6c2d8cd7",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            },
            "728cf051d51682c753e8e27391a4678a4af64c4c53ea06c9758ea8a7f3367815": {
                "Name": "opensource-magento",
                "EndpointID": "dca2b4ba83a793c550401ee5b1444f859c178d55c290784bed9f011af734e286",
                "MacAddress": "02:42:ac:13:00:06",
                "IPv4Address": "172.19.0.6/16",
                "IPv6Address": ""
            },
            "7964fa137a537c2ba4132d2c8e9fc492586323560afffc4802a3e94fae33db68": {
                "Name": "opensource-cron",
                "EndpointID": "db478da15938f5875e1b5b98040008870dc2227c2bd0ce1405cbbde9b7df4455",
                "MacAddress": "02:42:ac:13:00:09",
                "IPv4Address": "172.19.0.9/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "private",
            "com.docker.compose.project": "docker-config",
            "com.docker.compose.version": "1.28.5"
        }
    }
]

As you can see I have a gitlab-runner:alpine instance running alongside all of the other docker containers.如您所见,我有一个 gitlab-runner:alpine 实例与所有其他 docker 容器一起运行。

What I need is for a runner registered inside that instance to deploy some code, this being:我需要的是在该实例中注册的跑步者来部署一些代码,这是:

  1. Pulling code from a remote gitlab从远程提取代码 gitlab
  2. Issuing commands inside those containers (for instance docker exec... bin/magento c:ci )在这些容器内发出命令(例如docker exec... bin/magento c:ci
  3. Restarting a docker container重新启动 docker 容器

Is there any way for me to achieve this?我有什么办法可以做到这一点? If so, what executer do I need to run the commands I need?如果是这样,我需要什么执行器来运行我需要的命令?

I've already tried to use a local gitlab-runner installation and use the shell executor to be able to issue commands, but running gitlab inside a docker container is so much resource-saving!我已经尝试使用本地 gitlab-runner 安装并使用 shell 执行程序来发出命令,但是在 Z05B6053C41A2130AFD6FC3B158BDA4E 容器中运行 gitlab 是非常节省资源的!

The best way to use gitlab-runner to do something is to run a pipeline in gitlab-ci使用 gitlab-runner 做某事的最好方法是在 gitlab-ci 中运行管道

Do you have a project in gitlab?您在 gitlab 有项目吗? Set a pipeline.设置管道。 It will trigger for example every time you push code例如,每次推送代码时都会触发

tree ways:树方式:

1. Install the gitlab-runner and configure as shell runner add a label (for example custom-docker) 1.安装gitlab-runner并配置为shell runner添加一个label(例如custom-docker)

On you gitlab-ci.yml the script should be something like:在你的 gitlab-ci.yml 脚本应该是这样的:

my-job:
  tags:
    - custom-docker
  script:
    - docker exec ... bin/magento c:ci

Configure a gitlab runner as docker executer, mount the docker volume for the docker service and you can replicate the above job configuration将 gitlab 运行器配置为 docker 执行器,安装 docker 卷用于 Z05B6053C3B15A2130AFD6

3. Make you docker host of the machine available via ssh (search docker over ssh) that way your runner can be deployed on another machine and use the docker image to remote connect and run the same as above 3. Make you docker host of the machine available via ssh (search docker over ssh) that way your runner can be deployed on another machine and use the docker image to remote connect and run the same as above

my-job:
  image: docker
  tags:
    - custom-docker
  before_script:
    - add the SSH key
    - ignore the known_host everification
    - set DOCKER_HOST=ssh://user@server
  script:
    - docker exec ... bin/magento c:ci

Did you look into docker in docker?您是否查看了 docker 中的 docker? Dind.丁。 It's a runner exec that has docker engine, inside the docker container, you tell it which image to run.它是具有 docker 引擎的 runner exec,在 docker 容器内,您可以告诉它要运行哪个映像。 Each ci step can run different image.每个 ci 步骤可以运行不同的图像。 If you choose a docker image you can compose etc.如果您选择 docker 图像,您可以进行构图等。

I don't think using docker exec inside ci is a good idea, the runner context is going to die when ci ends.我不认为在 ci 中使用 docker exec 是一个好主意,当 ci 结束时,运行器上下文将会消失。

If you're trying to run the images produced in ci, start them outside of runner.如果您尝试运行在 ci 中生成的图像,请在 runner 之外启动它们。 We use watchtower for that, but there are other methods.我们为此使用了望塔,但还有其他方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM