简体   繁体   中英

Restart Docker container with newly built image through jenkins (running in a container)

I have a docker-compose.yml file that defines all the services I need. The important parts are the following:

services:
  backend-api:
    build: .
  jenkins:
    build: ./jenkins
    volumes:
      - jenkins_data:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock

The backend-api is a simple Java application built with gradle and the jenkins service is the standard jenkins image with docker installed into it.

Dockerfile of backend-api:

FROM gradle:jdk8 as builder

COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build

FROM openjdk:8-jre-slim
EXPOSE 8080
COPY --from=builder /home/gradle/src/backend/build/distributions/backend.tar /app/
WORKDIR /app
RUN tar -xvf backend.tar
WORKDIR /app/backend
CMD bin/backend

I want to rebuild and restart the backend-api service from the jenkins container. I read about his a lot and looked at the CloudBees docker plugins for jenkins but I cant figure out how to restart and rebuild the container on the host from inside the jenkins container.

Is it possible to achieve this somehow or is my whole approach to this wrong?

Quickly answer

You need to configure properly the docker client inside jenkins docker container. Here is the Dockerfile or the entire post

After that you can use -H docker parameter to exec any command in remote dockers. Foe example I use this to see my remote logs from another another machine:

docker -H 10.10.10.66:2375 logs --tail 500 my-app

Note: You must to enable docker rest api before!

Long answer

The common, easy, manual and proven way to work with C.I and docker is:

  • Install your c.i in one server.
  • Install or configure your docker apps in another servers, not in the same host of your c.i
  • Enable docker rest api in your remote docker servers. This allow you to connect and execute docker commands from your c.i with docker to another remote machine with docker.
  • Configure your c.i (jenkins in your case) in order to execute tasks in your other servers which has docker installed.
  • Trigger jenkins tasks/jobs manually or with webhooks
  • This jenkins tag must be build and push the docker image to your private docker registry
  • Finally this task could download or pull the new image from private registry to the target docker server

Note:

  • You can use -H parameter instead api rest for commands execution in remote docker hosts.
  • You can push your images to docker public hub following this guide if you don't have a private docker registry or the required money to implement it. Be careful with the source code and variables if you choose the public https://hub.docker.com
  • If you cannot have a private docker registry server $$ and you can not push your enterprise code to public docker registry, you can install a private docker registry in your jenkins host. Not recommended but: what can we do?
  • If install docker registry alongside your jenkins is not an option you could create a task in your jenkins which will download the source code (Dockerfile and app files) int the remote machine and exec docker build in the remote machine.

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