diff --git a/Containerization & Orchestration/Docker/3-Docker-File.md b/Containerization & Orchestration/Docker/3-Docker-File.md index 67f66e9..8689871 100644 --- a/Containerization & Orchestration/Docker/3-Docker-File.md +++ b/Containerization & Orchestration/Docker/3-Docker-File.md @@ -1,20 +1,16 @@ - -# Dockerfile: A Complete Guide - ## What is a Dockerfile? -A **Dockerfile** is a simple text file that contains a list of instructions and commands to create a Docker image. Docker images serve as a blueprint for containers, providing a consistent and reproducible environment to run applications. - -Using a Dockerfile, you can automate the process of creating these images, making it easy to define software dependencies, configuration, and the operating system in a clear, version-controlled format. +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**: This is the starting point for your Docker image. You typically begin with an official operating system image like Ubuntu, CentOS, or Alpine Linux. -- **Instructions**: Commands like `RUN`, `COPY`, and `CMD` define what gets installed, how the image behaves, and which files to include. + +- **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 (like installing software) inside the container. - - **`COPY`**: Copies files from your local machine into the image. - - **`CMD`**: Specifies the default command to run when a container starts. + - **`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. --- @@ -22,9 +18,9 @@ Using a Dockerfile, you can automate the process of creating these images, makin ### 1. Create a File Named `Dockerfile` -The first step is to create a file called `Dockerfile` in your project directory. If you name it something other than `Dockerfile`, you'll need to specify the file name when building the image (more on that later). +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. -Here’s a basic example of a Dockerfile: +#### Example Dockerfile: ```dockerfile # Use Ubuntu 22.04 as the base image @@ -40,19 +36,20 @@ RUN apt update && apt install -y bash vim curl RUN apt install -y nginx ``` -#### Explanation of Instructions: -- **`FROM ubuntu:22.04`**: Sets the base image to Ubuntu 22.04. -- **`LABEL version="0.0.1"`**: Adds metadata to the image (in this case, a version label). -- **`RUN`**: Executes commands inside the container, such as updating the package list and installing tools like `bash`, `vim`, `curl`, and `nginx`. +#### Breakdown of Instructions: + +- **`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`. --- -### 2. Another Example Using Alpine Linux +### 2. Example Using Alpine Linux -Alpine Linux is a minimal, lightweight distribution often used to create smaller Docker images. Below is an example Dockerfile using Alpine as the base image: +Alpine Linux is a lightweight option that results in smaller images. Here's an example of a Dockerfile using Alpine: ```dockerfile -# Use the lightweight Alpine base image +# Use Alpine as the base image FROM alpine # Add version metadata @@ -62,46 +59,57 @@ LABEL version="0.0.1" RUN apk update && apk add bash vim curl ``` +This example is perfect for when you need a compact, minimalistic image. + --- -### 3. Complex Dockerfile Example with a Script +### 3. Complex Dockerfile with a Script -This example shows how to copy a script into the image, set the working directory, and grant execution permissions to the script. +In this example, you'll learn how to copy a script into the container, set a working directory, and make the script executable. ```dockerfile -# Use the lightweight Alpine base image +# Start with Alpine as the base image FROM alpine -# Add version metadata +# Add metadata LABEL version="0.0.1" -# Update package lists and install essential tools and network utilities +# Update package lists and install essential tools RUN apk update && apk add bash vim curl iputils-ping -# Copy the script file from the local machine into the image -COPY +# Copy the script into the image +COPY -# Set the working directory inside the container -WORKDIR +# Set the working directory for subsequent commands +WORKDIR + +# Add environment variables +ENV API_KEY="123445" + +# Set user and expose ports +USER deploy +EXPOSE 3210 # Give execution permissions to the script RUN chmod +x app.sh -# Define the default command to run when the container starts +# Define the default command to run CMD ["./app.sh"] +# Alternatively, you can use ENTRYPOINT +ENTRYPOINT ["bash", "./app.sh"] ``` -#### Key Additions in This Example: -- **`COPY `**: Copies a file from the local system into the container. -- **`WORKDIR`**: Sets the working directory for subsequent commands (like `RUN`, `CMD`). -- **`RUN chmod +x app.sh`**: Grants execute permissions to the script `app.sh`. -- **`CMD ["./app.sh"]`**: Specifies the command to run when the container starts (in this case, running the `app.sh` script). +#### 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. --- -### 4. Build an Image from the Dockerfile +### 4. Build Your Docker Image -Once you have your `Dockerfile` set up, the next step is to build the Docker image. You can do this with the `docker build` command. +Once your `Dockerfile` is ready, build the Docker image using the `docker build` command. ```bash docker build -t @@ -109,29 +117,28 @@ docker build -t #### Usage Examples: -- If the file is named `Dockerfile` and is located in the current directory: +- To build with a `Dockerfile` in the current directory: ```bash docker build -t app-test . ``` - Here, the `.` specifies the current directory as the build context (where Docker looks for the `Dockerfile`). + Here, the `.` indicates the current directory as the build context. -- If the file is named something else (e.g., `CustomDockerfile`): +- If your file is named something other than `Dockerfile` (e.g., `CustomDockerfile`): ```bash docker build -t app-test -f CustomDockerfile . ``` - In this case, `-f CustomDockerfile` tells Docker to use the custom-named Dockerfile. -#### Explanation of the Build Command: -- **`docker build`**: Command to build a Docker image. -- **`-t `**: Tags the image with a name (e.g., `app-test`). This is useful for referring to the image later. -- **``**: Specifies the location of the `Dockerfile`. You can use `.` to refer to the current directory or provide an absolute path. +#### 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. --- ## Summary -A Dockerfile simplifies the process of creating Docker images, allowing you to automate the creation of a consistent and reproducible environment for your applications. Here’s a quick recap of the process: +A **Dockerfile** is a powerful tool for automating the creation of Docker images. Here’s a quick recap: -1. **Create a Dockerfile**: Define the image using instructions like `FROM`, `RUN`, `COPY`, and `CMD`. -2. **Build the Image**: Use the `docker build` command to turn the Dockerfile into a Docker image. -3. **Run the Container**: After building the image, you can create and run a container using the `docker run` command. +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.