jenkinsci / docker-agent

Jenkins agent (base image) and inbound agent Docker images
https://hub.docker.com/r/jenkins/inbound-agent/
MIT License
282 stars 228 forks source link

How can I build docker images from inside jenkins/agent #783

Open objt-pe opened 5 months ago

objt-pe commented 5 months ago

Describe your use-case which is not covered by existing documentation.

We want to create docker builds from inside the jenkins/agent to push them to an artifactory. Does anyone have a clue how to do this?

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

Are you interested in contributing to the documentation?

No response

gounthar commented 5 months ago

I guess you would have to use DinD or DonD, or use only the docker client and target a docker engine elsewhere. 🤔

zhan9san commented 1 week ago

Hi @objt-pe

It depends how do you use jenkins/agent image.

If you are using k8s plugin with jenkins/agent, generally, the jnlp container(jenkins/agent) acts as a sidecar container and it is recommended to build docker images in your own image.

If you want to use customized jnlp image, you can add jenkins-agent as your customized image's entrypoint like below.

podman is daemon-less. If can be used inside the container.

# Install podman here

WORKDIR /opt
RUN wget https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2_linux-x64_bin.tar.gz && \
  tar -zxvf openjdk-21.0.2_linux-x64_bin.tar.gz && \
  rm openjdk-21.0.2_linux-x64_bin.tar.gz
ENV JENKINS_JAVA_BIN=/opt/jdk-21.0.2/bin/java

ARG VERSION=3261.v9c670a_4748a_9
RUN curl --create-dirs -fsSLo /usr/share/jenkins/agent.jar https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/${VERSION}/remoting-${VERSION}.jar \
  && chmod 755 /usr/share/jenkins \
  && chmod 644 /usr/share/jenkins/agent.jar

# Set Jenkins connection entrypoint
RUN curl -fsSLo /usr/local/bin/jenkins-agent https://raw.githubusercontent.com/jenkinsci/docker-agent/3261.v9c670a_4748a_9-2/jenkins-agent
RUN chmod a+x /usr/local/bin/jenkins-agent
ENTRYPOINT ["/usr/local/bin/jenkins-agent"]

Feel free to let me know if you have any concerns.