How to run Jenkins from a Docker Container
Installation
Install Docker
Create Directory
- Create a new directory from
Terminalmkdir ~\Docker\Jenkins
Build docker image and run
- Execute the command below from the newly created directory.
docker run
-u root
--rm
-d
-p 8080:8080
-p 50000:50000
-v ~/Docker/Jenkins:/var/jenkins_home
-v /var/run/docker.sock:/var/run/docker.sock
--name jenkins
jenkinsci/blueocean
- Each flag from the above command is described more thoroughly below
-u root- is needed to be able bind docker service with jenkins--rm- is used to remove this container after finishing therun-d- run as daemon, so you can detach console window-p 8080:8080and-p 50000:50000- expose jenkins ports for main Jenkins and slave communication~/Docker/Jenkins- my created directory-v /var/run/docker.sock:/var/run/docker.sock- mount/bid docker sockets--name jenkins- alias for created container - e.g. to be able login docker machine easilyjenkinsci/blueocean- name of docker used to create Jenkins - released every time when new blue ocean is released
- Single line view below
docker run -u root --rm -d -p 8080:8080 -p 50000:50000 -v ~/Docker/Jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock --name jenkins jenkinsci/blueocean
Jenkins configuration
- Open web browser and type address
localhost:8080 - The
/var/jenkins_home/secrets/initialAdminPasswordfile is located under mounted place.- For example if
/var/jenkins_homeis “mapped” to~/Docker/Jenkins, then file is located at~/Docker/Jenkins/secrets/initialAdminPassword
- For example if
Starting Jenkins
Interacting with Container
Running created container
- Execute the command below to run the newly created container.
docker container start jenkins
Listing running container
docker container ls --all
- Executing the command above should yield an output comparable to that below.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42e3edd6087b jenkinsci/blueocean "/sbin/tini -- /usr/…" 12 minutes ago Up 12 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins
Stop container
- Execute the command below to stop the container.
- Note: You can also use Container ID instead alias name
docker container stop jenkins
Remove container
- Execute the command below to remove the container.
- Note: Stopping and removing container is also required to be able rebuild docker (container)
docker container rm jenkins
Remove image
- Execute the command below to remove the image.
docker image rm jenkinsci/blueocean
Bulk removing images and containers:
#!/bin/bash
# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)



