I'm trying to adapt a docker build for jenkins. I'm following our docker-compose file and I'm creating a Jenkinsfile that is creating each container and linking them together. The problem I'm running into is that the docker-compose files declare a context that is not where the Dockerfile is. As far as I understand, jenkins will set the context to where the Dockerfile is, which puts files to be copied in a different relative location depending on whether the jenkinsfile or docker-compose file is building.
The folder structure is:
workspace
|-docker
|-db
|-Dockerfile
|-entrypoint.sh
This is how the Dockerfile declares the COPY instruction for the file in question
COPY docker/db/entrypoint.sh /
This is how my jenkinsfile builds the file. Which to my knowledge puts the context at that directory
docker.build("db", "${WORKSPACE}/docker/db")
the docker-compose file declares it like:
db:
build:
context: .
dockerfile: docker/db/Dockerfile
which puts the context at the root of the project.
Is there any way to tell a jenkinsfile to use the same context as the docker-compose file so that the Dockerfile's COPY instruction can remain unchanged and valid for both Jenkins and docker-compose? If that's not possible, does anyone know of any alternative solutions?
In Jenkins you have to add a new credential with your Docker Hub account details. Go to Credentials → Global → Add credentials and fill out the form with your username and password. Create your Jenkins pipeline.
Description. The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context ...
Docker plugin for JenkinsThe aim of this docker plugin is to be able to use a Docker host to dynamically provision a docker container as a Jenkins agent node, let that run a single build, then tear-down that node, without the build process (or Jenkins job definition) requiring any awareness of docker.
It turns out that I was pulling a previous version (1.6) of the docker-pipeline-plugin. The function in question has been since updated (1.7) to allow the second parameter to designate a Dockerfile location outside the context.
The updated statement in my Jenkinsfile is:
return docker.build("db", "-f docker/db/Dockerfile .")
And this allows my container to build without modifying the expected context of the developer's docker-compose or Dockerfiles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With