Docker Project for DevOps Engineers

Docker Project for DevOps Engineers

Dockerfile:

A Dockerfile is a text file that contains a set of instructions to build a Docker image. It provides a standardized and reproducible way to create containerized applications. 

It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.

Dockerfile components:

Base Image:

The Dockerfile typically starts with a base image, which serves as the starting point for the container. It can be a minimal operating system or a preconfigured image with specific tools and dependencies.

Instructions:

The Dockerfile consists of a series of instructions that define the steps to build the image. Here are some common instructions:


FROM: Specifies the base image.

RUN: Executes commands to install dependencies, configure the environment, or run any necessary setup steps.

COPY or ADD: Copies files from the host machine to the image.

WORKDIR: Sets the working directory for subsequent instructions.

ENV: Sets environment variables.

EXPOSE: Declares the network ports that the container will listen on.

CMD or ENTRYPOINT: Specifies the command to run when the container starts.


TASK:

  • Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
  • Build the image using the Dockerfile and run the container
  • Verify that the application is working as expected by accessing it in a web browser
  • Push the image to a public or private repository (e.g. Docker Hub )


For this project, we will use an ec2 instance where we will first create the NodeJS application. This will require NodeJS installed in the instance.

To install npm:

sudo apt install npm        

Once the app is installed , we will use the command 'npm init -y' to initialize the Node. The output will be like this:

No alt text provided for this image

We will also install any dependencies that will be required for the Node.js application. For example, we will install Express:

npm install express        

Once express is installed, we will create a file named 'index.js' and add the following code to create a basic Express server:

No alt text provided for this image

To check if the application works using the ec2 instance public Ip, run the command, 'node index.js'. The server will start running on port 8080

No alt text provided for this image

Now, in order to containarize the application, we will write a Dockerfile in the same directory:

No alt text provided for this image

We will then create a Docker image using the following command:

sudo docker build -t my-node-app .        

Since we did not give it any tag, it will automatically assign the tag 'latest' to it.

We can see the created image using the command 'docker images':

No alt text provided for this image

Lastly, we will run the docker container using the command:

docker run -d -p 8080:8080 my-node-app        

The docker container will run the application and can be accessed on the port 8080.


Pushing the image to Dockerhub:

To push the docker images to the Dockerhub we need to have a Dockerhub account. Once the account is created, we can use below commands to push the image to Dockerhub:

docker login        

Once the username and password are filled, we will be logged into the Dockerhub account.

Tagging the Docker image with Docker Hub username and the desired repository name:

docker tag my-node-app:latest your-dockerhub-username/my-node-app:latest        

Pushing the tagged image to Docker Hub using the following command:

docker push your-dockerhub-username/my-node-app:latest        

Once the image is pushed to the Dockerhub, we can see it as below:

No alt text provided for this image

This pushed image can we pulled again and can be run as per requirement. 


Thank you for reading!




 










To view or add a comment, sign in

More articles by Shubham Deshpande

  • Docker Important Interview Questions

    Docker is a good topic to ask in DevOps Engineer Interviews, mostly for freshers. One must surely try these questions…

  • Docker- Volumes and Networking

    Docker-Volume Docker volumes are a mechanism for persistently storing data generated or used by Docker containers. They…

  • Docker-compose

    ||Docker Compose|| Docker Compose is a tool that allows us to define and manage multi-container Docker applications. It…

  • Docker for DevOps Engineers

    Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications…

  • Python Libraries for DevOps

    ||Python Libraries for DevOps|| Parsing files in Python refers to the process of reading and interpreting the contents…

    2 Comments
  • Python Data Types and Data Structures for DevOps

    ||Python Data Types and Data Structures for DevOps|| Data Types: Data types in Python define the type and behavior of…

    1 Comment
  • Starting with Python

    Introduction: Python is a Open source, general purpose, high level, and object-oriented programming language. It was…

  • Advance Git & GitHub for DevOps Engineers: Part-2

    ||Advance Git & GitHub for DevOps Engineers: Part-2|| Git Stash: Git stash is a command that allows you to temporarily…

  • Advanced Git & GitHub for DevOps Engineers

    Git Branching: Git branching is a fundamental feature of the Git version control system that allows for parallel…

  • Deep Dive in Git & GitHub

    What is Git and why is it important? As explained in the previous article, git is a version control system that allows…

Insights from the community

Others also viewed

Explore topics