Dockerfile creation Tutorial - Dockerfile Instructions Explained with example!
How to Create a Dockerfile: A Hands-On Guide
Introduction to Dockerfiles
- Docker images are constructed using instructions from a text file called a Dockerfile. This video aims to teach the proper syntax for creating a Dockerfile and explain various instructions through a hands-on demonstration.
- The speaker references a previous tutorial on building a JavaScript application, emphasizing the necessity of having a correctly named Dockerfile for image creation.
Naming and Syntax of Dockerfiles
- The official name for the file must be "Dockerfile" with an uppercase 'D'. Following this naming convention is recommended by the official documentation.
- Comments in the Dockerfile can be added using the hash symbol (#), similar to Python or other programming languages.
Structure and Instructions in Dockerfiles
- The format of a Dockerfile consists of an instruction followed by its argument. While not case-sensitive, it is advised to use capital letters for instructions for clarity.
- Various instructions are available in Dockerfiles that dictate how to create the desired image, with "FROM" being one of the first and most crucial commands used.
Specifying Base Images
- The "FROM" instruction defines which base image will be utilized when creating your own image. Users can find different images on platforms like Docker Hub.
- For example, if an application requires Python, you can specify it as your base image by using
FROM python. Specific versions can also be indicated (e.g.,FROM python:3.11).
Exploring Base Images on Docker Hub
- Each base image is created using its own set of instructions within another Dockerfile. Users can explore these details on their respective pages on Docker Hub.
- In this demonstration, Alpine 3.18 is chosen as the base image with
FROM alpine:3.18, showcasing how users can select lightweight images.
Building and Verifying Images
- After defining the base image in your Dockerfile, you build your custom image using the command
docker build -t my_image ..
- Upon successful execution, this command creates an image consisting of layers based on provided instructions; currently only one layer exists due to one instruction.
Checking Operating System Inside Image
- To verify what operating system is running inside your newly created containerized environment, you run
cat /etc/os-release.
- Executing this command within your container confirms that Alpine Linux 3.18 was successfully used as specified in the FROM instruction.
Dockerfile Instructions Overview
Base Image Specification
- The
FROMinstruction allows you to specify the base image for your Docker image, which can be pulled from various container registries like Docker Hub, GCR, or ECR.
- You can use multiple
FROMinstructions in a single Dockerfile to create either multiple images or a multistage Docker image. Each stage can be named using theASkeyword.
Running Commands in Docker
- The
RUNinstruction executes commands during the image build process. For example, you can install packages on an Alpine image using this command.
- There are two formats for the
RUNcommand: shell form and exec form. Shell form does not require specifying the shell, while exec form does (e.g.,/bin/shfor Linux).
- To install curl on an Alpine image, you would use
RUN apk add curl, similar to how you'd useapt install curlin Ubuntu.
Layer Management
- After executing a command with
RUN, it creates a new layer in the Docker image. You can view these layers by runningdocker history my_image.
- The output shows all executed layers; each command contributes to building up the final image.
Setting Working Directory
- The
WORKDIRinstruction sets the working directory for subsequent commands like ADD, COPY, CMD, and ENTRYPOINT.
- If you set a working directory that doesn't exist (e.g.,
/downloads), it will automatically create that directory when building the image.
User Configuration
- The
USERinstruction specifies which user should be used for subsequent commands within the Dockerfile. By default, this is set to root.
Creating and Managing Users in Docker Containers
Adding a User to a Container
- The
RUNcommand is utilized to execute commands on top of the image, allowing for user creation within the container.
- A new user named "cloud champ" is created, and this user will be set as the default for subsequent instructions using the
USERinstruction.
- In Windows, users can be created with the
net user addcommand, demonstrating cross-platform compatibility in Docker.
Building a Windows Image
- The base image is set to PowerShell from Microsoft, which can be found on Docker Hub.
- Two
RUNcommands are included: one creates a folder named/demo, while another writes "Hello World" into a text file inside that folder.
- An error occurs during the build process due to an attempt to use Linux commands in a Windows environment.
Changing Shell Instructions
- To resolve shell-related errors, the
SHELLinstruction can specify which shell (e.g., PowerShell or CMD) should be used for executing commands.
- The default shell (
/bin/sh) is replaced with PowerShell by adding aSHELLinstruction before running subsequent commands.
- After correcting the shell type to "pwsh," no further errors occur during the build process.
Verifying Command Execution
- Upon successful execution of previous commands, checking the contents of "demo/message.txt" confirms that "Hello World" was written correctly.
Setting Environment Variables
- Environment variables are crucial for application configuration and can be set using the
ENVinstruction in Dockerfiles.
- Multiple environment variables can be defined either individually or combined into single instructions using backslashes for line continuation.
Testing Environment Variables
- After building an image with three environment variables (app host, app port, ABC), running it allows verification of these variables through an
ENVcommand.
How to Use Docker Instructions for File Management
Copying Files into Docker Containers
- The process of setting environment variables in Docker containers is discussed, emphasizing that a Docker container includes code, dependencies, and libraries.
- To copy files from the local machine to a Docker container, the
COPYinstruction is used. The source is specified as the local file path and the destination as a directory within the container.
- After building the image with
Docker build, verification can be done by running the container and listing files in the target directory to confirm successful copying ofapp.py.
Using ADD Instruction for Remote Files
- The
ADDinstruction serves a similar purpose toCOPY, but it also allows copying files from remote locations (e.g., S3 buckets).
- An example demonstrates using
ADDto retrieve a file namedtoken.txtfrom an S3 bucket into the container during image creation.
- After building with
ADD, checking the contents of/c/codeconfirms bothapp.pyandtoken.txtare present.
Exposing Ports in Docker
- The
EXPOSEinstruction is introduced, which defines network ports that will be open for inbound traffic. For instance, exposing port 5000 allows external access to services running on that port.
- Building an image with an exposed port can be verified using the command
docker inspect, which shows that port 5000 is indeed open.
Running Applications with ENTRYPOINT and CMD
- The importance of defining either an
ENTRYPOINTor aCMDin a Dockerfile is highlighted; these instructions dictate what processes run inside the container.
- A practical example illustrates how both commands can be utilized: while one specifies executable parameters directly, another can define them separately through CMD.
Testing Container Functionality
- A demonstration runs through creating an image based on defined instructions and executing it on specific ports (e.g., localhost:880).
Understanding Docker CMD and ENTRYPOINT
Overview of CMD and ENTRYPOINT
- The speaker explains how to override the default sleep duration in a Docker container. By specifying a different value (e.g., 10 seconds instead of 5), users can control the execution time when running the container.
- To clarify the differences between CMD and ENTRYPOINT, viewers are encouraged to consult official documentation for deeper insights. Additional resources include Stack Overflow answers and YouTube videos for further understanding.
Importance of Labels in Docker Images
- Labels provide metadata about a Docker image, such as versioning or creator information. This helps maintain clarity regarding who created the image and its purpose.