diff --git a/Containerization & Orchestration/Docker/3-Docker-File.md b/Containerization & Orchestration/Docker/3-Docker-File.md index cc69eca..6920bd3 100755 --- a/Containerization & Orchestration/Docker/3-Docker-File.md +++ b/Containerization & Orchestration/Docker/3-Docker-File.md @@ -1,148 +1,160 @@ -## What is a Dockerfile? - -A **Dockerfile** is a simple text file containing instructions to create a Docker image. Docker images provide a consistent and reproducible environment for running applications in containers. By defining dependencies, configurations, and the operating system, Dockerfiles automate the image creation process, ensuring version-controlled and portable environments. - -### Key Concepts: - -- **Base Image**: The foundational layer of your image, typically an official operating system like Ubuntu, CentOS, or Alpine Linux. -- **Instructions**: Commands such as `RUN`, `COPY`, and `CMD` define what’s installed, how the image behaves, and which files to include. - - Common instructions include: - - **`RUN`**: Executes commands (e.g., installing software) inside the container. - - **`COPY`**: Copies files from your local machine to the image. - - **`CMD`**: Specifies the default command to run when the container starts. +# 🚒 Understanding Dockerfile: A Complete Guide --- -## Step-by-Step Guide to Creating a Dockerfile +## πŸ“„ What is a Dockerfile? -### 1. Create a File Named `Dockerfile` +A **Dockerfile** is a simple text file containing instructions to create a **Docker image**. Docker images provide a consistent, reproducible environment to run applications inside containers. By defining dependencies, configurations, and the operating system, Dockerfiles automate image creation, ensuring version-controlled and portable environments. -Start by creating a file called `Dockerfile` in your project directory. If you name it something else, you'll need to specify the file name during the build process. +--- + +### πŸ”‘ Key Concepts: + +* πŸ—οΈ **Base Image**: The foundational layer of your image, usually an official OS like Ubuntu, CentOS, or Alpine Linux. +* πŸ“ **Instructions**: Commands that tell Docker what to install, how the image behaves, and which files to include. + +Common instructions include: + +| Instruction | Description | +| ----------- | ---------------------------------------------------------------- | +| πŸƒβ€β™‚οΈ `RUN` | Executes commands inside the container (e.g., install software). | +| πŸ“ `COPY` | Copies files from your local machine to the image. | +| ▢️ `CMD` | Specifies the default command when the container starts. | + +--- + +## πŸ› οΈ Step-by-Step Guide to Creating a Dockerfile + +--- + +### 1️⃣ Create a File Named `Dockerfile` + +Create a file called `Dockerfile` in your project directory. If named differently, specify the filename during build. #### Example Dockerfile: ```dockerfile -# Use Ubuntu 22.04 as the base image +# 🐧 Use Ubuntu 22.04 as the base image FROM ubuntu:22.04 -# Add metadata such as version information +# 🏷️ Add metadata such as version information LABEL version="0.0.1" -# Update package lists and install essential tools +# πŸ”„ Update package lists and install essential tools RUN apt update && apt install -y bash vim curl -# Install Nginx web server +# 🌐 Install Nginx web server RUN apt install -y nginx ``` -#### Breakdown of Instructions: +**Explanation:** -- **`FROM ubuntu:22.04`**: Defines Ubuntu 22.04 as the base image. -- **`LABEL version="0.0.1"`**: Adds metadata, such as the version label. -- **`RUN`**: Runs commands inside the container. In this case, it updates package lists and installs `bash`, `vim`, `curl`, and `nginx`. +* `FROM ubuntu:22.04` β€” Use Ubuntu 22.04 as the base image. +* `LABEL version="0.0.1"` β€” Adds version metadata. +* `RUN` β€” Executes commands inside the container to install tools and software. --- -### 2. Example Using Alpine Linux +### 2️⃣ Example Using Alpine Linux -Alpine Linux is a lightweight option that results in smaller images. Here's an example of a Dockerfile using Alpine: +Alpine Linux is lightweight and creates smaller images. ```dockerfile -# Use Alpine as the base image +# 🐧 Use Alpine as the base image FROM alpine -# Add version metadata +# 🏷️ Add version metadata LABEL version="0.0.1" -# Update package lists and install essential tools +# πŸ”„ Update package lists and install essential tools RUN apk update && apk add bash vim curl ``` -This example is perfect for when you need a compact, minimalistic image. +Perfect for a compact, minimalistic image. --- -### 3. Complex Dockerfile with a Script - -In this example, you'll learn how to copy a script into the container, set a working directory, and make the script executable. +### 3️⃣ Complex Dockerfile with a Script ```dockerfile -# Start with Alpine as the base image +# 🐧 Start with Alpine as the base image FROM alpine -# Add metadata +# 🏷️ Add metadata LABEL version="0.0.1" -# Update package lists and install essential tools +# πŸ”„ Update packages and install essential tools RUN apk update && apk add bash vim curl iputils-ping -# Copy the script into the image +# πŸ“‚ Copy the script into the image COPY -# Set the working directory for subsequent commands +# 🏠 Set working directory WORKDIR -# Add environment variables +# 🌿 Add environment variables ENV API_KEY="123445" -# Set user and expose ports +# πŸ‘€ Set user and expose ports USER deploy EXPOSE 3210 -# Give execution permissions to the script +# βš™οΈ Make the script executable RUN chmod +x app.sh -# Define the default command to run +# ▢️ Default command to run CMD ["./app.sh"] -# Alternatively, you can use ENTRYPOINT +# Alternatively, ENTRYPOINT ensures always running the executable ENTRYPOINT ["bash", "./app.sh"] ``` -#### Key Concepts in This Example: -- **`COPY`**: Copies a file from your local machine to the container. -- **`WORKDIR`**: Sets the working directory inside the container for subsequent commands. -- **`RUN chmod +x app.sh`**: Grants execution permissions to the `app.sh` script. -- **`CMD` vs. `ENTRYPOINT`**: `CMD` provides default behavior, while `ENTRYPOINT` is used when you want to ensure the container always runs a specific executable. +--- + +### ❀️ Health Check Setup + +```dockerfile +HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ + CMD curl -f http://localhost/ || exit 1 +``` + +* ⏲️ **interval**: time between checks +* ⏳ **timeout**: fail if check takes longer than 5 seconds +* πŸ”„ **retries**: mark container unhealthy after failed attempts +* πŸ›‘οΈ **start-period**: grace period before counting failures --- -### 4. Build Your Docker Image - -Once your `Dockerfile` is ready, build the Docker image using the `docker build` command. +### 4️⃣ Build Your Docker Image ```bash docker build -t ``` -#### Usage Examples: +**Examples:** + +* Build with Dockerfile in current directory: -- To build with a `Dockerfile` in the current directory: ```bash docker build -t app-test . ``` - Here, the `.` indicates the current directory as the build context. +* Use custom Dockerfile name: -- If your file is named something other than `Dockerfile` (e.g., `CustomDockerfile`): ```bash docker build -t app-test -f . ``` -- If you want build file without use cache - ```bash - docker build -t app-test:v1 -f . --no-cache - ``` +* Build without cache: -#### Explanation: -- **`docker build`**: Builds a Docker image. -- **`-t `**: Tags the image with a name (e.g., `app-test`). -- **``**: Specifies the location of the `Dockerfile`. Use `.` for the current directory or provide an absolute path. + ```bash + docker build -t app-test:v1 -f . --no-cache + ``` --- -## Summary +## πŸ“‹ Summary -A **Dockerfile** is a powerful tool for automating the creation of Docker images. Here’s a quick recap: +A **Dockerfile** is a powerful tool for automating Docker image creation: + +1. πŸ“ **Create a Dockerfile**: Define the image with `FROM`, `RUN`, `COPY`, and `CMD`. +2. πŸ—οΈ **Build the Image**: Use `docker build` to generate your image. +3. πŸš€ **Run the Container**: Use `docker run` to start your container. -1. **Create a Dockerfile**: Use instructions like `FROM`, `RUN`, `COPY`, and `CMD` to define the image. -2. **Build the Image**: Run the `docker build` command to turn your Dockerfile into a Docker image. -3. **Run the Container**: Once the image is built, use `docker run` to create and run a container based on it.