简体   繁体   中英

Run docker-compose from Docker container

I have Jenkins running inside a Docker container with docker.sock mounted. Can I call docker-compose from this container to run a service on host machine? 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.

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.

Then, in your Jenkins job:

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

Why is that?

Keep in mind that docker-compose gives instructions to the docker engine. The docker engine runs on the docker host (and not in the Jenkins container). So any path given by docker-compose to the docker engine must exist on the docker host.

Create your own dockerfile that is based on image you use for build (probably docker:latest)

Then, in RUN line put downloading the docker-compose and setting it as executable.

Spin up jenkins agent to build from your image instead default one.

you need to install docker-compose on that build container, not on jenkins master.

For builds in gitlab-ci I had a special build container that based on docker image with compose installed additionally. 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. 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.

Also, you could try to install compose as part of your build. 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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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