Docker Project for DevOps Engineers.

Docker Project for DevOps Engineers.

Dockerfile

  • Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.

  • A Dockerfile is like a set of instructions for making a container. 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.

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.

Task

  1. 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.

    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

  2. Build the image using the Dockerfile and run the container

    Now build the image by using the command docker build , -t option for tag & '.' represents the current path.

    Then run the container by using the command docker run , -d option to run this container in the background, -p for port exposure.

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

    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.

    Then open <public_IPv4_of_instance>:8000 at any web browser.

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

    Here use command docker login to login Docker Hub with credentials.

    Then use the command docker tag <image_name> <username/image_name:tag>

    Finally, push the image to a public or private repository by the command docker push <username/image_name:tag>

    Thus we could see the pushed image from the instance to DockerHub

Reference: Video


Thanks for reading my blog!

Also thanks for valuable time.

Keep learning, keep upskilling.