简体   繁体   English

从 Docker 容器运行 docker-compose

[英]Run docker-compose from Docker container

I have Jenkins running inside a Docker container with docker.sock mounted.我让Jenkins在安装了docker.sockDocker容器内运行。 Can I call docker-compose from this container to run a service on host machine?我可以从这个容器调用docker-compose来在主机上运行服务吗? I tried executing installation script from within a container, but it keeps saying我尝试从容器内执行安装脚本,但它一直说

"no such file or directory". “没有相应的文件和目录”。

docker exec jenkins curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

docker exec jenkins chmod +x /usr/local/bin/docker-compose

It is achievable but tricky to get it right.这是可以实现的,但很难做到正确。

You need to mount with a docker volume the path the docker-compose.yml file is in on your docker host, to the exact same location in the container.您需要使用 docker将 docker-compose.yml 文件在您的 docker 主机上的路径挂载到容器中完全相同的位置。

So if the docker-compose.yml file location is /home/leonid/workspace/project1/docker-compose.yml on the docker host, you need to add the volume -v /home/leonid/workspace/project1/:/home/leonid/workspace/project1/ for the jenkins container.所以如果docker-compose.yml文件位置是/home/leonid/workspace/project1/docker-compose.yml主机上的/home/leonid/workspace/project1/docker-compose.yml ,则需要添加volume -v /home/leonid/workspace/project1/:/home/leonid/workspace/project1/用于 jenkins 容器。

Then, in your Jenkins job:然后,在您的 Jenkins 工作中:

cd /home/leonid/workspace/project1/
docker-compose up -d

Why is that?这是为什么?

Keep in mind that docker-compose gives instructions to the docker engine.请记住,docker-compose 向 docker 引擎提供指令。 The docker engine runs on the docker host (and not in the Jenkins container). docker 引擎在 docker 主机上运行(而不是在 Jenkins 容器中)。 So any path given by docker-compose to the docker engine must exist on the docker host.因此 docker-compose 提供给 docker 引擎的任何路径都必须存在于 docker 主机上。

Create your own dockerfile that is based on image you use for build (probably docker:latest)根据用于构建的图像创建自己的 dockerfile(可能是 docker:latest)

Then, in RUN line put downloading the docker-compose and setting it as executable.然后,在 RUN 行中下载 docker-compose 并将其设置为可执行文件。

Spin up jenkins agent to build from your image instead default one.启动 jenkins 代理以从您的图像构建而不是默认一个。

you need to install docker-compose on that build container, not on jenkins master.您需要在该构建容器上安装 docker-compose,而不是在 jenkins master 上。

For builds in gitlab-ci I had a special build container that based on docker image with compose installed additionally.对于 gitlab-ci 中的构建,我有一个特殊的构建容器,它基于 docker 镜像并另外安装了 compose。 I think this is your case - you are using jenkins to spin a container based on docker:latest which by default does not have docker-compose.我认为这是你的情况 - 你正在使用 jenkins 来旋转基于 docker:latest 的容器,默认情况下它没有 docker-compose。 You need to either create own image that is from docker:latest, install compose or use some image from docekrhub that is done like this.您需要创建自己的来自 docker:latest 的图像,安装 compose 或使用来自 docekrhub 的一些图像,就像这样完成。

Also, you could try to install compose as part of your build.此外,您可以尝试将 compose 作为构建的一部分进行安装。 Just download it to some local dir and use it from there.只需将它下载到某个本地目录并从那里使用它。

The "Install as a container" section in the docs worked for me: https://docs.docker.com/compose/install/文档中的“作为容器安装”部分对我有用: https : //docs.docker.com/compose/install/

sudo curl -L --fail https://github.com/docker/compose/releases/download/1.25.0/run.sh -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

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

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