Day 17 Task: Docker Project for DevOps 
                                      Engineers.

Day 17 Task: Docker Project for DevOps Engineers.

What is Dockerfile with example..?

A Dockerfile is a text file that contains a set of instructions for building a Docker image. The Dockerfile describes how to create a custom Docker image that includes all the necessary dependencies and configuration for running an application. The Dockerfile contains commands that are executed in sequence to build the image. Each command in the Dockerfile creates a new layer in the image.

Dockerfile is used as the source code to build Docker images, and it is highly configurable, allowing developers to customize the environment for their application. Dockerfile is typically used with the docker build command, which takes the Dockerfile as input and produces a Docker image as output. Once the image is built, it can be used to start containers that run the application.

Dockerfiles are commonly used to define and automate the build process for complex applications that require specific dependencies, configuration, and environment variables. They are a powerful tool for building consistent and reproducible Docker images that can be easily shared and deployed across different environments.

Here’s an example of a simple Dockerfile that sets up a Python environment:

# Use an official Python runtime as a parent image
FROM python:3.9-slim-buster

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

In this example, the Dockerfile starts with a base image of the official Python 3.9 runtime in a slim Debian-based container. It sets the working directory to /app and copies the contents of the current directory into the container. Then, it installs any necessary dependencies specified in a requirements.txt file using pip. Next, it exposes port 80 to the host machine, sets an environment variable, and specifies that the app.py file should be run when the container launches.

Once the Dockerfile is defined, it can be used to build a Docker image using the docker build command. The resulting image can then be used to start containers running the application.

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

Here’s a simple web application in Node.js:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Here is the Dockerfile for a simple web application using Node.js:

# Use an official Node.js runtime as a parent image
FROM node:14-alpine

# Set the working directory to /app
WORKDIR /app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose port 3000
EXPOSE 3000

# Start the application
CMD ["npm", "start"]

This Dockerfile sets up a Node.js environment and installs the application dependencies, copies the rest of the application code to the container, exposes port 3000 for incoming traffic, and runs the npm start command to start the application. You can build an image from this Dockerfile using the docker build command, and then run the image in a container using the docker run command.

02. To build the Docker image from the Dockerfile, follow these steps:

  1. Navigate to the directory where your Dockerfile is located.

  2. Run the following command to build the Docker image:

docker build -t my-node-app

This command tells Docker to build a new image using the Dockerfile in the current directory and tag it with the name “my-node-app”.

3. Once the image has been built, you can run a container using the following command:

docker run -p 3000:3000 my-node-app

4. This command tells Docker to run a container from the “my-node-app” image, map port 3000 on your local machine to port 3000 in the container, and start the application.

You should now be able to access your web application by visiting http://localhost:3000 in your web browser.

02. How to Build the image using the Dockerfile and run the container..?

To build the Docker image from the Dockerfile, follow these steps:

  1. Navigate to the directory where your Dockerfile is located.

  2. Run the following command to build the Docker image:

docker build -t my-node-app .

3. This command tells Docker to build a new image using the Dockerfile in the current directory and tag it with the name “my-node-app”.

4. Once the image has been built, you can run a container using the following command:

docker run -p 3000:3000 my-node-app

5. This command tells Docker to run a container from the “my-node-app” image, map port 3000 on your local machine to port 3000 in the container, and start the application.

You should now be able to access your web application by visiting http://localhost:3000 in your web browser.

03. How to verify that the application is working as expected by accessing it in a web browser..?

To verify that the application is working as expected, you can follow these steps:

  1. Open a web browser of your choice.

  2. In the address bar, enter the IP address or hostname of the machine where the Docker container is running, followed by port 3000. For example, if the machine’s IP address is 192.168.0.10, you would enter http://192.168.0.10:3000.

  3. If everything is working correctly, you should see the web application displayed in your web browser.

If you encounter any issues, you can check the logs of the Docker container by running the following command:

docker logs <container_id>

Replace <container_id> with the ID of the Docker container, which you can find by running the command docker ps. The logs may contain useful information about any errors or issues with the application.

04. How to Push the image to a public or private repository docker hub..?

To push the Docker image to a public or private repository on Docker Hub, follow these steps:

  1. Log in to Docker Hub using the docker login command. If you don't already have an account, you can create one for free on the Docker Hub website.
docker login

This command prompts you to enter your Docker Hub username and password.

2. Tag the image with your Docker Hub username and the name of the repository you want to push the image to. For example, if your Docker Hub username is myusername and you want to push the image to a repository called my-node-app, you would run the following command:

docker tag my-node-app myusername/my-node-app

3. Push the image to the repository using the docker push command:

docker push myusername/my-node-app

04. This command uploads the image to Docker Hub and makes it available to others.

Once the push is complete, you can confirm that the image has been uploaded to Docker Hub by visiting the Docker Hub website and checking your repositories.