In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Dockerfile introduction
Docker automatically builds image through sequential parsing of a series of instructions in Dockerfile.
Build the image according to the description of Dockerfile by using the build command.
Dockerfile instruction
only supports a set of instructions defined by Docker itself, not custom
is not case-sensitive, but it is recommended to use all uppercase
executes according to the content order of Dockerfile
FROM instruction
FROM {base Image}
The must be placed on the first line of the DOckerfile, indicating which baseimage to start building from
MAINTAINER
MAINTAINER: xxx
Optional, used to identify the image author
RUN
each RUN instruction will be run in a new container and submitted as an image as the base of the next RUN
can contain multiple RUN in a Dockerfile, executed in defined order
RUN supports two modes of operation:
RUN this will run as / bin/sh-c "cmd"
RUN ["executable", "arg1".] Docker parses it as the order of json, so double quotation marks must be used, and executable needs to be the full path
RUN starts a container, executes commands, and then commits changes to the storage layer files. The execution of the first layer of RUN command1 is only the current process, a change in memory, and the result does not result in any files. When you get to the second layer, you start a brand new container, which has nothing to do with the container in the first layer, so it is naturally impossible to inherit the memory changes in the previous layer. If you need to execute two or more commands together, you need to add & &. Such as: cd / usr/local/src & & wget xxxxxxx
ADD & COPY
When is built in source code, files or directories on host can be copied to image by means of ADD and COPY
The source of ADD and COPY must be under the context path
when src is a network URL, the ADD instruction can download it to the specified location of dest, which can be work in any build mode
ADD also has a more function than COPY, which can decompress the compressed package automatically.
ENV
ENV key value
is used to set environment variables, and subsequent RUN can use the environment variables created by it
When creates a container based on this image, it automatically has the set environment variable.
CMD
The role of CMD is to act as the default behavior when executing container (the default startup command for the container)
When declares command when running container, the commands defined by default in CMD in image are no longer used.
there can be only one valid CMD in a Dockerfile. When defining multiple CMD, only the last one will work.
There are three ways to define CMD:
CMD this will be executed as / bin/sh-c "cmd"
CMD ["executable", "arg1",....]
CMD ["arg1", "arg2"], when CMD is used as a parameter of ENTRYPOINT
Dockerfile example
Example 1: take the most commonly used tomcat as an example
1234FROM tomcat:latestENV TZ=Asia/ShanghaiRUN rm / usr/local/tomcat/webapps-rfCOPY. / webapps/ / usr/local/tomcat/webapps/
The example explains:
The basic image is tomcat:latest. If the basic image exists locally, the local image is used. If it does not exist, the image is automatically pulled from the official image repository. Check the image: docker images
ENV setting time zone, Asia / Shanghai
RUN runs the command to clear the contents of tomcat's own webapps directory
COPY copies the webapps directory in the current directory to the image to generate the new image we need.
Here are a few things to pay attention to.
The difference between rm / usr/local/tomcat/webapps-rf and rm / usr/local/tomcat/webapps/*-rf
Rm / usr/local/tomcat/webapps-rf means to delete the entire webapps directory, that is, the webapps directory is gone
Rm / usr/local/tomcat/webapps/*-rf means to delete something in the webapps directory, that is, the webapps directory is still there, but the contents are gone.
Where do I put the war package or the front-end package of the program?
This needs to be determined in combination with the writing of the rm statement above. If it says rm / usr/local/tomcat/webapps-rf, that is, there is no webapps in the container, then when we COPY or ADD packages, we will use the
Instructions such as COPY. / webapps/ / usr/local/tomcat/webapps/ insert a webapps into the container COPY, and the package in this case is of course placed in the webapps directory of the current directory.
If you are using instructions such as COPY. / webapps/ / usr/local/tomcat/webapps/*-rf, you can put the war package or front-end package directly into the Dockerfile sibling directory. That is, it is like this:
12345FROM tomcat:latestENV TZ=Asia/ShanghaiRUN rm / usr/local/tomcat/webapps/*-rfCOPY * .war / usr/local/tomcat/webapps/COPY build / usr/local/tomcat/webapps/
Example 2: the initial tomcat image is not enough for us to use, we need to download something, download some plug-ins.
1234FROM tomcat:latestENV TZ=Asia/ShanghaiRUN rm / usr/local/tomcat/webapps-rf & & apt-get update & & apt-get install-y ffmpeg & & apt-get cleanCOPY. / webapps/ / usr/local/tomcat/webapps/
The example explains:
This example adds one more step to downloading the program, which can be done with & & using a RUN command (also explained in the RUN instruction). Here is the download of the ffmpeg plugin.
It should be noted that the release version of the image is different with different initial images, that is, the release version is not necessarily an image of the centos version. The tomcat here is made using debain images, so you have to refer to the method of downloading the package of debain. How to download the package of debain,ubuntu can be done on Baidu on the Internet.
How can you tell if it's the centos release, debain, or ubuntu? You can view the mirror official document or run an initial image
Docker run-it tomcat:latest bash runs a base image
If cat / etc/debian_version is a debian system, you can view the debian version if you have this file in the / etc/ directory
If it is a centos system, there is a cat / etc/redhat-release, and ubuntu also has its own corresponding logo.
Example 3: add a CMD instruction as an instruction to start the program
For the above tomcat image, since the CMD startup instruction has been made in the initial image, here I will replace it with an example of the image of the basic platform. (for tomcat image, you can also write another CMD yourself, which will overwrite the startup instructions of the basic image)
1234567 [root@localhost app] # cat DockerfileFROM microsoft/dotnet:2.2-aspnetcore-runtimeRUN ln-s / lib/x86_64-linux-gnu/libdl-2.24.so / lib/x86_64-linux-gnu/libdl.soRUN apt-get update & & apt-get install-y net-tools procps lsof sysstat libfontconfig1 libgdiplus & & ln-s libgdiplus.so gdiplus.dll#ENV LANG C.UTF-8COPY. / JF-ISBM/ etc/JF-ISBMCMD ["sh", "- c", "cd / etc/JF-ISBM/ Dotnet / etc/JF-ISBM/JF-ISBM2.0WEBAPI.dll "]
Example explanation: the basic image is the dotnet2.2 image of Microsoft Image Repository.
RUN downloads dependency packages required by the basic platform
COPY copies the underlying platform programs of the current directory to the image
CMD sets the startup command for the base platform.
It should be noted that when setting up the program to start the command, it needs to be run in the foreground, not in the background. For example, if tomcat is set to start in startup.sh mode, it will be started in the background, and the container will exit automatically after running. As a solution, you can use catalina.sh startup, or add a sentence, tail-f / usr/local/tomcat/logs/catalina.out, after startup.sh startup, so that the container foreground has continuous output.
Docker builds an image
Docker build-t IMAGENAME:TAG.
Docker-compose introduction
Docker-compose is a container management method. When we start a container in a production environment, we not only use the-p parameter to map the container port, but also use many parameters, such as-v mapping directory,-- restart to specify the container restart method, and so on. As a result, a startup naming will be very long and inconvenient to use.
In addition, using the docker run command directly can only manage one container at a time. Docker-compose is a very convenient docker container stand-alone scripting.
The operation of docker-compose is based on the docker-compose.yml file, which is a yaml-style key-value file.
123456789101112131415161718192021222324252627282930313233343536version: "3" # docker-compose version. According to the docker-compose version, if the version is incorrect, an error will be reported at runtime indicating the version that should be used. Services: # defines the service jf-isgct-web: # service name, customized. The first service name defined here is jf-isgct-web build:. / web # build, specify the location of the Dockerfile, and enable this parameter restart: always # restart when you need to build an image. Container restart policy image: jf-isgct-web:v2-20190901 # the image name of the running container. If the image is pulled or needs to be uploaded to the private image repository, you need to add the private image repository prefix in the later deployment document. If the build parameter is added earlier, this is also the name of the newly built image, so each time you update and rebuild the image, you need to modify the version number to distinguish between the new image and the old image to facilitate rollback. When you roll back, you only need to change it to the old image version. Privileged: true # Container privilege mode container_name: jf-isgct-web # specify container name # mem_limit: 1g # memory limit # cpus: 1 # cpu limit ports: # Container exposure port, host port before colon, in-container service port after colon-6199 TZ=Asia/Shanghai links 6190 environment: # environment variable, multiple groups can be added, starting with-container: # associate the container, if there is a container to be associated You can add this parameter. If not, do not add-jf-isgct:backend depends_on: # relying on the container, that is, the container under this parameter will not start this service container until it is started. Add-jf-isgct# volumes: # container directory mapping according to the requirements. The directory mapped to the host before the colon can be a relative path. The colon is followed by the directory # -. / logs:/usr/local/tomcat/logs command that needs to be mapped in the container: "sh-c'cd / front-end. Npm run start' "# is similar to CMD in Dockerfile. Add a service startup command in the container. Only one command can appear for a service, and you also need the foreground to start jf-isgct: # the second service, which can write multiple services in a docker-compose.yml. You can also have a separate # build:. / backend image: 1.xxx.xxx.xxx/zhxy/jf-isgct:v2.0-20180830 restart: always container_name: jf-isgct# mem_limit: 1g # memory limit # # cpus: 1.0ports:-12216pur8080 volumes: -. / config.txt:/usr/share/JFConfig/config.txt -. / logs:/usr/local/tomcat/logs-/ etc/localtime:/etc/localtime:ro command : "sh-c'/ usr/local/tomcat/bin/startup.sh Tail-f / usr/local/tomcat/logs/catalina.out' "
The docker-compose command uses:
Docker-compose up-d run container
Docker-compose down closes the container
Docker-compose up-d-- build builds a new image and runs a container with the new image
Docker-compose build only builds new images
Docker-compose restart restart the container
It is important to note that running the docker-compose command needs to be in the directory where docker-compose.yml is stored.
Docker image update
There may be updates when we build the image. Here are two ways to update the image.
Update the image built directly with the official initial image
The initial image of this method is the official image, that is, the FROM image is the official image, such as tomcat:latest, node:10.16, microsoft/dotnet:2.2-aspnetcore-runtime, etc., are all official basic images. All the corporate business images I do are built in this way. Here, take the dotnet image of the basic platform as an example.
123456FROM microsoft/dotnet:2.2-aspnetcore-runtimeRUN ln-s / lib/x86_64-linux-gnu/libdl-2.24.so / lib/x86_64-linux-gnu/libdl.soRUN apt-get update & & apt-get install-y net-tools procps lsof sysstat libfontconfig1 libgdiplus & & ln-s libgdiplus.so gdiplus.dll#ENV LANG C.UTF-8COPY. / JF-ISBM/ etc/JF-ISBMCMD ["sh", "- c", "cd / etc/JF-ISBM/" Dotnet / etc/JF-ISBM/JF-ISBM2.0WEBAPI.dll "]
Dockerfile details:
The initial image here is microsoft/dotnet:2.2-aspnetcore-runtime officially downloaded by Microsoft.
The two RUN commands are the dependent packages and some tools needed for the basic image.
The COPY command is to copy the underlying platform program JF-ISBM directory of the same level directory as Dockerfile to the image
CMD dotnet starts the basic platform
The directories I put here for Dockerfile and JF-ISBM are: / mdata/jf-isbm/app; mdata standardized deployment directory, jf-isbm, basic product directory, app, program placement directory. If it is a tomcat program, according to the contents of Dockerfile, there may be a webapps directory under the app directory, which is determined according to different Dockerfile, as explained above.
If you need to update the program, you just need to replace the old base package with the new one, and then rerun the command to build the image.
Before rebuilding the image, you need to change the version number of the image field in the docker-compose.yml file. This benefit is also obvious. If the new image is not suitable for running online, you only need to change the version number of the image field in the docker-compose.yml file to the old version number and rerun the container.
The image pulled from the image warehouse needs to be updated.
Normally, if there is a program update, I will make a new image and push it back to the image warehouse, and you can pull the new image directly. However, if you encounter special circumstances and the image of the image repository has not been updated, then you need to make an image update in the deployment environment.
Since the old version of the image we pulled from the image repository already exists in the deployment environment, we can update our image by using this image as the basic image for FROM.
If we pull the image directly from the repository, there may be no app directory or Dockerfile file in our deployment directory jf-isbm, so we need to create one manually and deploy it according to the specification.
Create an app directory under jf-isbm to store the packages, and create a Dockerfile under the app directory.
123FROM 1.xxx.xxx.xxx/zhxy/jf-isbm:v2.0-20190830COPY. / JF-ISBM/ etc/JF-ISBMCMD ["sh", "- c", "cd / etc/JF-ISBM/; dotnet / etc/JF-ISBM/JF-ISBM2.0WEBAPI.dll"]
Dockerfile details:
The basic image here is the old image that we have already used that needs to be updated. Because this image already contains the plug-ins and tools we need, there is no need for us to download it again, just replace the package, and COPY will overwrite the original JF-ISBM.
After writing Dockerfile, upload our new package and decompress it. The name should be the same as the name in Dockerfile. After that, we need to modify the docker-compose.yml file.
The first modification is to uncomment the build line because you need to build the image, or add a build:. / app at the sibling above the image line (that is, with as many spaces as before). Note here that there is a space after the colon. App is the directory where programs and Dockerfile are stored at the same level in docker-compose.yml. If your directory is not called app, please change it to your own name.
The second modification is to change the version number of the image field.
After the modification, we can docker-compose down and docker-compose up-d-- build.
Supplement and some points for attention
1 pay attention to the problem of Chinese garbled code after decompressing the uploaded file, and the specific solution lies in "solving the problem of garbled code of basic platform program".
2 pay attention to the revision of the version number. You must remember that there are many benefits of modification.
3 docker-compose may not be used in the specified version of version due to different versions. Here, there will be an error prompt at run time, indicating which version you should use, as follows: you will be prompted to use 2.2 or 3.3.
123 [root@localhost jf-isbm] # docker-compose buildERROR: Version in ". / docker-compose.yml" is unsupported. You might be seeing this error because you're using the wrong Compose fileversion. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the fileto use version 1.For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
4 here are only two examples of image construction and update, other image construction is similar, you can refer to it.
5 the address of the company's private image warehouse http://1.xxx.xxx.xxx
First, set the trust of the private image repository:
Vi / etc/docker/daemon.json
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],"
"insecure-registries": ["1.xxx.xxx.xxx"]
}
Systemctl restart docker
Centos login method: docker login-u xxx-p xxxx 1.xxx.xxx.xxx
Docker pull 1.xxx.xxx.xxx/datacenter/jf-beais:v1.0-20190516 pull image
Docker push 1.xxx.xxx.xxx/datacenter/jf-beais:v1.0-20190516 push image
The name of the image needs to be identified by the address of the private warehouse
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.