From building applications, installing dependencies and services, to automated deployment, and more - it all starts with Dockerfile. Let's review the syntax of Dockerfile, from basic to complexity, and some best practices when building Docker images.
This guide will write a Dockerfile that guides Docker to select a minimized Linux (base image) for the applications we will deliver, and comes with our selected toolset and specific configuration to effectively build our own Linux distribution , this distribution is just right suitable for running our applications.
- Docker enables users to “build, deliver and run any application, no matter where it is” by packing the application with all its necessary binaries, runtime libraries, backend tools, operating system tweaks and services. , thus enabling them to be delivered immediately and deployed automatically.
- Dockerfile is a plain text file containing instructions for building images in Docker, which is the starting point for building applications, installing dependencies and services, and automated deployment. The syntax of Dockerfile is similar to that of Apache configuration files, and is processed one instruction after another.
- Dockerfile instructs Docker to select a base image (e.g., minimized Linux) for the application to be delivered, and comes with a set of selected tools and specific configurations, effectively creating a custom Linux distribution suitable for running the application. .
- Dockerfile allows you to select and install specific tools and libraries, set environment variables, add files and directories, handle cache issues, and more to ensure that the container has everything you need.
- The Docker build command is used to build images based on instructions in the Dockerfile, while the docker run command is used to start an interactive terminal with created images.
- Why choose Docker
With Docker you can "build, deliver, and run any application, no matter where you are." That is, you can package your application with all the binary and runtime libraries, backend tools, operating system tuning and even specific services required for the application to run – and enable it to be delivered and deployed automatically .
Docker implemented software container technology makes this possible. Although I won't go into the details behind it here, you can read more about Docker, what software containers are and how they work in Understanding Docker, Containers, and Safer Software Delivery.
Installing Docker
Before you start, you need to install Docker, whether it is on the local machine or on a remote server.
Luckily, the latest version of Docker (version 1.12 as of this writing) makes the installation process very smooth, and you can get a simple and easy-to-understand guide for Windows, MacOS, and Linux.
Dockerfile
To build the image in Docker, you first need to set the directive for this build and the context in a plain text file called Dockerfile (more on this later). This file has a syntax similar to the Apache configuration file—one instruction per line and its corresponding parameters, all instructions are processed in sequence. Comments begin with # characters and spaces. Finally, once you have the Dockerfile, the command docker build will build the image, which we will cover in more detail later.
Before we start writing the Dockerfile, we will set up the workspace. We will create a directory called my_image in the home directory, use it as our working directory, and place the Dockerfile in it:
mkdir ~/my_build cd ~/my_build touch Dockerfile
Now we are ready to start building the image.
Select basic mirror
When creating an image, in most cases you will use one starting point—i.e. another image. This could be the official Ubuntu, MySQL, WordPress, or any other image available in Docker Hub. You can also use the images you created yourself before.
Note: You can create your own basic image using Docker's minimum image (called scratch) that contains your own core tools and directory structure. I won't cover this process here, but you can refer to the guide on creating basic images on the Docker website.
For example, if you want to start with a minimized Debian distribution, you will add the following to your Dockerfile:
# 设置基础镜像 FROM debian
FROM must be the first instruction you use when writing a Dockerfile. Note that you can also use a specific version of the base image by appending: and version_name at the end of the image name. For example:
# 设置基础镜像 FROM debian:sid
In the above code, we are using "sid" Debian (unstable distribution). This is also important when you need a specific version of Ruby or Python interpreter, MySQL version, etc. when you use the official basic image of any of these tools. Currently, in this guide, we will stick to the default (stable) debian image.
Specify maintainer and add metadata
You can choose to specify who the maintainer is, replace Lucero del Alba with your name or person or team responsible for the construction:
# 作者 MAINTAINER Lucero del Alba
This is not required, but we can also add some metadata using the LABEL directive, which will be available later when checking the image using the docker inspect command:
# 额外的元数据 LABEL version="1.0" LABEL description="First image with Dockerfile."
For more information about this feature, see the Docker Object Tag.
Create your own distribution
At this point, we will select some tools and libraries to include in the mirror so that our container has everything we need to perform the expected action. At the end of this tutorial, we will do a job that is very close to actually building a Linux distribution.
Some containers (such as those running PostgreSQL databases) are designed to run in the background. However, we often need a console to perform certain operations on the container, so we may need some extra tools because the underlying image will only bundle the minimum GNU tools.
Troubleshooting cache issues
You will almost certainly have caching issues when trying to install additional packages on your image. This is because the underlying image has cached metadata and the real-time repository from which you extract data changes frequently.
In a Debian-based distribution, you can add the following command before installing a new package to handle this:
mkdir ~/my_build cd ~/my_build touch Dockerfile
Installing basic tools
Tools like code editor, locale, git or tmux - now is the time to install everything you need afterwards so they are bundled in the image.
We will install one per line:
# 设置基础镜像 FROM debian
We can install all of this in one line, but if we want to add or remove packages later, we need to rerun the whole process. So the best practice here is to install one package per line so that you can benefit from Docker's cache.
In addition, keep it simple. You don't want to install the tool "just in case" as this may increase build time and image size.
Install the runtime library for the application
We will also deliver our application in this image. Do you need a specific version of PHP, Ruby or Python, and some modules? Now is the time to deliver all the programs and runtimes needed to deliver the application.
You can specify as you like, as this container is designed to run your application only:
In this example, we will install Python 3 along with Psycopg 2 (for connecting to PostgreSQL database), Python Mustache module, and YAML module. (When you create your own Dockerfile, you will naturally install the specific dependencies you need.)# 设置基础镜像 FROM debian:sid
Compilation and download package
Your distribution may also not have packages for the specific modules or programs you need. But you don't need to install it manually in the running container! Instead, you can use the RUN directive (one per line) to batch download, compile, and set up any libraries you need for your application.
You can even script this file on a separate file, add this file to the build and run it, which we will see in the Delivery Your Own Application section.
Cleaning
To make your mirror neat and as small as possible, it is best to clean up at the end of the installation sequence
:Also, please note that we are using apt-get because we chose Debian, but please use the corresponding command for the distribution of the base image.
# 作者 MAINTAINER Lucero del AlbaDeliver your own application
The whole purpose of building this environment is to enable you to deliver your application smoothly and get ready to run. To add the contents of files, directories, and even remote URLs to the image, we will use the ADD directive.
However, before adding files, we need to put them in the appropriate
context. To simplify operations, we put everything in the aforementioned my_build directory, along with the Dockerfile itself.
Assuming using the app and everything we want to put into the image, we have the following files in ~/my_build (where app.py and lib.py are in the subdirectory app/):
mkdir ~/my_build cd ~/my_build touch Dockerfile
We add the .bashrc and .profile scripts to the /root directory in the container so that they execute when we start the shell on the container and copy the contents of app/ to the /app/ directory in the container.
We add the following command:
# 设置基础镜像 FROM debian
Set your environment
Finally, we will set some environment variables that are needed at both the system and application level.
Many of you use the default Debian character set to do it, but since we are targeting an international audience, let's see how to have a UTF-8 terminal. We installed the locales package before, so now all we have to do is generate the character set and set up the appropriate Linux environment:
# 设置基础镜像 FROM debian:sid
You may also need to set some environment variables for the application to exchange passwords and paths. Dockerfile provides ENV instructions to do this accurately:
# 作者 MAINTAINER Lucero del Alba
Note that you can also pass environment variables from the command line when starting the container, which may be convenient for sharing some sensitive information (such as passwords).
Full Dockerfile
Of course, you have to adjust the Dockerfile as you want, but hopefully you understand its possibilities.
This is the complete file:
# 额外的元数据 LABEL version="1.0" LABEL description="First image with Dockerfile."
Build a mirror
From inside the my_build directory, we will use the docker build command, passing the -t flag to "tag" the new image with the name, in this case my_image. . Indicates that the Dockerfile is in the current directory, and the so-called "context" - that is, the remaining files that may exist in that location:
# 更新源列表 RUN apt-get clean RUN apt-get update
This will generate a long output where each "step" is a directive in our Dockerfile. Here is a truncated output:
# 每行安装一个基本应用程序,以获得更好的缓存 RUN apt-get install -qy git RUN apt-get install -qy locales RUN apt-get install -qy nano RUN apt-get install -qy tmux RUN apt-get install -qy wget
List the mirror
We can use the docker images command to list our images:
# 安装应用程序运行时和模块 RUN apt-get install -qy python3 RUN apt-get install -qy python3-psycopg2 RUN apt-get install -qy python3-pystache RUN apt-get install -qy python3-yaml
This will output our newly created my_image and other basic images we downloaded:
# 清理 RUN apt-get -qy autoremove
…That's it, our mirror is ready to be delivered and run!
Start the container
Finally, in order to start our newly created interactive terminal, we will use the docker run command:
<code>.bashrc .profile app/app.py app/lib.py Dockerfile</code>
Next steps
I did not introduce all the possibilities of Dockerfile. In particular, I did not review how EXPOSE ports so that you can run services and even link containers between them; how HEALTHCHECK containers to verify that they are still working; even how to specify VOLUME to store and recover data from the host... and other useful features.
We will introduce these in future articles. Currently, you may want to check out the following resources.
From Docker website:
- Dockerfile reference
- Best Practice for Writing Dockerfile
From SitePoint:
- Understanding Docker, containers and safer software delivery
- Docker sub-channel
- All Docker-related articles
FAQs on how to build images using Dockerfile
What is the importance of building Docker images using Dockerfile?
Dockerfile is a text document containing all commands that the user can call on the command line to assemble the image. Using Dockerfile simplifies the process of building images in Docker. It allows you to automate the process, making it more efficient and less prone to errors. Dockerfile also provides clear, versioning documentation on how to build images, which makes it easier for other developers to understand your work and use or modify it.
How to optimize the build process using Dockerfile?
Dockerfile provides a variety of ways to optimize the build process. One of the most effective ways is to use multi-stage builds. This allows you to use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different foundation, and each instruction begins a new stage of construction. You can optionally copy artifacts from one stage to another, leaving everything you don't want to appear in the final image.
What are the best practices for writing Dockerfiles?
There are several best practices for writing Dockerfiles. First, you should avoid installing unnecessary packages to keep the mirror size small. Second, use multi-stage builds to optimize the build process. Third, each Dockerfile should represent a single application. If you have multiple applications, you should use multiple Dockerfiles. Finally, you should use the .dockerignore file to exclude files and directories that should not be included in the image.
How to debug Dockerfile?
Dockerfile can be debugged by building the image and running it with the shell command. If the build fails, Docker returns an error message that can help you identify the problem. You can also use the RUN command to execute commands that will help you debug your Dockerfile.
Can I use environment variables in Dockerfile?
Yes, you can use environment variables in your Dockerfile. The ENV directive sets the environment variable to this value. This value will be in the environment of all subsequent instructions in the build phase and can also be replaced inline in many instructions.
How to copy files from host to Docker image?
You can use the COPY directive to copy new files from the host to the Docker image. Copy files from source on the host to the destination in the Docker image.
How to expose a port in a Docker image?
You can use the EXPOSE directive to notify the Docker container to listen for a specified network port at runtime. However, this does not actually publish the port. To publish a port, you need to use the -p flag on the docker run command.
How to set up a working directory in a Docker image?
You can use the WORKDIR directive to set the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD directives in the Dockerfile.
How to run commands in Docker image?
You can use the RUN directive to run commands in a Docker image. This will execute any command on the new layer above the current image and submit the result.
How to specify default commands for Docker images?
You can use the CMD directive to provide default values for the executing container. These can include executable files, or executable files can be omitted, in which case you must specify the ENTRYPOINT directive.
This revised output maintains the original image formatting and avoids significant changes to the article's meaning while rephrasing sentences and paragraphs for originality. Remember to replace /uploads/20250218/...
with the actual image URLs.
The above is the detailed content of How to Build an Image with the Dockerfile. For more information, please follow other related articles on the PHP Chinese website!

This Go-based network vulnerability scanner efficiently identifies potential security weaknesses. It leverages Go's concurrency features for speed and includes service detection and vulnerability matching. Let's explore its capabilities and ethical

Website construction is just the first step: the importance of SEO and backlinks Building a website is just the first step to converting it into a valuable marketing asset. You need to do SEO optimization to improve the visibility of your website in search engines and attract potential customers. Backlinks are the key to improving your website rankings, and it shows Google and other search engines the authority and credibility of your website. Not all backlinks are beneficial: Identify and avoid harmful links Not all backlinks are beneficial. Harmful links can harm your ranking. Excellent free backlink checking tool monitors the source of links to your website and reminds you of harmful links. In addition, you can also analyze your competitors’ link strategies and learn from them. Free backlink checking tool: Your SEO intelligence officer

DeepSeek-R1 empowers Baidu Library and Netdisk: The perfect integration of deep thinking and action has quickly integrated into many platforms in just one month. With its bold strategic layout, Baidu integrates DeepSeek as a third-party model partner and integrates it into its ecosystem, which marks a major progress in its "big model search" ecological strategy. Baidu Search and Wenxin Intelligent Intelligent Platform are the first to connect to the deep search functions of DeepSeek and Wenxin big models, providing users with a free AI search experience. At the same time, the classic slogan of "You will know when you go to Baidu", and the new version of Baidu APP also integrates the capabilities of Wenxin's big model and DeepSeek, launching "AI search" and "wide network information refinement"

The rise of Chinese women's tech power in the field of AI: The story behind Honor's collaboration with DeepSeek women's contribution to the field of technology is becoming increasingly significant. Data from the Ministry of Science and Technology of China shows that the number of female science and technology workers is huge and shows unique social value sensitivity in the development of AI algorithms. This article will focus on Honor mobile phones and explore the strength of the female team behind it being the first to connect to the DeepSeek big model, showing how they can promote technological progress and reshape the value coordinate system of technological development. On February 8, 2024, Honor officially launched the DeepSeek-R1 full-blood version big model, becoming the first manufacturer in the Android camp to connect to DeepSeek, arousing enthusiastic response from users. Behind this success, female team members are making product decisions, technical breakthroughs and users

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.

Midea will soon release its first air conditioner equipped with a DeepSeek big model - Midea fresh and clean air machine T6. The press conference is scheduled to be held at 1:30 pm on March 1. This air conditioner is equipped with an advanced air intelligent driving system, which can intelligently adjust parameters such as temperature, humidity and wind speed according to the environment. More importantly, it integrates the DeepSeek big model and supports more than 400,000 AI voice commands. Midea's move has caused heated discussions in the industry, and is particularly concerned about the significance of combining white goods and large models. Unlike the simple temperature settings of traditional air conditioners, Midea fresh and clean air machine T6 can understand more complex and vague instructions and intelligently adjust humidity according to the home environment, significantly improving the user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software