Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker.image().inside() doesn't run sh command inside my Image

I am writing a Jenkins pipeline. I am creating a docker image that runs a command from python virtualenv(inside the image). I am creating this virtual env while creating the docker image. When I do below:

stage() {
  steps {
    script {
      def dockerImage = docker.build('-f <path_to_dockerfile> .')
      dockerImage.inside() {
        sh 'venv/bin/isort src'
      }
    }
  }
}

It says that 'venv/bin/isort' is not found. if I do a 'pwd' then it shows path to the host machine jenkins workspace. Jenkins workspace has a checkout copy from my git repo and repo doesn't contain the venv(Virtualenv). But, I expect this should return path inside my docker container.

My Docker file :

FROM python:3.6-alpine

RUN apk update && apk add bash && apk add build-base

RUN mkdir /app

WORKDIR /app

COPY requirements /app/requirements

COPY Makefile /app/

RUN make venv/.venv_build

Am I doing something wrong ?

like image 826
pelican Avatar asked Sep 02 '25 14:09

pelican


1 Answers

If you look carefully at the Jenkins logs, the docker run command it executes does a lot of things to simulate the workspace environment. image.inside { ... } bind-mounts the workspace directory into the container at the same path, overrides the current directory to be the workspace directory, and overrides the default command, among other things.

I think there are two good options here:

  1. Explicitly specify the absolute path to the program when you sh the command.

    dockerImage.inside() {
      sh '/app/venv/bin/isort src'
    }
    
  2. Use image.run to run the image "normally", maybe with a -v option to bind-mount parts of the workspace directory into the container. This is the same way you'd docker run the container.

    dockerImage.run("-v ${env.WORKSPACE}/src:/app/src")
    

    This construct would run whatever the default CMD is in your image; that line seems to be missing from the Dockerfile in the question.

like image 108
David Maze Avatar answered Sep 05 '25 12:09

David Maze