Issue
I'm using Docker in a Jenkins pipeline.
In the Dockerfile
, the WORKDIR
is specified like so:
WORKDIR /usr/src/directory
Then in the Jenkins pipeline, the container is deployed like so:
stage('Deploy') {
steps {
script {
docker.image('deployscript:latest').withRun("""\
-e 'IMAGE=${name}:${BUILD_ID}' \
-e 'CNAME=${name}' \
-e 'PORT=8000:80' \
-e 'PORT=8000:443' \
-e 'CONFIG={\"HostConfig\":{\"Binds\":[\"/local/home/certificates:/certificates\"]}}'""") { c ->
sh "docker logs ${c.id}"
}
}
}
}
The container is deployed but my problem is with the CONFIG
parameter; the /local/home/certificates
directory of the host machine is not being added to the container as a certificates
directory. What could I be doing wrong?
Solution
The following will stop the existing container and run a new one with a new image, and it will mount the host certificates directory into the container:
stage('Deploy') {
steps {
sh "docker stop ${IMAGE_NAME} || true && docker rm ${IMAGE_NAME} || true"
sh "docker run -d \
--name ${IMAGE_NAME} \
--publish ${PORT}:443 \
--mount type=bind,src=/local/home/certificates,dst=/certificates \
${IMAGE_NAME}:${BUILD_ID}"
}
}
Answered By - Adam Zarn