A. The img.push() should be called before docker.withRegistry
B. The docker.build method is missing the Dockerfile path
C. The credentials ID 'my-credentials' is invalid syntax
D. The img.inside block is outside the docker.withRegistry block and will fail
Solution
Step 1: Check scope of img variable
The variable img is defined inside the docker.withRegistry block, so it is not accessible outside that block.
Step 2: Identify the error with img.inside usage
Using img.inside outside the block where img is defined causes a runtime error because img is undefined there.
Final Answer:
The img.inside block is outside the docker.withRegistry block and will fail -> Option D
Quick Check:
Variable scope matters in Jenkins pipeline blocks [OK]
Hint: Keep variable usage inside the block where it's defined [OK]
Common Mistakes:
Assuming variables are global across blocks
Confusing credentials ID syntax
Thinking push must be outside withRegistry
5. You want to build a Docker image, run tests inside a container from that image, and then push the image to a private registry using Jenkins Docker Pipeline plugin. Which pipeline snippet correctly implements this workflow?
hard
A. def img = docker.build('myapp:test')
img.inside {
sh 'run-tests.sh'
}
docker.withRegistry('https://registry.example.com', 'creds') {
img.push()
}
B. docker.withRegistry('https://registry.example.com', 'creds') {
def img = docker.build('myapp:test')
img.push()
img.inside {
sh 'run-tests.sh'
}
}
C. docker.withRegistry('https://registry.example.com', 'creds') {
img.push()
def img = docker.build('myapp:test')
img.inside {
sh 'run-tests.sh'
}
}
D. def img = docker.build('myapp:test')
docker.withRegistry('https://registry.example.com', 'creds') {
img.push()
}
img.inside {
sh 'run-tests.sh'
}
Solution
Step 1: Build image and run tests inside container first
The image must be built before running tests inside it, so docker.build() and img.inside { } come first.
Step 2: Push image inside docker.withRegistry() block after tests
Pushing requires registry credentials, so img.push() must be inside docker.withRegistry() block after tests pass.
Step 3: Verify variable scope and order
def img = docker.build('myapp:test')
img.inside {
sh 'run-tests.sh'
}
docker.withRegistry('https://registry.example.com', 'creds') {
img.push()
} correctly builds image, runs tests inside container, then pushes image with credentials in proper order and scope.
Final Answer:
def img = docker.build('myapp:test')
img.inside {
sh 'run-tests.sh'
}
docker.withRegistry('https://registry.example.com', 'creds') {
img.push()
} -> Option A
Quick Check:
Build -> Test inside container -> Push with credentials [OK]
Hint: Build first, test inside container, then push inside withRegistry [OK]
Common Mistakes:
Pushing image before running tests
Using img.inside after pushing outside scope
Defining variables inside wrong blocks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.