Day 17 : Docker Project for DevOps Engineers.

Day 17 : Docker Project for DevOps Engineers.

·

5 min read

Table of contents

Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image. It specifies the base image, the layers to be added to the image, and the commands to be run to create the image.

Docker image

A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. It is read-only and cannot be changed while it is running.

Docker container

A Docker container is a running instance of a Docker image. It is a lightweight, isolated environment that can run on any operating system that has Docker installed. Containers are ephemeral and can be easily destroyed and recreated.

Relation between Dockerfile, Docker image, and Docker container

The following diagram shows the relation between Dockerfile, Docker image, and Docker container:

The Dockerfile is used to build the Docker image. The Docker image is used to create Docker containers. Docker containers are running instances of Docker images.

Example

The following example shows how to use a Dockerfile to build a Docker image for a simple web application:

Dockerfile

FROM python:3.10-alpine

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "app.py"]

This Dockerfile will create an image based on the python:3.10-alpine image, which is a small and efficient Python image. It will then copy your web application's code and package.json file into the image, and install the dependencies. Finally, it will expose port 5000 and start the web application using the python start command.

To build the Docker image, run the following command:

docker build -t my-web-application .

To run the Docker container, run the following command:

docker run -p 8080:8080 my-web-application

This will create a running instance of the web application on port 8080. You can then access the web application in a web browser at http://localhost:8080.

Benefits of using Docker

Docker offers a number of benefits, including:

  • Portability: Docker images are portable and can run on any operating system that has Docker installed. This makes it easy to deploy and run applications in different environments.

  • Efficiency: Docker images are efficient and use less resources than traditional virtual machines. This makes it possible to run more applications on the same hardware.

  • Scalability: Docker images are scalable and can be easily scaled up or down to meet the demands of the application.

  • Reproducibility: Docker images are reproducible and can be built and run in the same way on different machines. This makes it easy to ensure that applications are running in a consistent environment.

Conclusion

Docker is a powerful tool for containerizing and deploying applications. It offers a number of benefits, including portability, efficiency, scalability, and reproducibility.

In addition to the above benefits, Docker also offers a number of other features, such as:

  • Networking: Docker containers can be easily networked together to create complex applications.

  • Storage: Docker containers can use persistent storage volumes to store data that needs to survive container restarts.

  • Security: Docker containers can be isolated from each other and from the host system to improve security.

Docker is a widely used tool in the development and operations community. It is used by companies of all sizes to build, deploy, and manage their applications.


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

Here a simple web application will be a Django to do list

https://github.com/LondheShubham153/node-todo-cicd

At first we need to clone this in a folder on our EC2 instance machine.

To run this in local read the readme file.

  • Build the image using the Dockerfile and run the container

The docker build command builds a Docker image from a Dockerfile and a context. The context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context.

The following command builds a Docker image from the current directory and tags it with the name <image-name>:

docker build . -t <image-name>

The . tells Docker to use the current directory as the context for the build. The -t flag tells Docker to tag the resulting image with the name <image-name>.

Now we need to run this image to create it into a container

docker run -d -p 8000:8000 <image-name>

The docker run command starts a Docker container from an image. The -d flag tells Docker to run the container in detached mode, which means that Docker will not attach a console to the container. The -p flag tells Docker to publish a port from the container to the host machine.

  • Verify that the application is working as expected by accessing it in a web browser

NOTE : The port should be accessible for inbound rules (eg here 8000).Therefore change it in the Inbound rules<--Security Details

  • Push the image to a public or private repository (e.g. Docker Hub )

Log in to Docker Hub:- Open a terminal or command prompt and login to your Docker Hub account using the following command:

docker login

Tag the Image:- Before pushing the image, you need to tag it with the repository name. If you want to push it to Docker Hub, the image name should follow the format username/repository_name:tag. Run the following command to tag your image:

docker tag node-app pmgoriya/pankaj

Push the Image:- Once the image is tagged, you can push it to Docker Hub using the following command

docker push pmgoriya/pankaj


Thankyou again for reading until here.Hope you have learned something new and added some value in your journey to becoming a DevOps engineer.See you in the next one.