Docker
What is Docker ?
Docker is a platform designed to simplify the process of creating, deploying, and running applications by using containerization technology. Containers are lightweight, portable, and run consistently across different computing environments. Docker enables developers to package an application along with all its dependencies into a standardized unit called a container.
How dose docker work ?
Docker works through the CLI by allowing users to interact with the Docker Daemon using a set of command-line commands. When a user issues a command via the Docker CLI (e.g., docker run to start a container), the CLI translates this command into an API request and sends it to the Docker Daemon (dockerd). The Docker Daemon then processes the request and manages the necessary Docker objects. For example, if the command is to run a container, the Daemon pulls the specified Docker image (if not already present locally), creates a new container from that image, and starts it. The CLI provides a comprehensive interface for all Docker operations, such as building images (docker build), managing containers (docker start, docker stop, docker rm), and handling networks and volumes. This interaction ensures that users can efficiently manage their Docker environments through simple, consistent commands.
Key Components of Docker
- Docker Engine: - The core component that allows you to create and run containers.
- Consists of a daemon, an API, and a CLI.
 
- Docker Daemon ( - dockerd):- A background service running on the host operating system that manages Docker containers, images, networks, and storage volumes.
- Listens for Docker API requests and manages Docker objects like images, containers, networks, and volumes.
 
- Docker Client ( - docker):- The command-line interface (CLI) used by users to interact with the Docker Daemon.
- Sends commands to the Docker Daemon through REST API requests.
 
- Docker Images: - Read-only templates used to create Docker containers.
- Can be built from scratch or downloaded from Docker Hub or other repositories.
 
- Docker Containers: - Executable instances of Docker images.
- Encapsulate an application and its dependencies in a standardized unit.
- Lightweight and isolated from the host and other containers.
 
- Docker Hub: - A cloud-based repository where Docker users can create, test, store, and distribute container images.
- Offers public and private repositories for sharing container images.
 
- Docker Compose: - A tool for defining and running multi-container Docker applications.
- Uses a YAML file to configure the application's services, networks, and volumes.
- Allows for the orchestration of multiple containers as a single service.
 
- Dockerfile: - A text file containing a series of instructions on how to build a Docker image.
- Specifies the base image, commands to run, files to include, and environment variables.
 
- Docker Swarm: - A native clustering and orchestration tool for Docker.
- Turns a pool of Docker hosts into a single, virtual Docker host.
- Allows for the management of a cluster of Docker engines, creating and scaling services.
 
- Docker Volumes: - Mechanisms for persisting data generated and used by Docker containers.
- Allow data to be shared between containers and survive container restarts.
 
- Docker Network: 
- A virtual network that connects Docker containers.
- Provides various network drivers (bridge, host, overlay) to control how containers communicate.
The Docker life cycle encompasses the stages from the creation of a Docker image to the termination of a Docker container. Here’s an overview of the Docker life cycle:
- Build: This is the initial phase where you create a Docker image. You define the image in a Dockerfile, specifying the base image, dependencies, configurations, and the application code. The - docker buildcommand processes the Dockerfile and creates a Docker image.
- Registry (Optional): After building the image, you can push it to a Docker registry (such as Docker Hub, AWS ECR, or a private registry) to make it accessible for others or for deployment purposes. 
- Pull (Optional): On any host machine, you can pull the Docker image from the registry to run it. 
- Run: You create and start a container from the Docker image using the - docker runcommand. During this phase, the container is in a running state, executing the application as specified.
- Pause and Unpause: You can temporarily pause and later resume a container's processes. This is useful for resource management. 
- Stop: To stop a running container, you can use the - docker stopcommand. This gracefully stops the container by sending a SIGTERM signal and then a SIGKILL if the container does not stop within a grace period.
- Restart: You can restart a stopped or running container using the - docker restartcommand.
- Kill: If you need to forcefully stop a container, you can use the - docker killcommand, which sends a SIGKILL signal to the container.
- Remove (rm): After stopping a container, you can remove it to free up resources. Use the - docker rmcommand to remove a stopped container
- Prune (Optional): To clean up unused Docker objects (containers, networks, images, volumes), you can use the - docker system prunecommand. This helps in maintaining a clean Docker environment.- Here are the top Docker commands often asked about in interviews - docker version: Displays the Docker version information. 
- docker info: Displays detailed system-wide information about Docker. 
- docker build: Builds an image from a Dockerfile. 
- docker pull: Downloads an image from a Docker registry. 
- docker push: Uploads an image to a Docker registry. 
- docker images: Lists all Docker images on the local machine. 
- docker rmi: Removes one or more Docker images. 
- docker ps: Lists all running containers. 
- docker run: Creates and starts a new container from an image. 
- docker exec: Runs a command in a running container 
- docker stop: Stops a running container. 
- docker start: Starts a stopped container. 
- docker restart: Restarts a running or stopped container. 
- docker kill: Kills a running container. 
- docker rm: Removes one or more stopped containers 
- docker logs: Fetches the logs of a container. 
- docker inspect: Displays detailed information about a container or image. 
- docker network ls: Lists all Docker networks. 
- docker network create: Creates a new Docker network. 
- docker-compose up: Starts and runs multi-container Docker applications defined in a - docker-compose.ymlfile.
- docker-compose down: Stops and removes containers, networks, images, and volumes created by - docker-compose up.
- docker volume ls: Lists all Docker volumes. 
- docker volume rm: Removes one or more Docker volumes. 
- docker tag: Creates a tag for an image. 
- docker prune: Cleans up unused Docker objects (containers, images, networks, volumes). 
 What is Docker File ?
A Dockerfile is a script that contains a series of commands and instructions to create a Docker image. The image is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and configuration files.
Here are some key components and concepts related to a Dockerfile:
- Base Image: The starting point for your Docker image. It is defined by the FROMinstruction.
- Maintainer: (Optional) The author or maintainer of the image, defined by the MAINTAINERinstruction.
- Commands: Instructions that are executed during the image build process.
- RUN: Executes commands in a new layer on top of the current image and commits the results.
- COPYand- ADD: Copy files and directories from the host filesystem into the image.
- CMD: Provides a default command to run when a container is started from the image.
- ENTRYPOINT: Configures a container to run as an executable.
- ENV: Sets environment variables.
- EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
- WORKDIR: Sets the working directory for any- RUN,- CMD,- ENTRYPOINT,- COPY, and- ADDinstructions.
- USER: Sets the user name or UID to use when running the image and for any- RUN,- CMD, and- ENTRYPOINTinstructions.
- VOLUME: Creates a mount point with the specified path and marks it as holding externally mounted volumes from the host or other containers.
# Use the official Nginx image as a base image
FROM nginx:latest
         # Set the working directory to the default Nginx HTML directory
         WORKDIR /usr/share/nginx/html
         # Copy the index.html file from the current directory on your host to the container's HTML                   directory
      COPY index.html .
    # Expose port 80 (the default Nginx port)
     EXPOSE 80
    # No need to define CMD as the base image already has the default command to run Nginx
Steps to Build and Run the Docker Image
- Prepare the - index.htmlfile: Ensure you have an- index.htmlfile in your project directory. This file should be on your Windows machine.
- Write the Dockerfile: Create a file named - Dockerfilein your project directory with the content provided above.
- Build the Docker image: Open a terminal (e.g., PowerShell or Command Prompt) and navigate to the directory containing the Dockerfile. Run the following command to build the image: - docker build -t my-nginx-image .- This command builds the image and tags it as - my-nginx-image.
- Run the Docker container with a random port: Use the following command to run a container based on your image and map a random port on your host to port 80 in the container: - docker run -d -P my-nginx-image- The - -dflag runs the container in detached mode, and the- -Pflag automatically maps a random port on the host to the exposed port 80 in the container.
- Find the mapped port: To find the port that Docker has mapped to port 80, you can use the following command: - docker ps- This command will list all running containers and their details, including the port mappings. Look for the line corresponding to your container, which will show something like - 0.0.0.0:<random_port>->80/tcp.

Comments
Post a Comment