145 lines
4.7 KiB
Markdown
145 lines
4.7 KiB
Markdown
## 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.
|
||
|
||
---
|
||
|
||
## Step-by-Step Guide to Creating a Dockerfile
|
||
|
||
### 1. Create a File Named `Dockerfile`
|
||
|
||
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.
|
||
|
||
#### Example 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
|
||
```
|
||
|
||
#### 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. Example Using Alpine Linux
|
||
|
||
Alpine Linux is a lightweight option that results in smaller images. Here's an example of a Dockerfile using Alpine:
|
||
|
||
```dockerfile
|
||
# Use Alpine as the 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
|
||
```
|
||
|
||
This example is perfect for when you need 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.
|
||
|
||
```dockerfile
|
||
# Start with Alpine as the base image
|
||
FROM alpine
|
||
|
||
# Add metadata
|
||
LABEL version="0.0.1"
|
||
|
||
# Update package lists and install essential tools
|
||
RUN apk update && apk add bash vim curl iputils-ping
|
||
|
||
# Copy the script into the image
|
||
COPY <local-file-path> <container-destination-path>
|
||
|
||
# Set the working directory for subsequent commands
|
||
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
|
||
RUN chmod +x app.sh
|
||
|
||
# Define the default command to run
|
||
CMD ["./app.sh"]
|
||
# Alternatively, you can use ENTRYPOINT
|
||
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.
|
||
|
||
---
|
||
|
||
### 4. Build Your Docker Image
|
||
|
||
Once your `Dockerfile` is ready, build the Docker image using the `docker build` command.
|
||
|
||
```bash
|
||
docker build -t <app-name> <path-to-dockerfile>
|
||
```
|
||
|
||
#### Usage Examples:
|
||
|
||
- 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.
|
||
|
||
- If your file is named something other than `Dockerfile` (e.g., `CustomDockerfile`):
|
||
```bash
|
||
docker build -t app-test -f CustomDockerfile .
|
||
```
|
||
|
||
#### Explanation:
|
||
- **`docker build`**: Builds a Docker image.
|
||
- **`-t <app-name>`**: Tags the image with a name (e.g., `app-test`).
|
||
- **`<path-to-dockerfile>`**: Specifies the location of the `Dockerfile`. Use `.` for the current directory or provide an absolute path.
|
||
|
||
---
|
||
|
||
## Summary
|
||
|
||
A **Dockerfile** is a powerful tool for automating the creation of Docker images. Here’s a quick recap:
|
||
|
||
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.
|