Jenkins sh command is stuck

Hi

sh command inside the Jenkinsfile is stuck. Here is the content:

pipeline {
    agent {
        docker {
            image 'my-maven-aws-cli'
            args "-u jenkins"
        }
    }
    
    stages {
        stage('Build') {
            steps {
                echo "hi"
                sh 'id'
            }
        }
    }
}

I created this my-maven-aws-cli image using the following Dockerfile:

# Base Maven image
FROM maven:3.9.9-eclipse-temurin-21

USER root

# Create a jenkins user and group
ARG USER_ID=1001
ARG GROUP_ID=1001


RUN groupadd -g $GROUP_ID jenkins \
    && useradd -m -u $USER_ID -g jenkins jenkins

# Install dependencies for AWS CLI
RUN apt-get update && \
    apt-get install -y unzip curl ca-certificates  && \
    rm -rf /var/lib/apt/lists/*

# Install AWS CLI v2
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip" && \
    unzip /tmp/awscliv2.zip -d /tmp && \
    /tmp/aws/install && \
    rm -rf /tmp/awscliv2.zip /tmp/aws


# Install Docker CLI using your commands
RUN mkdir -p /etc/apt/keyrings && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && \
    chmod a+r /etc/apt/keyrings/docker.asc && \
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null && \
    apt-get update && \
    apt-get install -y docker-ce-cli && \
    rm -rf /var/lib/apt/lists/*



If I change args to args "-u root", then it starts working. Why it is not executing with jenkins user.

Thanks

I think that you need USER jenkins as the last statement in the Dockerfile. A Docker blog post says:

The USER instruction in a Dockerfile is a fundamental tool that determines which user will execute commands both during the image build process and when running the container.

Since you never declared that the container should run as the USER jenkins, it runs as the USER root.

1 Like

Thank you so much. I was stuck on it for 2 days.