This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
【Docker series 】 Docker learning 6, DockerFile
Let’s learn about DockerFile
DcokerFile is the file used to build the Docker image. It is a command parameter script
General Docker image building steps:
1, write a dockerfile file
The Docker build becomes an image
Docker run runs images
4, Docker push release image (we can publish to DockerHub, can also be published to Ali Cloud)
Let’s see how the official mirror works
For example, we can search ubuntu on DockerHub to see what DockerFile looks like on the official website
hub.docker.com/_/ubuntu
Clicking the link will take you to the Git repository, which is also a DockerFile
We see only 3 lines of Docker command, is the official image DockerFile, so this official Ubuntu image is very simple, castration version, even clear command is not, ll command is not
Many official image packages are very simple, many features are not, we usually build their own mirror to meet our various needs
DockerFile build process
Officials can build a mirror, and we can build our own
DockerFile Basics:
- The reserved word (directive) of each DockerFile must be uppercase
- DockerFile script execution is performed sequentially
#
Said annotation- Each directive creates and commits a new mirror layer and commits
You can find such images on the network, you can see that the image is layer by layer, you can search the instructions in the DockerFile in the browser
Dockerfile is oriented to development, we later in the project, is directly publish an image, delivery is an image, you need to write dockerfile file, this file is very simple!
We must master Docker image, gradually become the enterprise delivery standard.
The process of learning is that we will first use others’ things, and then study how others write, and then we also learn how to write, to develop
Such as:
So we learned how to use it,
DockerImages: Build production images from DockerFile, and finally release and run the product
Docker container: The container service is the server that the image is running on
Now let’s go into the details of the DockerFIle: the build file that defines all the steps, which is the source code
DockerFile instructions
Pictures from the Internet, we explain one by one wave
- FROM
The mirror image of the base. It all started here
- MAINTAINER
Identify who wrote the mirror, write down your name and email address
- RUN
Commands that need to be run when the image is built
- ADD
Add some configuration, such as add mysql compression package, add content
- WORKDIR
The working directory of the image
- VOLUME
Mount the directory
- EXPOSE
Expose port and -p are one effect
- CMD
The command specified when the container is started will only be the best one to take effect and will be replaced
- ENTRYPOINT
Specifies the command to be executed when the container is started, which can be appended
- ONBUILD
When an inherited DockerFIle is built, the ONBUILD directive is run, triggering the corresponding action
- COPY
Like ADD, this command copies files into the image
- ENV
Set environment variables at build time
At first glance, it looks like CMD and ENTRYPOINT have similar functionality, but it’s not clear what the differences are
In actual combat
Let’s make a custom Ubuntu image ourselves
The official Ubuntu version is a castration version, many tools and commands are not supported, so we just add them and feed ourselves
Write your own DockerFile
It is important to note that 99% of the images are based on this basic image Scratch, and we can see that the official DockerFIle is also based on this image to play
ubuntu git url
So we can customize it based on this Ubuntu, adding some tools we need, such as Vim, ifconfig, etc
FROM ubuntu
RUN apt-get update # update source
RUN apt-get install -y vim # install vim
RUN apt-get install -y net-tools # install.net - tools
ENV MYPATH /usr/local Set environment variables
WORKDIR $MYPATH # Set the working directory of the image
EXPOSE 8888 # Expose port
CMD echo "----- end -----" # Run the echo command
CMD /bin/bash
Copy the code
Begin to build
docker build -f dockerfile2 -t xmtubuntu .
If you do not write the apt-get update source in the DockerFile, the following problem will occur, this should be noted
After the preceding command is executed, the following information is displayed. If the message “Successfully” is displayed, the construction is successful
From the figure above, we can see that there are nine steps written in the DokerFile, and the execution is also divided into nine steps, until all successful
We can see that when the final build is successful
Successfully built a6f88c9f245b
Successfully tagged xmtubuntu:latest
Copy the code
The verification results
Docker images View our image
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xmtubuntu latest a6f88c9f245b 13 minutes ago 172MB
Copy the code
Docker inspect a6f88c9f245b to see how our image was built
# docker history a6f88c9f245bIMAGE 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: 524 e8d93ad65f08a0... 72.8 MBCopy the code
In the same way we can see how the official image is built, let’s take a look at the official Ubuntu image
# docker history ubuntuIMAGE 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.8 MBCopy the code
The first step is to add the Ubuntu compression package, and the second step is /bin/bash
Let’s look at our custom image, XMtubuntu
Sure enough, our custom Ubuntu image, with vim, ifConfig tools, combat success
CMD and ENTRYPOINT
- CMD
The command specified when the container is started will only be the best one to take effect and will be replaced
- ENTRYPOINT
Specifies the command to be executed when the container is started, which can be appended
How do you understand that? Let’s do a comparison experiment to understand the above explanation well. There are many commands in Docker that have such small differences. We can draw inferno from one example and gradually learn in depth
An example of CMD
Write a simple DockerFile file named DockerFile -cmd
FROM xmtubuntu
CMD ["ls"."-a"]
Copy the code
Build the mirror
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
Copy the code
Create and start the container
Docker Run 101670AF4290, you can see the following effect
We tried to append commands when starting the container
Docker run 101670af4290-l docker run 101670af4290-l
# 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.
Copy the code
Here’s why:
Using CMD (for example, ls -a in our case), we start the container, and then append the command (-l) to replace ls -a, because -l is not a command, so an error is reported
An example of ENTRYPOINT
Write a simple DockerFile file named dockerfile-entryPoint
FROM xmtubuntu
ENTRYPOINT ["ls"."-a"]
Copy the code
Creating the image, creating and starting the container is exactly the same as the CMD example. Starting the container directly has the same effect as the CMD example. Let’s start the container directly and append parameters
As you can see, if you use ENTRYPOINT, you can append parameters. If you use CMD to append parameters, you will overwrite the directive specified by CMD
So, for the relevant instructions in the future, we can also draw inferences from one example and do comparative tests, so that we can understand more clearly
How do WE publish our images
1. Log in to DockerHub
Unregistered XDM users can register one at hub.docker.com/
#Docker login -u XXXX User name
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
Copy the code
2. Modify our image tag
Docker tag Our image ID Our Docker username/image name: version
3. Push the mirror into our own warehouse
When publishing images, they are also committed layer by layer
Finally, add an image found on the Internet, and now you can see how it works more clearly
References:
docker docs
Welcome to like, follow and collect
Dear friends, your support and encouragement are the motivation for me to keep sharing and improve the quality
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am nezha, welcome to like the collection, see you next time ~