To run Jenkins in Docker, use the following production-ready docker-compose.yml file. This configuration includes data persistence and mounts the Docker socket so Jenkins can build and run other Docker containers inside your pipelines.

1. The docker-compose.yml File

services: jenkins: image: jenkins/jenkins:lts container_name: jenkins restart: unless-stopped privileged: true user: root ports: - "8181:8080" # Jenkins Web UI - "50000:50000" # Jenkins inbound agents port volumes: - ./jenkins_data:/var/jenkins_home - /var/run/docker.sock:/var/run/docker.sock 

2. How to Start Jenkins

  1. Create a directory for your project and navigate into it:

    mkdir jenkins-docker && cd jenkins-docker 
  2. Save the configuration above into a file named docker-compose.yml.

  3. Start the container in the background:

    docker compose up -d 

3. Retrieve the Initial Admin Password

Once the container starts, Jenkins will generate a one-time setup password. Run this command to read it directly from the container:

docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword 

Copy the printed code, open your browser to http://localhost:8181, and paste it in to finish your setup.