buffcat
buffcat

Reputation: 295

Jenkins: Can't run commands inside Docker container

I am trying to build a simple Docker image and then run commands inside it using Jenkins Pipeline. I'm confused because I seem to be executing commands on my build server instead of inside my docker container. When I run 'pwd' inside my built container by running the following:

node ('docker2') {
    docker.withRegistry('https://my-registry.com', 'my-creds') {

        stage ('Checkout SCM') {
            checkout scm
        }

        // Build Docker image from Dockerfile
        stage ('Build Docker Image') {
            def myImage = docker.build("my-image:${env.BUILD_ID}")

            myImage.inside {
                sh 'pwd'
            }
        }
    }
}

It returns "/home/build/workspace/Docker-Build-And-Push", which is seemingly the working directory from which I am building my container, not a directory inside the container itself.

Furthermore when I try to run "mkdir test" inside my container it created a test directory in my build server, not the container. But when I run "hostname" it doesn't return the hostname of my build server, but rather a random string of numbers, so that seems to be coming from my container...

My Dockerfile is extremely simple and just contains "FROM alpine:latest".

It seems to me that my commands should be running inside my container and not on my build server. What am I doing wrong?

Upvotes: 0

Views: 3436

Answers (1)

Michael
Michael

Reputation: 2683

Jenkins mounts the workspace in you docker container using the -v parameter. Therefore you see your build server's directory structure and if you create a file it is actually created in the workspace.

You should be able to see the exact docker command in the logs of you build.

Upvotes: 1

Related Questions