Day17-Docker Project for DevOps Engineers.

90Days of DevOps Challenge

Day17-Docker Project for DevOps Engineers.

In this blog, I’ll show you how to create a Docker project for a simple web application in Node.js/Python). Docker is a tool that allows you to build, run, and share containerized applications.

Containers are isolated environments that contain everything needed to run an application, such as code, dependencies, libraries, and configuration files. Docker makes it easy to deploy and manage your applications across different platforms and environments.

Dockerfiles are text files that list instructions for the Docker daemon to follow when building a container image. When you execute the docker build command, the lines in your Dockerfile are processed sequentially to assemble your image. You can use a Dockerfile to specify the base image, the files to copy, the commands to run, the ports to expose, and other options for your image

Below is what the workflow will look like:

  • Create a Dockerfile and mention the instructions to create your Docker image

  • Run the docker build command which will build a docker image

  • Now the docker image is ready to be used, use the docker RUN command to create containers

Docker file Commands:

FROM: It is used to specify the base image from which created new docker image. It must use this image at the beginning of the docker file.

 Syntax: FROM ubuntu:20.04

RUN: Executes command within the image. This is often used to install packages or set up the environment.

 Syntax:- RUN apt-get update && apt-get install -y python3

COPY & ADD: Copy files and directories from the host system into the image.

 Syntax: COPY ./app /app
ADD ./app.tar.gz /app

WORKDIR: Sets the working directory for subsequent instructions in the dockerfile.

 Syntax: WORKDIR /app

ENV: sets the environment variable inside the container

 Syntax: ENV MY_VARIABLE=my_value

EXPOSE: Inform docker that the container listens on specific network ports at runtime.

 Syntax: EXPOSE 80

CMD : Similar function as a RUN command, but it gets executed only after the container is instantiated.

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

ENTRYPOINT: It targets your default application in the image when the container is created.

 Syntax: 
ENTRYPOINT ["java", "-jar", "myapp.jar"]

VOLUME: Creates a mount point for attaching external volumes to the container.

 Syntax: VOLUME /data

USER: Sets the user that the container should run as.

  Syntax: USER myuser

ARG: ARG is used to define the variable dynamically, that you change during image build by passing an argument with the docker build command.

 Syntax: ARG APP_VERSION=1.0

TASKS:

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

  1. Now let's create a Dockerfile for our Project app
-------------------Dockerfile-------------------------------
FROM python:3

WORKDIR /data

RUN pip install django==3.2

COPY . .

RUN python manage.py migrate

EXPOSE 8000

CMD ["python","manage.py","runserver","0.0.0.0:8000"]
  1. Build an Image using Dockerfile:

To build an image using a Dockerfile, you can use the “docker build” command.

    1. Run the image to create a container:

To run a container from an image, you can use the “docker run” command.

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 ) To push an image to a public or private repository, you first need to have an account on the repository platform (e.g. Docker Hub) and be logged in.

docker login

Now we need to tag that image before pushing it to the docker hub

docker tag <user-name>/<image-name>:<tag-name>

docker tag chandanahdam/todo:latest

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

you can use the “docker push” command to push your image to a specific repository.

In the Next Article, we will explore more about Docker......

Thank you for giving your precious time to read this blog and if any suggestions or improvements are required on my blogs feel free 💬comment below.

Happy Learning😊🚀