Docker Project Using Dockerfile [Day 17 Task]

Docker Project Using Dockerfile [Day 17 Task]

1. What Is Dockerfile?

As we know docker image is a read-only file or a template with a set of instructions that is used to create a container. Usually, we used to create a docker image from a file that's called a Dockerfile.

So, In simple terms, Dockerfile is a simple text file that consists of instructions to build Docker images.

While creating a docker file letter 'D' should be a Capital letter.

Like this : vim Dockerfile / nano Dockerfile

2. Dockerfile Commands

Dockerfile follows a set of commands to build an image. All commands will be in capital form.

  1. FROM: Defines a base image, it can be pulled from the docker hub
    (for example- if we want to create a javascript application with the node as the backend then we need to have node as a base image, so it can run node application). This will be the first command in a docker file always.

    eg: FROM ubuntu:20.4

  2. RUN: RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image.

    eg: RUN apt-get install tree -y

  3. COPY: It is used to copy your local files/directories to Docker Container.

    eg: COPY . .

    First . [dot] is used as a source which means if there are some files present in your current working directory they will be copied into the docker file i.e. second . [dot]

  4. WORKDIR: Defines the working directory for subsequent instructions in the Dockerfile(Important point to remember that it doesn't create a new intermediate layer in Image).

    eg: WORKDIR /app

    This means after creating the container working directory will be /app.

  5. EXPOSE Documents in which ports are exposed (It is only used for documentation).

    eg: EXPOSE 8000

    If we want to access our application from outside for that we need to expose our container port here i.e 800 [suppose].

  6. CMD: Command to be executed when running a container( It is asked to have one CMD command, If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.

    eg: CMD ["python", "hello.py"]

    Whatever command you want to run just keep it inside of this [] bracket as per the example.

  7. ENTRYPOINT: This is where the ENTRYPOINT instruction shines. The ENTRYPOINT instruction works very similarly to CMD in that it is used to specify the command executed when the container is started. However, where it differs is that ENTRYPOINT doesn't allow you to override the command.

    eg: ENTRYPOINT ["echo","hello world"]

  8. ENV - Sets environment variables inside the image.

    eg: ENV env_name xyz

There are a few more commands like VOLUME, ADD etc...but these are some important commands to write a docker file.

3. Docker Image Layers Architecture:

Docker images consist of many layers. Each layer is built on top of another layer to form a series of intermediate images. This arrangement ensures that each layer depends on the layer immediately below it. The way layers are placed in a hierarchy is very significant. It allows you to place the layers that frequently change high up the hierarchy so that you manage the Docker image’s lifecycle efficiently.

Changes made to a Docker image layer trigger Docker to rebuild that particular layer and all other layers built from it. Making changes to a layer at the top of the stack ensures the rebuilding of the entire image using fewer computational resources. This means you should keep the layers with the least or no changes at the bottom of the hierarchy formed.

4. 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 )

Here I am creating a Node.js dockerfile .

step1: git clone github.com/messaoudimaher/Docker-Task-Day17.., cloned the repo from GitHub.Then go inside this repo and checked all the files to create a web application using node image.

Here you can use also the "ls" command.

Step 2: Create a docker file.

FROM node:12, is the base image that will be pulled from the docker hub.

COPY . . , all the files from the current location to the docker file.

RUN npm install, if we want to execute a node application then it will require an npm package.

EXPOSE 8000, exposed a container port that is 8000 to access the application.

CMD ["npm", "start"], this command will be executed after the creation of the container.

Step 3: build the docker file to create the docker image

I changed the image name because I wrote the wrong docker hub username.

To run a docker file use the command as docker build -t image name [mahermesaoudi/node-todo:latest1] . [dot is used as the current location where the docker file is present and it is mandatory to put .]

[mahermesaoudi is my docker hub username to tag that image as will be pushed the image to the docker hub later ]. Images are created in a layer form successfully.

Step 4:

check if the image is created or not by the command docker images

Step 5: Create the container by running the docker image

Now create a container from the image by the command :

docker run -d -p 8000:8000 image-id

[-d is detached mode and -p is for port mapping to the host to container ].

Step 6:

Step 7:

First, allow port 8000 in the AWS ec2 server in the inbound rule to access the application from the internet [instance public-key:8000].

Now this is the application.

ThankYou For Reading :)