You are on page 1of 34

16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Node.js improved error handling

Continuous delivery of react app


with Jenkins and Docker
eniy February 18th 2018  TWEET THIS
trov
heniybystrov
It’s the second part of article — React app from scratch. I’ll show you
how to create a CD process to make your app better from scratch.

Before we start I’ll put useful links.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 1/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

First part of article:

React app from scratch


It’s a rst part of tutorial where I’m going to show how to create
react app from scratch.
medium.com

Other my article — how to start using Docker:

Making right things using Docker


In this article I want to show how to use docker for development
and testing. To show that now is time to switch from…
hackernoon.com

If your project is open source — you can use Travis CI. You just need to
create .travis.yml like this:

language: node_js
node_js:
- "8"
- "9"
script:
- npm test

And see result:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 2/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

But I want to show you how to create CI/CD environment from scratch.

We will use Jenkins with Docker. Jenkins has of cial image on docker
hub.

All con gs you can nd on github: https://github.com/evheniy/react-


app/tree/master/jenkins

Jenkins

To run Jenkins using of cial image from docker hub we need to run next
command:

docker run --name jenkins -p 8080:8080 jenkins

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 3/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

But we need to store our data if we need to update image or restart


container. So we need to map volume to host machine:

docker run -p 8080:8080 -v $PWD/jenkins:/var/jenkins_home jenkins

We will use docker not only for running Jenkins. With docker we can run
docker registry and we can test and create images for our react app.

Registry

To run docker registry using docker image on docker hub run:

docker run -d -p 5000:5000 --restart always --name registry registry

To push a new image to registry use next commands:

docker pull ubuntu


docker tag ubuntu localhost:5000/ubuntu
docker push localhost:5000/ubuntu

Docker compose

To run registry with Jenkins I’ll use docker-compose.

But before I’ll create Docker le for Jenkins. We need it for running
docker inside docker.

touch Dockerfile

And put:

FROM jenkins/jenkins:lts

USER root

Here I use latest image of Jenkins and run it as root.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 4/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Now we are ready to use docker-compose.

Let’s create docker-compose le for running Jenkins and docker


repository in one command:

touch docker-compose.yml

And one more. As we need to run docker inside docker we need to add
more volumes. But rst run command:

which docker

And put next code:

version: '3'

services:

jenkins:
build: .
container_name: jenkins
privileged: true
restart: always
ports:
- 8080:8080
volumes:
- ./jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/local/bin/docker:/usr/bin/docker

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 5/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

registry:
image: registry
container_name: registry
restart: always
ports:
- 5000:5000

To run it use

docker-compose up -d

And to stop:

docker-compose stop

To bring everything down (with volumes):

docker-compose down --volumes

Or if you want to remove docker images:

docker-compose down --rmi all

So let’s run it and con gure Jenkins to use pipeline.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 6/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Open http://localhost:8000/:

To see the password just run (we need it only once):

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

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 7/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

And enter it on page:

And as it’s our rst run we see con guration page:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 8/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

I choose the second point and check next plugins:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 9/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 10/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 11/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Click install and wait:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 12/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

If you check directory we have a lot of Jenkins les:

After you need to create a new user:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 13/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

And that’s all. Jenkins is ready.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 14/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Next we need to create a new build. You can make it from start page:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 15/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

On rst step we need to enter name and choose type of our build
con gurations:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 16/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

I use multibranch pipeline con guration.

Next we need to con gure our build (name, access to github)

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 17/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

And some other con gs like cleaning and scanning time.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 18/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

As I made it for react-app I use the same github repo


(https://github.com/evheniy/react-app)

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 19/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

And after Jenkins scans it

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 20/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Con gs

Next step is creating Jenkins le, Docker le, Docker le.test and save it
on github:

node {
try {
stage('Checkout') {
checkout scm
}
stage('Environment') {
sh 'git --version'
echo "Branch: ${env.BRANCH_NAME}"
sh 'docker -v'
sh 'printenv'
}
stage('Build Docker test'){
sh 'docker build -t react-test -f Dockerfile.test --no-cache .'
}
stage('Docker test'){
sh 'docker run --rm react-test'
}
stage('Clean Docker test'){
sh 'docker rmi react-test'
}
stage('Deploy'){
if(env.BRANCH_NAME == 'master'){
sh 'docker build -t react-app --no-cache .'
sh 'docker tag react-app localhost:5000/react-app'
https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 21/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

sh 'docker push localhost:5000/react-app'


sh 'docker rmi -f react-app localhost:5000/react-app'
}
}
}
catch (err) {
throw err
}
}

Docker le

# Extending image
FROM node:carbon

RUN apt-get update


RUN apt-get upgrade -y
RUN apt-get -y install autoconf automake libtool nasm make pkg-config git apt-utils

# Create app directory


RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Versions
RUN npm -v
RUN node -v

# Install app dependencies


COPY package.json /usr/src/app/
COPY package-lock.json /usr/src/app/

RUN npm install

# Bundle app source


COPY . /usr/src/app

# Port to listener
EXPOSE 3000

# Environment variables
ENV NODE_ENV production
ENV PORT 3000
ENV PUBLIC_PATH "/"

RUN npm run start:build

# Main command
CMD [ "npm", "run", "start:server" ]

Docker le.test

# Extending image
FROM node:carbon

RUN apt-get update


RUN apt-get upgrade -y
RUN apt-get -y install autoconf automake libtool nasm make pkg-config git apt-utils

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 22/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

# Create app directory


RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Versions
RUN npm -v
RUN node -v

# Install app dependencies


COPY package.json /usr/src/app/
COPY package-lock.json /usr/src/app/

RUN npm install

# Bundle app source


COPY . /usr/src/app

# Environment variables
ENV NODE_ENV test

# Main command
CMD [ "npm", "test" ]

And after rescanning Jenkins checks it:

And logs:

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 23/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 24/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

We see a lot of interesting information.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 25/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 26/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Now we can check that our image stored in our registry. Run command:
https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 27/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

docker pull localhost:5000/react-app

Now you can run this image on production server.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 28/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

In this article I created docker compose le for running Jenkins and


Docker registry. I created Jenkins le and Docker le for testing and
releasing our app.

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 29/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

In next articles I’ll show how to use SSR, modular structure, webpack
optimization, chaos monkey, testing and code coverage…

# Docker # React # Jenkins # Continuous Integration

# Continuous Delivery

Continue the discussion 

More by Evheniy Bystrov

First steps in react

Evheniy Bystrov

# React

Full stack react app from scratch — Part 1

Evheniy Bystrov

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 30/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

# Javascript

Making right things using Docker

Evheniy Bystrov
Oct 23

# Docker

Open source node.js package from scratch

Evheniy Bystrov
Oct 18

# Javascript

React app from scratch

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 31/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Evheniy Bystrov

# React

Hackernoon Newsle er curates great stories by real


tech professionals
Get solid gold sent to your inbox. Every week!

Email Address *

First Name Last Name

TOPICS OF INTEREST

So ware Development Blockchain Crypto

General Tech Best of Hacker Noon

Get great stories by email

More Related Stories

1 min* to run a service in Kubernetes — kapp tool

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 32/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

Peter Jausovec
May 02

# Docker

10 React JS Articles Every Web Developer Should Read

javinpaul
Oct 05

# React

An Interview With CircleCI CTO Rob Zuber

Chris Chinchilla

# Continuous Integration

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 33/34
16/01/2020 Continuous delivery of react app with Jenkins and Docker - By

6 Patterns for Continuous Delivery with Serverless Applications

Marko Anastasov
Sep 10

# Serverless

5 ways to deploy PHP applications

Buddy
Jul 06

# Docker

https://hackernoon.com/continuous-delivery-of-react-app-with-jenkins-and-docker-8a1ae1511b86 34/34

You might also like