Update Docker File Doc

This commit is contained in:
2024-09-18 18:38:44 +03:30
parent 71186dbaa8
commit 999d3ad88d

View File

@@ -1,20 +1,16 @@
# Dockerfile: A Complete Guide
## What is a Dockerfile? ## 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. 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.
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.
### Key Concepts: ### 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 whats installed, how the image behaves, and which files to include.
Common instructions include: Common instructions include:
- **`RUN`**: Executes commands (like installing software) inside the container. - **`RUN`**: Executes commands (e.g., installing software) inside the container.
- **`COPY`**: Copies files from your local machine into the image. - **`COPY`**: Copies files from your local machine to the image.
- **`CMD`**: Specifies the default command to run when a container starts. - **`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` ### 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.
Heres a basic example of a Dockerfile: #### Example Dockerfile:
```dockerfile ```dockerfile
# Use Ubuntu 22.04 as the base image # 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 RUN apt install -y nginx
``` ```
#### Explanation of Instructions: #### Breakdown 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). - **`FROM ubuntu:22.04`**: Defines Ubuntu 22.04 as the base image.
- **`RUN`**: Executes commands inside the container, such as updating the package list and installing tools like `bash`, `vim`, `curl`, and `nginx`. - **`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 ```dockerfile
# Use the lightweight Alpine base image # Use Alpine as the base image
FROM alpine FROM alpine
# Add version metadata # Add version metadata
@@ -62,46 +59,57 @@ LABEL version="0.0.1"
RUN apk update && apk add bash vim curl 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 ```dockerfile
# Use the lightweight Alpine base image # Start with Alpine as the base image
FROM alpine FROM alpine
# Add version metadata # Add metadata
LABEL version="0.0.1" 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 RUN apk update && apk add bash vim curl iputils-ping
# Copy the script file from the local machine into the image # Copy the script into the image
COPY <location-of-file> <dest-location> COPY <local-file-path> <container-destination-path>
# Set the working directory inside the container # Set the working directory for subsequent commands
WORKDIR <dest-location> WORKDIR <container-destination-path>
# Add environment variables
ENV API_KEY="123445"
# Set user and expose ports
USER deploy
EXPOSE 3210
# Give execution permissions to the script # Give execution permissions to the script
RUN chmod +x app.sh RUN chmod +x app.sh
# Define the default command to run when the container starts # Define the default command to run
CMD ["./app.sh"] CMD ["./app.sh"]
# Alternatively, you can use ENTRYPOINT
ENTRYPOINT ["bash", "./app.sh"]
``` ```
#### Key Additions in This Example: #### Key Concepts in This Example:
- **`COPY <src> <dest>`**: Copies a file from the local system into the container. - **`COPY`**: Copies a file from your local machine to the container.
- **`WORKDIR`**: Sets the working directory for subsequent commands (like `RUN`, `CMD`). - **`WORKDIR`**: Sets the working directory inside the container for subsequent commands.
- **`RUN chmod +x app.sh`**: Grants execute permissions to the script `app.sh`. - **`RUN chmod +x app.sh`**: Grants execution permissions to the `app.sh` script.
- **`CMD ["./app.sh"]`**: Specifies the command to run when the container starts (in this case, running 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 ```bash
docker build -t <app-name> <path-to-dockerfile> docker build -t <app-name> <path-to-dockerfile>
@@ -109,29 +117,28 @@ docker build -t <app-name> <path-to-dockerfile>
#### Usage Examples: #### 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 ```bash
docker build -t app-test . 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 ```bash
docker build -t app-test -f CustomDockerfile . 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: #### Explanation:
- **`docker build`**: Command to build a Docker image. - **`docker build`**: Builds a Docker image.
- **`-t <app-name>`**: Tags the image with a name (e.g., `app-test`). This is useful for referring to the image later. - **`-t <app-name>`**: Tags the image with a name (e.g., `app-test`).
- **`<path-to-dockerfile>`**: Specifies the location of the `Dockerfile`. You can use `.` to refer to the current directory or provide an absolute path. - **`<path-to-dockerfile>`**: Specifies the location of the `Dockerfile`. Use `.` for the current directory or provide an absolute path.
--- ---
## Summary ## 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. Heres a quick recap of the process: A **Dockerfile** is a powerful tool for automating the creation of Docker images. Heres a quick recap:
1. **Create a Dockerfile**: Define the image using instructions like `FROM`, `RUN`, `COPY`, and `CMD`. 1. **Create a Dockerfile**: Use instructions like `FROM`, `RUN`, `COPY`, and `CMD` to define the image.
2. **Build the Image**: Use the `docker build` command to turn the Dockerfile into a Docker image. 2. **Build the Image**: Run the `docker build` command to turn your 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. 3. **Run the Container**: Once the image is built, use `docker run` to create and run a container based on it.