> For the complete documentation index, see [llms.txt](https://sai11101989.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sai11101989.gitbook.io/devops/paas-containers/docker.md).

# Docker

Docker is a powerful containerization platform that simplifies the process of building, shipping, and running applications. Here's an overview of its major components and how to use them:

### Major Components of Docker

1. **Docker Client**:
   * The Docker client is the interface through which users interact with the Docker daemon. It allows users to create, manage, and delete Docker objects like images, containers, networks, and volumes[1](https://sysdig.com/learn-cloud-native/docker-101-the-docker-components/)[3](https://thinksys.com/devops/docker-components/).
2. **Docker Daemon (Engine)**:
   * The Docker daemon is the core component that manages the creation, execution, and distribution of containers. It runs in the background and listens for Docker API requests[1](https://sysdig.com/learn-cloud-native/docker-101-the-docker-components/)[3](https://thinksys.com/devops/docker-components/).
3. **Docker Images**:
   * Docker images are read-only templates used to build Docker containers. They contain all the necessary code, libraries, and configurations for an application[1](https://sysdig.com/learn-cloud-native/docker-101-the-docker-components/)[3](https://thinksys.com/devops/docker-components/).
4. **Docker Containers**:
   * Containers are runtime instances of Docker images. They provide isolated environments for applications to run in, sharing the host OS kernel[1](https://sysdig.com/learn-cloud-native/docker-101-the-docker-components/)[3](https://thinksys.com/devops/docker-components/).
5. **Docker Registry**:
   * Docker Registry stores Docker images. Docker Hub is the official public registry, but users can also use private registries for storing and sharing images[1](https://sysdig.com/learn-cloud-native/docker-101-the-docker-components/)[2](https://www.linkedin.com/posts/alexxubyte_systemdesign-coding-interviewtips-activity-7172273177094537216-dVOa).
6. **Docker Networking**:
   * Docker networking allows containers to communicate with each other and the host system. It provides features like service discovery and load balancing[3](https://thinksys.com/devops/docker-components/).

### How to Use Docker

### **Step 1: Install Docker**

* Download and install Docker Desktop from the official Docker website. Follow the installation instructions for your operating system[4](https://www.docker.com/blog/docker-for-web-developers/).

### **Step 2: Create a Dockerfile**

* A Dockerfile is a script that contains instructions for building a Docker image. It specifies the base image, copies files, sets environment variables, and defines commands to run during the build process[4](https://www.docker.com/blog/docker-for-web-developers/).

### **Step 3: Build a Docker Image**

* Use the `docker build` command to create an image from your Dockerfile. This command compiles the instructions in the Dockerfile into a usable image[6](https://www.knowledgehut.com/blog/devops/basic-docker-commands).

  ```bash
  docker build -t my-app .
  ```

### **Step 4: Run a Docker Container**

* Use the `docker run` command to start a new container from the image you built. You can specify options like port mapping and detached mode[5](https://docs.docker.com/get-started/workshop/02_our_app/)[6](https://www.knowledgehut.com/blog/devops/basic-docker-commands).

  ```bash
  docker run -d -p 8080:80 my-app
  ```

### **Step 5: Manage Containers**

* Use various Docker commands to manage your containers:
  * `docker ps`: List running containers.
  * `docker stop`: Stop a running container.
  * `docker rm`: Remove a stopped container[6](https://www.knowledgehut.com/blog/devops/basic-docker-commands).

### **Step 6: Push Images to a Registry**

* Use the `docker push` command to share your images with others or deploy them to different environments[6](https://www.knowledgehut.com/blog/devops/basic-docker-commands).

  ```bash
  docker tag my-app:latest <your-username>/my-app:latest
  docker push <your-username>/my-app:latest
  ```

### **Step 7: Use Docker Compose**

* For applications with multiple containers, use Docker Compose to manage and orchestrate them. Create a `docker-compose.yml` file to define services and their dependencies[4](https://www.docker.com/blog/docker-for-web-developers/).

  ```bash
  services:
    web:
      build: .
      ports:
        - "8080:80"
  ```

  Run your application with:

  ```bash
  docker-compose up
  ```

By following these steps, you can effectively use Docker to containerize your applications, ensuring consistent and reliable deployments across different environments.

Building a Docker image from scratch involves creating a Dockerfile that outlines the steps to build your image. Here's a step-by-step guide on how to do it:

### Steps to Build a Docker Image from Scratch

### **1. Create a Dockerfile**

* Start by creating a new text file named `Dockerfile`. This file contains instructions for building your Docker image.

### **2. Choose a Base Image**

* Use the `FROM` instruction to specify a base image. If you want complete control over the image contents, you can use `FROM scratch`. Otherwise, choose an official or verified base image from Docker Hub.

  ```bash
  FROM scratch
  ```

  or

  ```bash
  FROM ubuntu:20.04
  ```

### **3. Add Files**

* Use the `ADD` or `COPY` instruction to add files to your image. `ADD` can also handle URLs and tarballs, while `COPY` is more straightforward for local files.

  ```
  ADD hello /
  ```

  or

  ```
  COPY index.html /app/
  ```

### **4. Run Commands**

* Use the `RUN` instruction to execute commands during the build process, such as installing dependencies.

  ```
  RUN apt-get update && apt-get install -y python3
  ```

### **5. Set Environment Variables**

* Use the `ENV` instruction to set environment variables that will be available during both build and runtime.

  ```
  ENV NAME World
  ```

### **6. Expose Ports**

* Use the `EXPOSE` instruction to specify ports that your application will use.

  ```
  EXPOSE 80
  ```

### **7. Define the Command to Run**

* Use the `CMD` instruction to specify the default command to run when the container starts.

  ```
  CMD ["python", "app.py"]
  ```

### **8. Build the Image**

* Navigate to the directory containing your Dockerfile and run the following command to build the image:

  ```bash
  docker build -t my-image .
  ```

  * `-t` specifies the tag for your image.
  * `.` indicates that the Dockerfile is in the current directory.

### **9. Verify the Image**

* Use the `docker images` command to list all available images and verify that your new image is listed.

  ```bash
  docker images
  ```

### **10. Run the Container**

* Use the `docker run` command to start a new container from your image.

  ```bash
  docker run -d --name my-container -p 8080:80 my-image
  ```

### Example Dockerfile

Here's a simple example of a Dockerfile that builds an image for a Python application:

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

# Set the working directory in the container
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 --no-cache-dir -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"]
```

By following these steps, you can create a Docker image from scratch that encapsulates your application and its dependencies, ensuring consistent and reliable deployments across different environments.
