Let's start to learn the knowledge points of DockerFile together
DcokerFile is a file used to build a docker image and is a command parameter script
General docker image construction steps:
1. Write a dockerfile file
2. docker build builds into a mirror
3. docker run to run the image
4. Docker push release image (we can publish to DockerHub or Alibaba Cloud)
Let’s take a look at the official mirror image
For example, we search ubuntu on DockerHub to see what the DockerFile of the official website looks like
https://hub.docker.com/_/ubuntu
Click on the link and we will enter the git repository, which is also DockerFile
We can see that there are only 3 lines of Docker commands, which are official DockerFiles made into mirrors, so this official ubuntu mirror is very simple, castrated version, not even the clear
command, ll
command
Many official mirror packages are very simple, and many functions are not available. We usually build our own mirrors to meet our various needs.
The build process of DockerFile
Officials can build mirrors, we can also build our own mirrors
Basic knowledge of
- Each reserved word (instruction) of DockerFile must be capitalized
- DockerFile script execution is executed in order
#
means comment- Each instruction will create and submit a new mirror layer, and submit
You can find such pictures on the Internet, you can see that the image is layer by layer, you can search for the instruction explanation in DockerFile on the browser
Dockerfile is development-oriented. When we are working on projects in the future, we will publish a mirror directly. If we deliver a mirror, we need to write a DockerFile file. This file is very simple!
We must master the Docker image, which has gradually become the standard for enterprise delivery.
The process of our learning is to first use other people’s things, and then to study how other people write, and then we also learn how to write and develop
E.g:
Let’s learn to use it first,
DockerImages: Build a production image through DockerFile, and finally release and run the product
Docker container: The container service is the server on which the image runs
Now we start to learn DockerFIle in detail: the build file defines all the steps, this is the source code
DockerFile instructions
The pictures come from the Internet, we explain one by one
- FROM
Basic mirroring, everything starts here
- MAINTAINER
Specify who wrote the mirror, write your name and email
- RUN
Commands that need to be run when the image is built
- ADD
Add some configuration, such as adding mysql compressed package, adding content
- WORKDIR
Mirrored working directory
- VOLUME
Mount directory
- EXPOSE
Exposing the port and -p
is an effect
- CMD
Specify the command to be executed when the container is started, only the best one will take effect and will be replaced
- ENTRYPOINT
Specify the command to be executed when the container is started, you can add
- ONBUILD
When constructing an inherited DockerFIle, the ONBUILD instruction will be executed at this time to trigger the corresponding action
- COPY
Similar to ADD, this command copies the file to the mirror
- ENV
Set environment variables when building
At first glance, the CMD
and ENTRYPOINT
seem to be similar, but it is still not clear where the specific differences are. There will be a detailed explanation at the end of the article.
Actual combat
Let's make a custom mirror of ubuntu
The official ubuntu
is a castrated version. Many tools and commands are not supported, so we will add it ourselves and be self-sufficient.
write a DockerFile
It should be noted here that basically 99%
is based on this basic image scratch
, we can see that the official DockerFIle is also based on this image to play
Then we can customize based on this ubuntu and add some tools we need, such as vim
, ifconfig
etc.
FROM ubuntu
RUN apt-get update # 更新源
RUN apt-get install -y vim # 安装 vim
RUN apt-get install -y net-tools # 安装 net-tools
ENV MYPATH /usr/local # 设置环境变量
WORKDIR $MYPATH # 设置镜像工作目录
EXPOSE 8888 # 暴露端口
CMD echo "----- end -----" # 执行 echo 命令
CMD /bin/bash
Start building
docker build -f dockerfile2 -t xmtubuntu .
If you do not write the apt-get update
update source in the DockerFile, the following problem will occur. Please pay attention to this
Execute the above command, you will see the following print information, and finally you will see Successfully
, which means the build is successful
From the above figure, we can see that there are 9 steps written in DokerFile, and it is also divided into 9 steps when executed. It is considered successful when all of them are successful.
After the final build is successful, we can see
Successfully built a6f88c9f245b
Successfully tagged xmtubuntu:latest
Validation results
docker images view our image
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xmtubuntu latest a6f88c9f245b 13 minutes ago 172MB
docker inspect a6f88c9f245b View the build process of our image
# docker history a6f88c9f245b
IMAGE CREATED CREATED BY SIZE COMMENT
a6f88c9f245b 14 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
3c0d23b8188f 14 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
ffb019142fc7 14 minutes ago /bin/sh -c #(nop) EXPOSE 8888 0B
8867e6d97670 14 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
c9d0141ec3b0 14 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
41e73f7e314d 14 minutes ago /bin/sh -c apt-get install -y net-tools 1.52MB
52013ca51f1d 14 minutes ago /bin/sh -c apt-get install -y vim 68.2MB
5ea7d553d403 14 minutes ago /bin/sh -c apt-get update 29.7MB
1318b700e415 11 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 11 days ago /bin/sh -c #(nop) ADD file:524e8d93ad65f08a0… 72.8MB
We can clearly see what steps have been performed during the construction of the xmtubuntu image. Of course, in the same way, we can also see how the official image is built. Let’s take a look at the official ubuntu
# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
1318b700e415 11 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 11 days ago /bin/sh -c #(nop) ADD file:524e8d93ad65f08a0… 72.8MB
The official one is very simple, castrated a lot of things, we can see that the official ubuntu has two steps, the first is to add the ubuntu
compressed package, the second is /bin/bash
We check our custom mirror xmtubuntu
Sure enough, our custom ubuntu mirror, with vim
and ifconfig
tools, was successful in actual combat
The difference between CMD and ENTRYPOINT
- CMD
Specify the command to be executed when the container is started, only the best one will take effect and will be replaced
- ENTRYPOINT
Specify the command to be executed when the container is started, you can add
How to understand it? Let's do a comparative experiment to understand the above explanation well. There are many commands in docker that have such minor differences. We can learn by analogy and learn more slowly.
CMD example
Write a simple DockerFile file named dockerfile-cmd
FROM xmtubuntu
CMD ["ls","-a"]
Build image
e# docker build -f dockerfile-cmd -t dockerfile-cmd .
Sending build context to Docker daemon 1.346GB
Step 1/2 : FROM xmtubuntu
---> a6f88c9f245b
Step 2/2 : CMD ["ls","-a"]
---> Running in 101670af4290
Removing intermediate container 101670af4290
---> 1697fc03b8ce
Successfully built 1697fc03b8ce
Successfully tagged dockerfile-cmd:latest
Create and start the container
docker run 101670af4290
, you can see the following effects
We try to append the command when starting the container
docker run 101670af4290 -l
, there will be the following error
# docker run 1697fc03b8ce -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
The reasons are as follows:
Using the CMD
instruction is (for example, our example is ls -a
). When we start the container, the additional command ( -l
) will ls -a
. Because -l
is not a command, an error is reported
example ENTRYPOINT of
Write a simple DockerFile file named dockerfile-entrypoint
FROM xmtubuntu
ENTRYPOINT ["ls","-a"]
Construction of a mirror, create and launch container and CMD
example of exactly the same effects we start direct container and CMD
example is exactly the same, we are direct examples of container and start to see additional parameters
It can be seen that using ENTRYPOINT
can add parameters later, using CMD
if adding parameters after the instruction, then it will overwrite the instruction specified by CMD
Then, for the relevant instructions in the future, we can also draw inferences and conduct comparative experiments, so that we will understand more clearly
How to publish our mirror
1. Log in to dockerhub
Unregistered xdm can register one, https://hub.docker.com/
# docker login -u xxxx用户名
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
2. Modify our mirror tag
docker tag Our image id Our docker username/image name: version
3. Push the image to our own warehouse
When publishing images, they are also submitted layer by layer.
Finally, add a picture found on the Internet. Now you can understand the principle more clearly by looking at this picture.
Reference materials:
Welcome to like, follow, favorite
Friends, your support and encouragement are my motivation to keep sharing and improve quality
Okay, that's it for this time
Technology is open, and our mindset should be more open. Embrace the change, live toward the sun, and work hard to move forward.
I’m Nezha , welcome to like and follow the collection, see you next time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。