I have the following docker network configuration (this was generated by my docker-compose.yml file):
[
{
"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.
What I need is for a runner registered inside that instance to deploy some code, this being:
- Pulling code from a remote gitlab
- Issuing commands inside those containers (for instance
docker exec ... bin/magento c:ci
) - Restarting a docker container
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!
3
Answers
The best way to use gitlab-runner to do something is to run a pipeline in gitlab-ci
Do you have a project in 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)
On you gitlab-ci.yml the script should be something like:
Configure a gitlab runner as docker executer, mount the docker volume for the docker service and you can replicate the above job configuration
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
Did you look into docker in docker? Dind.
It’s a runner exec that has docker engine, inside the docker container, you tell it which image to run. Each ci step can run different image.
If you choose a docker image you can compose etc.
I don’t think using docker exec inside ci is a good idea, the runner context is going to die when ci ends.
If you’re trying to run the images produced in ci, start them outside of runner. We use watchtower for that, but there are other methods.