Thursday, 17 February 2022

Multistage Dockerfile- Dockerize an Angular Application with Nginx

Multistage builds make use of one Dockerfile with multiple FROM instructions. Each of these FROM instructions is a new build stage that can COPY artifacts from the previous stages. By going and copying the build artifact from the build stage, you eliminate all the intermediate steps such as downloading of code, installing dependencies, and testing. All these steps create additional layers, and you want to eliminate them from the final image.

The build stage is named by appending AS name-of-build to the FROM instruction. The name of the build stage can be used in a subsequent FROM and COPY command by providing a convenient way to identify the source layer for files brought into the image build. The final image is produced from the last stage executed in the Dockerfile.

Try taking the example from the previous section that used more than one Dockerfile for the React application and replacing the solution with one file that uses a multistage build.

Dockerfile
FROM node:12.13.0-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx
EXPOSE 3000
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/build /usr/share/nginx/html

This Dockerfile has two FROM commands, with each one constituting a distinct build stage. These distinct commands are numbered internally, stage 0 and stage 1 respectively. However, stage 0 is given a friendly alias of build. This stage builds the application and stores it in the directory specified by the WORKDIR command. The resultant image is over 420 MB in size.

The second stage starts by pulling the official Nginx image from Docker Hub. It then copies the updated virtual server configuration to replace the default Nginx configuration. Then the COPY –from command is used to copy only the production-related application code from the image built by the previous stage. The final image is approximately 127 MB.

Case Study Lab: Dockerize an Angular Application with Ngnix




we will go through a step-by-step guide on how to write a multi-stage dockerfile to build an angular application using Docker and host the production-ready code in an NGINX container. We will also walk through some of the docker commands used to build, run and monitor the status of containers.

Creating an Angular Application:

In order to proceed with this step, ensure that you have Node.js and Angular CLI installed in your ec2 instance. Installation instructions can be found below

Step 1 – Install Node.js

First of all, you need to install node.js on your system. If you don’t have node.js installed use the following set of commands to add node.js PPA in your Ubuntu system and install it.

sudo apt update
sudo apt upgrade
sudo apt install python-software-properties
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs

Make sure you have successfully installed node.js and NPM on your system

node --version
npm --version
npm install n -g
n stable
n latest


Step 2 – Install Angular/CLI

After installation of node.js and npm on your system, use following commands to install Angular cli tool on your system.

npm install -g @angular/cli

Once the prerequisites are installed, you can start by executing the following commands to create an angular application in a local directory of your preference.

ng new sample-angular-app
view rawgistfile1.txt hosted with ❤ by GitHub

Navigate to the project’s directory, to run the application.

cd sample-angular-app
ng serve --host 0.0.0.0 --port 8001
view rawgistfile1.txt hosted with ❤ by GitHub
This image has an empty alt attribute; its file name is image-20210126-174551-1024x552.png

Go to your ipaddress:8001 . Make sure port 8001 is open in your security group

Now that we have an angular application successfully running, let’s start writing a dockerfile for it.

Writing a Dockerfile

Below is the dockerfile snippet we will use to dockerize our angular application with a NGINX server. The dockerfile comprises of a multi-stage docker build, which is divided into the following stages:

  1. Building the angular source code into production ready output
  2. Serving the application using a NGINX web server
# Stage 1: Compile and Build angular codebase
# Use official node image as the base image
FROM node:latest as build
# Set the working directory
WORKDIR /usr/local/app
# Add the source code to app
COPY ./ /usr/local/app/
# Install all the dependencies
RUN npm install
# Generate the build of the application
RUN npm run build
# Stage 2: Serve app with nginx server
# Use official nginx image as the base image
FROM nginx:latest
# Copy the build output to replace the default nginx contents.
COPY --from=build /usr/local/app/dist/sample-angular-app /usr/share/nginx/html
# Expose port 80
EXPOSE 80
view rawgistfile1.txt hosted with ❤ by GitHub

Stage 1:

  • FROM – Initializes a new build stage, and sets the latest node image from DockerHub registry as the base image for executing subsequent instructions relevant to the angular app’s configuration. The stage is arbitrarily named as build, to reference this stage in the nginx configuration stage.
  • WORKDIR – Sets the default working directory in which the subsequent instructions are executed. The directory is created, if the path is not found. In the above snippet, an arbitrary path of usr/local/app is chosen as the directory to move the angular source code into.
  • COPY – Copies the source files from the project’s root directory on the host machine to the specified working directory’s path on the container’s filesystem.
  • RUN – Executes the angular build in a new layer on top of the base node image. After this instruction is executed, the build output is stored under usr/local/app/dist/sample-angular-app and the compiled image will be used for the subsequent steps in the Dockerfile.

Stage 2:

  • FROM – Initializes a secondary build stage, and sets the latest nginx image from dockerhub registry as the base image for executing subsequent instructions relevant to nginx configuration.
  • COPY – Copies the build output generated in stage 1 (--from=build) to replace the default nginx contents.
  • EXPOSE – Informs Docker that the nginx container listens on network port 80 at runtime. By default, the nginx server runs on port 80, hence we are exposing that specific port.

Running the Docker Container

In order to build and run the docker container, open up a command prompt and navigate to the location of your Dockerfile in your project’s directory.

Execute the following command to build the docker image.

docker build -t devopstreams/sample-angular-app-image:latest .
view rawgistfile1.txt hosted with ❤ by GitHub

The file path . defines the location of the Dockerfile in the current directory, and the -t argument tags the resulting image, where the repository name is krish186/sample-angular-app-image and the tag is latest.

After the build is successfully finished, we can check to see if it appears in the list of docker images available locally. To do so, we can execute the below command.

docker image ls
view rawgistfile1.txt hosted with ❤ by GitHub

Output:

REPOSITORY TAG IMAGE ID CREATED SIZE
devopstreams/sample-angular-app-image latest c1789404fda5 19 seconds ago 138MB
view rawgistfile1.txt hosted with ❤ by GitHub

Now that we see our container is in the list, we can run the image using following command.

docker run -d -p 8080:80 devopstreams/sample-angular-app-image:latest
view rawgistfile1.txt hosted with ❤ by GitHub

The -d option, causes Docker to detach the container and have it run in the background. The -p argument establishes a port mapping, which defines that port 80 of the docker container (as specified in dockerfile), should be exposed to port 8080 of our host machine.

To check the details of our running container, type in the following command:

docker ps
view rawgistfile1.txt hosted with ❤ by GitHub

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12d6293d6e19 devopstreams/sample-angular-app-image:latest "/docker-entrypoint.…" 16 seconds ago Up 14 seconds 0.0.0.0:8080->80/tcp keen_khayyam
view rawgistfile1.txt hosted with ❤ by GitHub

As per the above output, we see that the container is up and running. If we now head to ipaddress:8080/ we can see the angular application is successfully dockerized.

Now that the application is running as expected, our next step would be to push our image to an image repository, to deploy our containers to a cloud service.

If you have a DockerHub account you can execute the following commands:

docker login -u <username> -p <password>
docker push devopstreams/sample-angular-app-image:latest
view rawgistfile1.txt hosted with ❤ by GitHub

Your image should now be pushed successfully to the Dockerhub registry.

How to Connect to a running container- Unlock Jenkins Password in Docker

 Prerequisite:

Make sure you complete this lab: https:/www.devopstreams.com/2022/02/how-to-pull-images-from-docker.html before attempting this


You can connect to a running container just like you can connect to a server and then run commands in that container. To do that you have to make use of the docker exec command

Step 1: Create a Container from a Jenkins image. see https:/www.devopstreams.com/2022/02/how-to-pull-images-from-docker.html

Step 2: Lunch another terminal on your docker host( Separate from the one you used on step1)

Step 3. Connect to your running container 

docker exec -it container_id sh--------->replace ur container id in red

docker exec -it 65357f31b2bc sh



This will connect you to the shell of the container.
Step 3: Unlock Jenkins : Now you are connected to the container shell, you can access the path of the Jenkin initial password



Step 4: Unlock Jenkins in the UI





Tuesday, 15 February 2022

How to Pull images from a Docker Repository and Run a container- Install jenkins using docker

 You can pull images from a Docker Repository by first looking for the image in a Docker repository eg Docker Hub, Then copy the image tag and run docker pull with the tag name

Step 1. Find the Image(Eg Jenkins)


Step 2.  Use the image name to pull the image


Here the official image name is : jenkins/jenkins:lts

Step 3. On your Docker host, Pull the image using docker pull command
docker pull jenkins/jenkins:lts

Step 4: View your image
docker images


Step 5: Run your image to get your container using the image name or image id

docker run -it -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts  Or

docker run -it -p 8080:8080 -p 50000:50000 image_id



Your Jenkins container is now running on Port 8080, You can access it by going to your Public ip:8080 on ur browser



Thursday, 2 December 2021

Troubleshooting, Tips and Tricks

 

1. How to downgrade/upgrade Jenkins

To Downgrade/upgrade Jenkins on ubuntu from eg version 2.320 to version 2.318 use the below command


sudo apt install jenkins=2.318

sudo systemctl restart jenkins



2. My Instance is Slow after installing Sonarqube

a. increase the storage volume 

b. upgrade your instace type from t2micro to like t2medium

c. try this steps to Extend the OS file

     i. log into the instance and enter the below commands:

      sudo growpart /dev/xvda 1

      sudo resize2fs /dev/xvda1

source:https://blog.shikisoft.com/increase-root-volume-of-ebs-backed-ec2/


3. How to change ansible Tower password(If you forgot)

Log into the Tower Console

Enter the following commands

docker ps
docker exec -it awx_web bash
awx-manage changepassword admin

http://vcloud-lab.com/entries/devops/reset-ansible-awx-tower-admin-password

4. Sonarqube is under maintenance




    1. Now browse http://publicip:9000/setup to upgrade your database
    2. After upgrade the database, browse your sonarqube server

     

  • 5.  Your jenkins pipeline couldnt find a program, eg. kubectl: like below

  • + kubectl get nodes
    /var/lib/jenkins/workspace/test@tmp/durable-fd334313/script.sh: line 1: kubectl: command not found

Login to the machine and copy the program into /usr/local/bin
cp kubectl /usr/local/bin



How to upgrade Maven

  java.lang.IllegalStateException I had installed maven in my ubuntu using command  apt install maven This installed maven in path /usr/shar...