Deep Dive with Docker along with Project

Deep Dive with Docker along with Project

Day 17 of 90days of DevOps

·

9 min read

Hello Readers,

Here we are Day 17 of #90daysofdevops

👣 Topics for #day17

  • Let's know how to use Docker commands without using sudo

  • Let's Understand Docker File?

  • Tasks of Day 17

2. Docker File, Image, Container - YouTube


Let's know how to use Docker commands without using sudo

Use the below command which will add a user to the "docker" group on a Linux system

sudo usermod -a -G docker $USER
  • usermod: This command is used to modify a user's attributes in the system's user database.

  • -a: This option is used to append the specified group to the user's current list of groups, without removing any existing groups.

  • -G docker: This option specifies the name of the group that the user should be added to. In this case its docker.

  • $USER: This is a shell variable that expands to the current username of the user executing the command. It is used to specify the user that should be added to the "docker" group.

Overall,the above command adds the current user to the "docker" group, which allows them to run Docker commands without having to use the sudo command every time.

After running above command we need to reboot the system.

Hope by now all of us know how to reboot a system.


Docker File

Dockerfile: main commands and instructions - DEV Community


Before that lets recap few basic topics.

Docker : Docker is a platform that enables developers to package, deploy, and run applications in isolated environments called containers.

Containers : Containers are lightweight, standalone executable packages that contain everything needed to run an application, including code, libraries, system tools, and settings, and provide isolation from the host environment.

Docker Hub : Docker Hub is a cloud-based registry service that enables users to store, manage, and share Docker images and applications.

Reference Link to Recap the above more briefly: https://pavankumar07.hashnode.dev/getting-started-with-docker


Let's dig into Docker File

  • A Dockerfile is a text file.

  • Text file contains instructions to build a Docker image.

  • A Docker image is a lightweight, standalone, and executable package that contains all the necessary dependencies, libraries, and configuration files required to run an application.

Does Docker Image and Containers are same?

No, Docker Images and containers are not the same things, although they are closely related.

Docker Image is used to create Docker Containers.

Think of Docker Image as a blueprint or template for creating Docker containers. You can think of an image as a snapshot of a container, or a read-only template for creating containers.

A Docker container is a runnable instance of an image. It can be started, stopped, deleted, and its contents can be modified. When you start a container, Docker creates a writable layer on top of the read-only image, allowing you to make changes and store data.

Docker Objects. Dockerfile | by Bikram | Medium


Lets understand Docker File with a Scenerio

A Dockerfile consists of a series of instructions and arguments that define how to build a Docker image.

And where do we use this Docker File?

Scenerio:

Let's say you are developing a web application using Python Flask. You want to deploy the application on a server, but you want to ensure that the environment on the server is the same as the environment on your development machine. You also want to make sure that the deployment process is repeatable and consistent.

To achieve this, you decide to use Docker.

Example of a basic Dockerfile:

FROM python:3.9-alpine
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "app.py"]

Here is what each line of this Dockerfile does:

  • FROM python:3.9-alpine - Specifies the base image to use for the Docker image. In this case, we're using the official Python 3.9 image with the Alpine Linux distribution as the base.

  • WORKDIR /app - Sets the working directory inside the Docker image to /app.

  • COPY . . - Copies all the files and directories from the current directory on the host machine to the /app directory in the Docker image.

  • RUN pip install --no-cache-dir -r requirements.txt - Runs the pip package manager to install the dependencies listed in the requirements.txt file.

  • CMD ["python", "app.py"] - Specifies the default command to run when the Docker container is started. In this case, we're running the app.py Python script.

Once you have the Dockerfile, you can use the docker build command to build a Docker image from it:

docker build -t myapp .

This will create a new Docker image with the name myapp.

To run the application in a container, you can use the docker run command:

docker run -p 5000:5000 myapp

This will start a new container based on the myapp image and map port 5000 on the host machine to port 5000 in the container.

The application will be accessible on http://localhost:5000.

By using a Dockerfile to build the Docker image, you can ensure that the environment on the server is the same as the environment on your development machine, and the deployment process is repeatable and consistent.


Tasks of Day 17

Project:


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

Here I considered Python App(React Django Demo App)

Git Repo:https://github.com/pavankumarraj07/react_django_demo_app

Before writing the Docker File I have cloned the project from GitHub

by looking at the manage.py I can guess that this will be a Python project.

Based on this I considered that we need Python to be installed in container in order to run this app along with the other requirements.

Docker File:

Explanation:

  • FROM python:3.9 - Specifies the base image to use for the Docker image. In this case, we're using the official Python 3.9 image as the base.

  • COPY . . - Copies all the files and directories from the current directory on the host machine to the root directory in the Docker image. This is a common pattern for copying the application code and configuration files into the Docker image.

  • RUN pip install -r requirements.txt - Runs the pip package manager to install the dependencies listed in the requirements.txt file. This command assumes that the requirements.txt file is in the same directory as the Dockerfile and copies it to the Docker image in the previous COPY step.

  • CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] - Specifies the default command to run when the Docker container is started. In this case, we're running the manage.py script using Python to start the web server on port 8000 and bind it to the IP address 0.0.0.0, which allows external connections to the container.

When you build and run the Docker image using the docker build and docker run commands, respectively, the Docker container will start the Python web server with the application code and dependencies installed, and listen on port 8000 for incoming HTTP requests.

This Dockerfile assumes that the necessary code and configuration files are located in the same directory as the Dockerfile and are copied into the Docker image using the COPY command. The requirements.txt file should contain a list of Python packages required by the application, and the RUN command installs them using pip. The CMD command specifies the default command to run when the container starts, which in this case is the web server command.


Step 2: Build the image using the Dockerfile and run the container

Explanation:

  • docker build - This command is used to build a Docker image from a Dockerfile.

  • . - The . specifies the build context, which is the directory that contains the Dockerfile and any other files needed for the build process. In this case, . refers to the current directory.

  • -t react-demo-app:latest - The -t option is used to tag the Docker image with a name and optionally a version. In this case, we're tagging the image with the name react-demo-app and the version latest.

When you run this command, Docker will look for a Dockerfile in the current directory and use it to build a new Docker image with the specified tag. The resulting Docker image will contain all the files and configuration specified in the Dockerfile, and can be used to run containers that have the application or service you want to run.


Step 3: Docker Run

Explanation:

  • docker run - This command is used to start a Docker container based on a Docker image.

  • -d - The -d option starts the container in detached mode, meaning it runs in the background and doesn't attach to the console of the container.

  • -p 8000:8000 - The -p option maps a port from the host machine to a port on the container. In this case, we're mapping port 8000 on the host machine to port 8000 on the container.

  • react-demo-app:latest - This specifies the name and version of the Docker image to use when starting the container. In this case, we're using the react-demo-app image with the latest tag.

When you run this command, Docker will start a new container based on the specified image, and map port 8000 on the host machine to port 8000 on the container. The container will run in the background, and you can use other Docker commands like docker ps to see the status of the container.


Step 4:Verify that the application is working as expected by accessing it in a web browser

Yes, Its working as expected.


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

Successfully Pushed my First Docker Image into Docker Hub


Tips:

Play with Docker Classroom

https://training.play-with-docker.com/

The Play with Docker classroom brings you labs and tutorials that help you get hands-on experience using Docker. In this classroom you will find a mix of labs and tutorials that will help Docker users, including SysAdmins, IT Pros, and Developers. There is a mix of hands-on tutorials right in the browser, instructions on setting up and using Docker in your own environment, and resources about best practices for developing and deploying your own applications.

Start with one of the Getting Started Guides, and then explore the individual labs that explore many advanced features of Docker.

For a comprehensive approach to understanding Docker, choose your preferred journey, IT Pros and System Administrators, or Developers.


Thank you for reading my Blog. I hope you have learnt something from it! If you find this blog helpful, please like, share, and follow me for more interesting posts like this in the future.

Pavan Kumar R

LinkedIn Link

Â