|
| 1 | +Step 1: Spin up a VM, and install docker on it,Make sure the docker service is up and running. |
| 2 | +Step 2: Log in to the server and open the docker service file /lib/systemd/system/docker.service. |
| 3 | +Search for ExecStart and replace that line with the following: |
| 4 | +ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock |
| 5 | + |
| 6 | +Step 3: Reload and restart docker service. |
| 7 | +sudo systemctl daemon-reload |
| 8 | +sudo service docker restart |
| 9 | + |
| 10 | +Step 4: Validate API by executing the following curl commands. Replace 54.221.134.7 with your host IP. |
| 11 | + |
| 12 | +curl http://localhost:4243/version |
| 13 | +curl http://54.221.134.7:4243/version |
| 14 | + |
| 15 | +Once you enabled and tested the API, you can now start building the docker slave image. |
| 16 | + |
| 17 | +Create a Jenkins Agent Docker Image : you can also use this dockerfile vailable at url: |
| 18 | +************************************* |
| 19 | +If create your own image than make sure, your image should contain the following minimum configurations to act as a slave. |
| 20 | +> sshd service running on port 22. |
| 21 | +> Jenkins user with password. |
| 22 | +> All the required application dependencies for the build. For example, for a java maven project, you need to have git, java, and maven installed on the image. |
| 23 | + |
| 24 | +Make sure the sshd service is running and can be logged into the containers using a username and password. Otherwise, Jenkins will not be able to start the build process. |
| 25 | + |
| 26 | +Note: The default ssh username is jenkins and the password is also jenkins as per the given Dockerfile. You will have to use these credentials in the below configuration. |
| 27 | + |
| 28 | +Configure Jenkins Server With Docker Plugin |
| 29 | +********************************** |
| 30 | + |
| 31 | +Step 1: Head over to Jenkins Dashboard –> Manage Jenkins –> Manage Plugins. |
| 32 | +Step 2: Under the Available tab, search for “Docker” and install the docker cloud plugin and restart Jenkins. |
| 33 | +Step 3: Once installed, head over to Jenkins Dashboard –> Manage Jenkins –>Manage Jenkins –> Manage Nodes and Clouds |
| 34 | +Step 4: Select cloud” and fill out the docker host parameters for spinning up the slaves. |
| 35 | +Step 5: Under docker, you need to fill out the details like: |
| 36 | +Replace “Docker URI” with your docker host IP. For example, tcp://10.128.0.3:4243 |
| 37 | +You can use the “Test connection” to test if Jenkins is able to connect to the Docker host. |
| 38 | + |
| 39 | +Step 6: Now, from “Docker Agent Template” dropdown, click the “Add Docker template” and fill in the details |
| 40 | + |
| 41 | +Labels – Identification for the docker host. It will be used in the Job configuration. Here we use java-docker-slave |
| 42 | +Name: Name of the docker template. Here we use the same name as label ie, java-docker-slave |
| 43 | +Docker Image – the image that you created for the slave. |
| 44 | +Remote Filing System Root – Home folder for the user you have created. In our case, it’s /home/jenkins |
| 45 | +Credentials – click add and enter the SSH username and password that you have created for the docker image. |
| 46 | +If you are using my Docker image, the user will be jenkins & password is also jenkins. |
| 47 | +By default, the workspace will not be persisted in the host. However, |
| 48 | +if you want the workspace to be persistent, add a host volume path under container settings. |
| 49 | +For example, if you want the workspace to be available at /home/ubuntu, you can add the volume path as shown below. |
| 50 | +/home/jenkins is the path inside the container. |
| 51 | + |
| 52 | +/home/ubuntu:/home/jenkins |
| 53 | + |
| 54 | +Towards the right of the Volumes option, if you click the question mark, it will show you additional volume options as shown below. |
| 55 | + |
| 56 | +Test Jenkins Build Inside a Docker container |
| 57 | +****************************** |
| 58 | + |
| 59 | +Now that you have the slave configurations ready, we will test the docker agent plugin using a freestyle job. |
| 60 | + |
| 61 | +Create a freestyle job, select “Restrict where this project can be run” option and select the docker host as a slave using the label. |
| 62 | + |
| 63 | +Add a shell build step which echoes a simple “Hello World“ |
| 64 | + |
| 65 | +If you have done all the configurations right, Jenkins will spin up a container, builds the project, and destroys the container once the build is done. |
| 66 | + |
| 67 | +First, you will see a pending notification as Jenkins tries to deploy a container on run time and establishes an SSH connection. After a few seconds, your job will start building. |
| 68 | + |
| 69 | +You can check the build logs in your jobs console output as well. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
0 commit comments