Skip to main content

Command Palette

Search for a command to run...

Docker Project for DevOps Engineers

Day 17 : #90DaysOfDevOps Challenge

Updated
β€’5 min readβ€’View as Markdown
Docker Project for DevOps Engineers

What is Dockerfile

A Dockerfile is a text file that contains a set of instructions for building a Docker image. The Dockerfile describes how to create a custom Docker image that includes all the necessary dependencies and configurations for running an application. The Dockerfile contains commands that are executed in sequence to build the image. Each command in the Dockerfile creates a new layer in the image.

Dockerfile is used as the source code to build Docker images, and it is highly configurable, allowing developers to customize the environment for their application. Dockerfile is typically used with the docker build command, which takes the Dockerfile as input and produces a Docker image as output. Once the image is built, it can be used to start containers that run the application.

Dockerfiles are commonly used to define and automate the build process for complex applications that require specific dependencies, configuration, and environment variables. They are a powerful tool for building consistent and reproducible Docker images that can be easily shared and deployed across different environments.

Let's talk about what we insert in Dockerfile

  1. FROM: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.

  2. RUN: Executes a command in the image. This command is run during the image build process. For example, you might use the RUN command to install necessary packages or dependencies for your application.

  3. COPY: Copies files from the host machine to the image. For example, you might use the COPY command to copy the files for your application into the image.

  4. ENV: Sets an environment variable in the image. For example, you might use the ENV command to set a variable that holds the version of your application.

  5. EXPOSE: Specifies the ports that should be exposed on the container. For example, you might use the EXPOSE command to specify that port 8000 should be exposed on the container.

  6. CMD: Specifies the command that should be run when a container is created from the image. For example, you might use the CMD command to specify that your application should be started when the container is created.

Here’s an example of a simple Dockerfile that sets up a Node.js environment:

  • FROM creates a layer from the node:12.2.0-alpine Docker image

  • WORKDIR defines the working directory of a Docker container

  • COPY adds files from the host machine to the image

  • RUN installs necessary packages or dependencies like npm for the application

  • EXPOSE specifies the port 8000 that should be exposed on the container

  • CMD specifies what command to run within the container

  • Project:- Node Todo App

nystudio107 | Running Node.js in Docker for local development

Task: Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

Clone the repository in your system using the git clone command, view the content of the repository & create a Dockerfile if not present or modify the present Dockerfile.

Step 1:- We can find the source code from the below GitHub location

https://github.com/bharatdevops-bn4/node-todo-cicd.git

Step 2:- Firstly need to create a separate directory to create a project so it will very easy to track all the changes we need to clone the repo by using the below command

git clone <Repo name>

Step 3:- Now we need to create Dockerfile to write the set of instructions to run the code

NOTE:- Here we have already a docker file that case no need to create a again

FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]

Step 4:- Now using the Docker file we can create an Docker image

docker build . -t <image_name>
# Ex:- sudo docker build . -t node_todo_app

Step 5:- Now using the above docker image we can create a n number of docker containers

docker run -itd -p 8000:8000 node_todo_app

Step 6:- Now we need to search in the web browser along with the ipv4 address and port number

Note: Go to your ec2 instance security group and check whether the port 8000 is opened or not. If not, then add it to the inbound rules of the security group.

Step 7:- Now we need to push that image to the Docker Hub repo so everyone can use that same image. using the below url we can log in to Docker Hub

https://hub.docker.com/

Step 8:- Now we need to log in to the docker hub from the Linux machine using docker hub conditionals

docker login

Step 9:- Now we need to tag that image before pushing it to the docker hub

docker tag node_todo_app bharat3006/node_todo_app

Step 10:- Now we can push that image to the docker hub

docker push bharat3006/node_todo_app

Step 11:- Now we can see the docker image in the docker hub

Congratulations! βœ¨πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰You have successfully installed Docker on your Ubuntu instance, added the current user to the Docker group, created a Docker container for the provided Node-Js demo application. Exposed port 8000 for everyone using IPv4 in the inbound rule of your instance's security group. Also pushed the Docker images to DockerHub.

Thank you for reading!!!
~Bhaarat