In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use DockerFile to customize your own image". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to customize your own image with DockerFile.
Dockerfile
Before that, we need to know dockerfile, because we create the image through dockerfile. So what is dockerfile? Dockerfile is a file in which we write instructions and then use the docker build command to build an image. The difficulty now is how to write this instruction, so let's take a look at the dockfile instruction next.
The dockerfile directive FROMFROM FROM: # tag is optional. The base image of the latest version is used by default.
The from instruction is the basic image that depends on. The so-called custom image is to add something of our own to other images and customize them into our own images. Of course, we can also build it from scratch without relying on any images. Then use it.
FROM scratch
Scratch is actually a docker image, but this image is difficult to be special. It is a virtual image with nothing in it and a blank image, so if you want to not rely on any image, you can use from scratch.
So now there is another question, can there be multiple From in the dockfile file?
Multiple From is not supported before docker version 17.05. a dockefile can have only one From instruction and must be placed on the first line of the file. Because it is used as a basic mirror. Multi-From is supported after docker17.05. Represents the multiple phases of the build, but the resulting image is based on the last From base image.
RUN
The run instruction is an instruction that indicates that it runs when the image is built. There are two formats:
# shell format run eg: run apt-get update#exec format run ["executable file", "parameter 1", "parameter 2".] COPY
Instructions for copying files
Copy source path destination path # supports wildcard eg:copy hom?.txt / mydir/ADD
Add is a more advanced replication. It uses all the functions of copy, it can also access network resources, and the source path can be a URL. The source path file can also be a compressed file that can be extracted directly. So if you want to copy a compressed package directly into it, you should use COPY instead of just ADD. It is officially recommended to use COPY if you can use COPY, because the semantics of the COPY command is to copy files, and the ADD instruction will invalidate the image construction cache, making image construction slow.
CMD
The cmd instruction represents the instruction that is executed when the container is run.
# shell format cmd eg:cmd echo $HOME#exec format cmd ["executable file", "parameter 1", "parameter 2"] eg:cmd ["sh", "- c", "echo $HOME"] ENTRYPOINT
Entrypoint entry point
Entrypoint entrypoint ["executable", "parameter 1", "parameter 2"]
The function of the entryPoint instruction is similar to that of the cmd instruction, but entrypoint allows the image to be used like a command and can be used to prepare the application before running. I'll talk about this later in detail.
ENV
Env is an instruction to set environment variables
Env MY_VERSION 1.0.0ARG
Parameters passed by arg at build time
Arg [=] eg: arg versionarg myversion=1.0.0VOLUME
Define Anonymous Volume
Volume volume [", ["...] eg: volume / etc/docker/logEXPOSE
Declare port
Expose [...]
It should be noted here that expose declares the container application port, but the container operation does not necessarily open this port to provide services. Writing a port declaration in dockerfile has two advantages: one is that it is used as a daemon port for mirroring services to facilitate mapping, and the other is that when random port mapping is used at run time, it will be mapped on the port set by expose.
Well, of course there are more than these instructions. Check out what you want to know more:
Https://docs.docker.com/engine/reference/builder/
Simple test
In the middle of this article, I put it down, because there is a problem with the docker, the download image has been prompted to time out, and then set the domestic acceleration to get it done. We've learned about the Dockerfile instructions above, so let's do a simple test first. Let's wear a springboot project. Create a HelloController class.
@ RestControllerpublic class HelloController {@ RequestMapping ("/") public String hello () {return "hello docke my simple Test";}}
Then pack it into a jar bag. Put it under the folder on our server. And create a Dockerfile file under the file
Vim Dcokerfile# file content FROM java:8VOLUME / tmpADD hello-1.0.0.jar hello-1.0.0.jarENTRYPOINT ["java", "- jar", "/ hello-1.0.0.jar"]
You can see that the commands used are all introduced above. Java8 as the base image, / tmp as the data volume, add adds the local jar package to the image, and entrypoint runs our jar package.
Build the image in this directory now, don't forget the last point of the face.
Docker build-t helle:v1.
You can see that our image is divided into four without building, that is, building four images, because we have four instructions in Dockerfile. As mentioned earlier, the latter instruction builds the mirror on the basis of the previous instruction. So the first three of the four mirrors are the middle images.
Let's now look at the mirror we created.
Let's start the mirror next.
Docker run-d-p 8090 8080 hello:v1
Where-d is the background startup,-p is the mapping port, the front is the port we set, and the back is the port where the project is running. After startup, we visit it on the browser.
Http://192.168.252.53:8090
So we build our springboot project through docker and create our own images.
Configure docker remote access
What we need to do now is to generate a docker image directly through idea packaging. So, the first step is to turn on remote access to docker, and my docker is installed on the server. I first check locally to see if the docker on the server is enabled for remote access.
Docker-H 192.168.252.53 info
Indicates that the remote service of docker is not enabled. So access the server. Do the following in docker.service. Create a http-proxy.conf file under the folder.
Sudo mkdir / etc/systemd/system/docker.service.dsudo vim / etc/systemd/system/docker.service.d/http-proxy.conf
File content
[Service] ExecStart=ExecStart=/usr/bin/dockerd-H tcp://0.0.0.0:2375-H unix:///var/run/docker.sock
Then restart daemon and docker
Sudo systemctl daemon-reloadsudo systemctl restart docker
And then we'll test it locally. Indicates that remote access to docker has been configured.
Idea configuration
We open our hello project and add configuration to pom.xml
1.8 quellanan
Added in build.
Com.spotify docker-maven-plugin 1.0.0 build-image package build ${docker.image.prefix} / ${project.artifactId} ${project.version} Src/main/docker http://192.168.252.53:2375 / $ {project.build.directory} ${project.build.finalName} .jar
Create a Dockerfile file in src/main/docker with the same Dockerfile content as above
FROM java:8VOLUME / tmpADD hello-1.0.0.jar hello-1.0.0.jarENTRYPOINT ["java", "- jar", "/ hello-1.0.0.jar"]
Mvn package
Because we configure docker packaging at build time. So our bosom friend runs mvn package
The console view was packaged successfully. Let's go to the server and see if there is any
You can see that it has been successful.
At this point, I believe you have a deeper understanding of "how to customize your own image with DockerFile". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.