Update Docker Doc
This commit is contained in:
@@ -101,6 +101,14 @@ docker run -dit nginx
|
|||||||
```
|
```
|
||||||
- **`docker run -dit`**: Runs a container in detached mode (background).
|
- **`docker run -dit`**: Runs a container in detached mode (background).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -dit --restart=always nginx
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -dit --restart=always nginx
|
||||||
|
```
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -dit --name <container-name> nginx
|
docker run -dit --name <container-name> nginx
|
||||||
```
|
```
|
||||||
@@ -136,6 +144,12 @@ docker logs -f
|
|||||||
```
|
```
|
||||||
- **`docker logs -f`**: Streams logs of a container in real-time.
|
- **`docker logs -f`**: Streams logs of a container in real-time.
|
||||||
|
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker events
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker exec -it <container-name>
|
docker exec -it <container-name>
|
||||||
```
|
```
|
||||||
@@ -189,6 +203,7 @@ docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-name>
|
|||||||
```bash
|
```bash
|
||||||
docker cp <file_on_local> <container-name>:/<location>
|
docker cp <file_on_local> <container-name>:/<location>
|
||||||
```
|
```
|
||||||
|
`-A` : use for archive mode (keep permmision and )
|
||||||
- **`docker cp <file_on_local> <container-name>:/<location>`**: Copies files from local machine to the container.
|
- **`docker cp <file_on_local> <container-name>:/<location>`**: Copies files from local machine to the container.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
137
Containerization & Orchestration/Docker/3-Docker-File.md
Normal file
137
Containerization & Orchestration/Docker/3-Docker-File.md
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
### 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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Step-by-Step Guide to Creating a 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).
|
||||||
|
|
||||||
|
Here’s a basic example of a Dockerfile:
|
||||||
|
|
||||||
|
```dockerfile
|
||||||
|
# Use Ubuntu 22.04 as the base image
|
||||||
|
FROM ubuntu:22.04
|
||||||
|
|
||||||
|
# Add metadata such as version information
|
||||||
|
LABEL version="0.0.1"
|
||||||
|
|
||||||
|
# Update package lists and install essential tools
|
||||||
|
RUN apt update && apt install -y bash vim curl
|
||||||
|
|
||||||
|
# Install Nginx web server
|
||||||
|
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`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. Another 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:
|
||||||
|
|
||||||
|
```dockerfile
|
||||||
|
# Use the lightweight Alpine base image
|
||||||
|
FROM alpine
|
||||||
|
|
||||||
|
# Add version metadata
|
||||||
|
LABEL version="0.0.1"
|
||||||
|
|
||||||
|
# Update package lists and install essential tools
|
||||||
|
RUN apk update && apk add bash vim curl
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Complex Dockerfile Example 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.
|
||||||
|
|
||||||
|
```dockerfile
|
||||||
|
# Use the lightweight Alpine base image
|
||||||
|
FROM alpine
|
||||||
|
|
||||||
|
# Add version metadata
|
||||||
|
LABEL version="0.0.1"
|
||||||
|
|
||||||
|
# Update package lists and install essential tools and network utilities
|
||||||
|
RUN apk update && apk add bash vim curl iputils-ping
|
||||||
|
|
||||||
|
# Copy the script file from the local machine into the image
|
||||||
|
COPY <location-of-file> <dest-location>
|
||||||
|
|
||||||
|
# Set the working directory inside the container
|
||||||
|
WORKDIR <dest-location>
|
||||||
|
|
||||||
|
# Give execution permissions to the script
|
||||||
|
RUN chmod +x app.sh
|
||||||
|
|
||||||
|
# Define the default command to run when the container starts
|
||||||
|
CMD ["./app.sh"]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Key Additions in This Example:
|
||||||
|
- **`COPY <src> <dest>`**: 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).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. Build an Image from the Dockerfile
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t <app-name> <path-to-dockerfile>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Usage Examples:
|
||||||
|
|
||||||
|
- If the file is named `Dockerfile` and is located 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`).
|
||||||
|
|
||||||
|
- If the file is named something else (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 <app-name>`**: Tags the image with a name (e.g., `app-test`). This is useful for referring to the image later.
|
||||||
|
- **`<path-to-dockerfile>`**: Specifies the location of the `Dockerfile`. You can use `.` to refer to 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:
|
||||||
|
|
||||||
|
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.
|
||||||
@@ -62,3 +62,41 @@
|
|||||||
| **511** | Server Errors (5xx) | Network Authentication Required: Client must authenticate to gain network access. |
|
| **511** | Server Errors (5xx) | Network Authentication Required: Client must authenticate to gain network access. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
# Docker Image Layers
|
||||||
|
|
||||||
|
A **Docker image** is composed of multiple layers that work together to create a fully functional container. Each layer represents a step in the build process, and layers are stacked on top of one another to form the complete image.
|
||||||
|
|
||||||
|
### Structure of a Docker Image:
|
||||||
|
|
||||||
|
1. **BootFS (Boot File System):**
|
||||||
|
- **Description:** This is the bottom-most layer in the Docker image. It includes files and directories needed to boot up a system.
|
||||||
|
- **Function:** It sets up the foundation for the base operating system within the container, similar to the host machine’s `/boot` folder.
|
||||||
|
|
||||||
|
2. **Base Image:**
|
||||||
|
- **Description:** The base image is typically a minimal operating system (e.g., Ubuntu, Alpine Linux) or any other image that acts as a starting point for your container.
|
||||||
|
- **Examples:** Ubuntu, Alpine, Debian.
|
||||||
|
- **Function:** Provides the core OS functionalities and dependencies needed for the higher layers.
|
||||||
|
|
||||||
|
3. **Libraries:**
|
||||||
|
- **Description:** Libraries required by the applications running in the container.
|
||||||
|
- **Examples:** libc, libssl, or any other standard libraries needed by the applications.
|
||||||
|
- **Function:** Provides necessary functionality for applications, ensuring they can function correctly within the container.
|
||||||
|
|
||||||
|
4. **Packages and Applications:**
|
||||||
|
- **Description:** Specific software, tools, or libraries that your application depends on.
|
||||||
|
- **Examples:** vim, curl, git, node.js, or custom software required by your application.
|
||||||
|
- **Function:** These packages allow you to run applications and scripts necessary for the container's purpose.
|
||||||
|
|
||||||
|
5. **User Application (Optional):**
|
||||||
|
- **Description:** The main application code that you intend to run within the container.
|
||||||
|
- **Examples:** A web server like Apache, Nginx, or any microservice application.
|
||||||
|
- **Function:** It is the purpose of the container, which could be serving web traffic, processing data, or any other specific task.
|
||||||
|
|
||||||
|
### Writable Layer (Container-Specific):
|
||||||
|
|
||||||
|
- **Description:** Once a container is created from a Docker image, a writable layer is added on top of the image layers.
|
||||||
|
- **Function:** Any changes made during the container's runtime (like creating files or modifying configurations) are stored in this writable layer.
|
||||||
|
- **Key Point:** Changes to the writable layer do not impact the underlying image layers.
|
||||||
|
|
||||||
|
---
|
||||||
|
|||||||
Reference in New Issue
Block a user