Running Docker Inside and Outside of a Jenkins Container, Along With Docker-Compose — a Tiny Pipeline as Code Demonstration

Everything is Code, Versioned and Docker Docker Everywhere!

Here is a graphical walk-through demonstration of PapalUdwig’s Docker Lessons. This assumes that you already have Docker installed.

This is a tiny example of how everything can be code, stored and versioned from a SCM like GitHub. Even the pipeline definition comes from GitHub, Jenkins job definition is an empty shell pointing the GitHub repo. Any changes needed can easily accomplished in the GitHub repo itself, without having to touch Jenkins.

Image for post
Image for post

Step 1: Launch Jenkins Docker Container

The Dockerfile I have used to push the customized Jenkins image looks like this. All I am doing here is to install Docker and Docker Compose. Though installing Docker client is not needed as you will see later in the below steps.

Make a folder called jenkins_home on your host machine and run the below command. You will notice that the Jenkins home folder, Docker client and Docker Socket are mapped so that docker commands running within the Jenkins (job) can actually talk to the host Docker Machine.

Let us look at the Docker container logs and wait till the Jenkins has finished its start up process.

There is not need to get inside the container to see the password since the host folder has been mapped to jenkins_home.

Browse http://localhost:8880 and paste the password.

Image for post
Image for post

Click on the Install Suggested Plugins button.

Image for post
Image for post

Wait till all the recommended plugins have been installed.

Image for post
Image for post
Image for post
Image for post

Create the admin user.

Image for post
Image for post

Save and finish.

Image for post
Image for post

Click Start using Jenkins!

Image for post
Image for post

In case you logout, this is how the login screen will look like.

Image for post
Image for post

This is Jenkins Dashboard!

Image for post
Image for post

Here is the running container using docker ps

Step 2: A Look Inside the GitHub Repository that Has Everything

Papaludwig’s repo has these files and folders, let us look at the interesting one’s for us.

This is the Dockerfile that will be used to build the mobycounter image.

Here is the docker-compose.yml file used in the build stage to deploy the microservices Docker Containers.

Here is the pipeline as code, the Jenkinsfile! It has three stages.

Below is the deploy script used in the deploy stage of our pipeline. In this case it deploys to a single Docker Host, this could very well be replaced by a Configuration Management script to use tools like Ansible, Chef, Puppet or SaltStack. And, the deployment target could very well be a container orchestration tool like Docker Swarm or Kubernetes. That will allow a pipeline job to deploy to hundreds of Docker Containers running on multiple Machines(Nodes) and load balanced.

Step 3 Create a Jenkins Pipeline Job

OK, let us now come back to our Jenkins dashboard and click ‘create a new job”

Image for post
Image for post

Proceed after entering a job name and selecting the Pipeline option.

Image for post
Image for post

Come to the Pipeline section and the repo url.

Image for post
Image for post

That is all there is! Click Build Now and watch the fun.

Image for post
Image for post

Pipeline is starting.

Image for post
Image for post

The Checkout stage

Image for post
Image for post

The build stage.

Image for post
Image for post

In case you an error, it will look like this. I had an npm install disruption first.

Image for post
Image for post

Reached the final deploy stage.

Image for post
Image for post

Successful!

Image for post
Image for post

Nice to see the success logs.

Image for post
Image for post

A full-stage view of the pipeline executions.

Image for post
Image for post

Let us check our docker host and see if there are running containers (from the deploy stage). Yes, we can see two extra containerized services running, a Redis database and a front end Node Application.

Let us browse port 80 which the port used by the front end.

Image for post
Image for post

Click on the screen to paste Docker images!!

Image for post
Image for post

The locations of these images are saved in the Redis database so that the state of the application is persisted across deployments and restarts!

Thank you for your time, do follow for more such tiny illustrations!

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store