Getting Started with Docker: A Beginner's Guide

  • What is Docker and Why use it?

Docker is a popular technology used in software development and deployment. It allows you to package an application and all its dependencies into a standardized unit called a "container." These containers are isolated and portable, meaning they can run consistently across different environments, such as your local computer, a server, or even a cloud platform.

Think of a container as a lightweight and self-contained virtual machine that contains everything needed to run your application, such as the code, runtime, libraries, and system tools. Docker provides a consistent and reproducible environment for your application, regardless of the underlying infrastructure.

Docker is used in various real-life scenarios and offers significant advantages in software development and deployment. Here are a few examples of its applications and why it is important:

  1. Simplified Application Deployment: Docker simplifies the deployment process by packaging applications and their dependencies into containers. This makes it easier to distribute and deploy applications consistently across different environments, such as development, testing, and production. With Docker, you can avoid issues caused by differences in the underlying infrastructure, ensuring that applications run reliably wherever they are deployed.

  2. Microservices Architecture: Docker is widely used in the context of microservices, where an application is divided into smaller, loosely coupled services. Each service runs in its container, making it easier to develop, test, deploy, and scale independently. Docker enables efficient management and orchestration of these microservices, allowing developers to focus on building and improving individual components.

  3. Continuous Integration and Continuous Deployment (CI/CD): Docker plays a vital role in CI/CD pipelines, which are used to automate the build, test, and deployment processes. By packaging applications into containers, developers can ensure that the same containerized application tested during development is deployed in production. Docker enables consistent and reliable application delivery, reducing deployment-related issues and improving the speed and efficiency of the CI/CD workflow.


  • Docker Architecture

By now, you must be aware of what Docker is and its importance in various real-life scenarios. Docker simplifies application deployment, enables microservices architecture, enhances CI/CD workflows, and facilitates hybrid and multi-cloud deployments. Now let's see how docker works :

Some Terminologies

  1. Container: A container is a lightweight and isolated runtime environment that packages an application and its dependencies, providing a consistent and reproducible execution environment.

  2. Image: An image is a read-only template used to create containers. It contains the application's code, runtime, libraries, and dependencies. Images are built based on instructions defined in a Dockerfile.

  3. Dockerfile: A Dockerfile is a text file that contains a set of instructions used to build a Docker image. It specifies the base image, copies files into the image, set environment variables, and configures the container's behavior.

  4. Registry: A Docker registry is a central repository where Docker images can be stored and shared. Docker Hub is the default public registry, but you can also set up private registries for your organization.

  5. Containerization: Containerization is the process of encapsulating an application and its dependencies into containers. It provides isolation, portability, and consistency, allowing applications to run consistently across different environments.

  6. Volume: A volume is a Docker feature used for persistent data storage. It allows you to share and persist data between containers or between a container and the host system, even if the container is restarted or recreated.

  7. Docker Compose: Docker Compose is a tool used for defining and managing multi-container applications. It allows you to describe a multi-container application in a YAML file and then use a single command to start, stop, and manage the application.


  • Confused between a Container and an Image?

ImageContainer
1. Definition: A Docker image is a read-only template or snapshot of a specific environment that includes everything needed to run an application, such as the code, runtime, libraries, and system tools. It is built based on the instructions provided in a Dockerfile.1. Definition: A Docker container is a running instance of a Docker image. It is a lightweight and isolated execution environment that runs processes and services in an isolated manner.
2. Content: An image is a static file that contains layers of the file system and metadata. Each layer represents a specific instruction in the Dockerfile. Images are designed to be immutable, meaning they do not change once created.2. Lifecycle: Containers are created from images and exist as long as the process inside them is running. Once the process stops, the container is terminated. Containers can be stopped, started, and deleted without affecting the underlying image.
3. Purpose: Images serve as the foundation for containers. They act as a blueprint for creating and running one or more containers.3. Isolation: Containers provide process-level isolation, meaning each container has its own isolated file system, network stack, and process space. Containers are isolated from both the host system and other containers, ensuring that applications do not interfere with one another.
4. Creation: Images are created using the docker build command or pulled from a Docker registry using the docker pull command.4. Modifiability: Containers are designed to be ephemeral and can be easily created, destroyed, and recreated as needed. Changes made within a container, such as writing data or modifying the environment, are not persisted unless explicitly stored in volumes.
5. Size: Docker images can vary in size depending on the number of layers and the content they include. They can range from a few megabytes to several gigabytes in size.5. Portability: Containers are highly portable. They can be run consistently across different environments and platforms, as long as the target system has Docker installed. Containers encapsulate the application and its dependencies, making them independent of the underlying infrastructure.

  • Docker Commands

    Note: Please Download Docker Desktop for your System.

    docker run: This command is used to create and start a new container from a Docker image. It pulls the image from a registry (if not already available locally) and runs it as a container. For example: docker run <image_name>.

      docker run ubuntu
      docker run -it ubuntu bash
    

    Note: -it flag and bash command is used to open a terminal inside the ubuntu that is currently running inside the container. Bash commands like echo will work inside that shell.

    docker ps: This command lists all the running containers on your system. It provides information such as the container ID, image used, status, and names. Adding the -a flag will show all containers, including those that are stopped.

      docker ps
    

    docker stop: This command is used to stop a running container. It gracefully stops the processes running inside the container and shuts it down. You need to provide either the container ID or the container name as an argument. For example: docker stop <container_id>.

      docker stop c3a32f7fab7e
    

    Note: c3a32f7fab7e is the id of the docker container currently running inside the local system.

    docker rm: This command removes one or more stopped containers from your system. You can specify the container IDs or names as arguments. For example: docker rm <container_id1> <container_id2>.

      docker rm c3a32f7fab7e
    

    Note: Here we are deleting the UBUNTU container that is formed using an image.

    docker images: This command lists all the Docker images available on your system. It displays information such as the image ID, repository, tag, and size.

      docker images
    

    docker pull: This command is used to download a Docker image from a registry. It fetches the specified image and stores it locally on your system. For example: docker pull <image_name>.

      docker pull nginx
    
    What is NGINX ?
    Nginx (pronounced "engine-x") is a high-performance, open-source web server and reverse proxy server. It is designed to efficiently handle concurrent connections and serve static content with low memory usage. Nginx is known for its scalability, stability, and ability to handle high traffic loads. Nginx can serve as a web server, delivering static files like HTML, CSS, and images directly to clients. It can also act as a reverse proxy, forwarding client requests to backend servers, such as application servers or other web servers. As a reverse proxy, Nginx can improve performance, load balance traffic across multiple servers, and provide caching and SSL/TLS termination.

    docker build: This command is used to build a Docker image from a Dockerfile. It reads the instructions defined in the Dockerfile and creates a new image. For example: docker build -t <image_name> . (where the . represents the current directory containing the Dockerfile).

      docker build -t docker-intro .
    

    docker exec: This command allows you to execute a command inside a running container. It is useful for running additional commands or accessing a container's shell. For example: docker exec -it <container_id> bash (starts a bash shell inside the container).

      docker exec -it <container_id> cmd
    

    docker logs: This command shows the logs of a running container. It displays the output generated by the processes running inside the container. You can specify the container ID or name as an argument. For example: docker logs <container_id>.

      docker logs <container_id>
    

  • Bonus Point

To run a simple web server using Python's built-in HTTP server, you can use the following command:

docker run -it ubuntu python3 -m http.server

Note: This will start the Ubuntu container and run the Python HTTP server, which will continuously serve files on port 8000. You can access the server from your host machine by opening a web browser and navigating to http://localhost:8000.