Two ways (b) generating a mirror - Docker

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/aogujianhanjianming/article/details/102772081

Sometimes Docker image downloaded from the warehouse in the mirror can not meet the requirements, we can build a mirror image of their own based on a base image

In two ways:

  • Update mirror: use docker committhe command

  • Construction of the mirror: Use the docker buildcommand, you need to create Dockerfile file

Update image

Create a container using a base image, and then make changes to the content of the container, and then use the docker commitcommand to submit a new image (for example to tomcat).

1. The base image to create the container

docker run --name mytomcat -p 80:8080 -d tomcat

 2. Modify the contents of the container

docker exec -it mytomcat /bin/bash
cd webapps/ROOT
rm -f index.jsp
echo hello world > index.html
exit

3. Submit a new image

docker commit -m="描述消息" -a="作者" 容器ID或容器名 镜像名:TAG
# 例:
# docker commit -m="修改了首页" -a="JeremySinnEr" mytomcat JeremySinnEr/tomcat:v1.0

4. Mirror operation with the new container

docker run --name mytomcat1 -p 8080:8080 -d JeremySinnEr/tomcat:v1.0

Use a mirror to build Dockerfile

Dokcerfile

What is Dockerfile

Dockerfile is nothing but the source code for building Docker images

  • Docker can build images automatically by reading the instructions from a Dockerfile

  • A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image

  • Using docker build users can create an automated build that executes several command-line instructions in succession

Dockerfile format

  • Format:

    • #Comment

    • INSTRUCTION arguments

  • The instruction is not case-sensitive

    • However,convention is for them to be UPPERCASE t o distinguish them from arguments more easily

  • Docker runs instructions in a Dockerfile in order

  • The first instruction must be 'FROM' in order to specify the Base Image from which you are building

Building SpringBoot application image using Dockerfile

First, prepare

0, create an empty directory / dockfile1, because docker build when the current directory will scan it again

1. Put your springboot project packaged into an executable jar package

2. Place the jar package upload / dockfile1 Linux server directory

Second, build

1. Create Dockerfile jar file in the package pathvi Dockerfile

# 指定基础镜像,本地没有会从dockerHub pull下来
FROM java:8
#作者
MAINTAINER JeremySinnEr
# 把可执行jar包复制到基础镜像的根目录下
ADD test.jar /test.jar
# 镜像要暴露的端口,如要使用端口,在执行docker run命令时使用-p生效
EXPOSE 80
# 在镜像运行为容器后执行的命令
ENTRYPOINT ["java","-jar","/test.jar"]

2. Use the docker buildcommand to build a mirror, basic grammar

docker build -t JeremySinnEr/mytest:v1 .
# -f指定Dockerfile文件的路径
# -t指定镜像名字和TAG
# .指当前目录,这里实际上需要一个上下文路径

Third, run

Run your own SpringBoot mirror

docker run --name mytest -p 80:80 -d 镜像名:TAG

Dockerfile common commands

FROM

FROM instruction is one of the most important and must be the first non-comment line Dockerfile opening files for the image file for the specified base image build process, running on this subsequent instruction execution environment provided by the base image

This base image can be any available mirror, docker build will find the designated image file from a local repository By default, if there is no will pull from Docker Hub

grammar:

FROM <image>
FROM <image>:<tag>
FROM <image>@<digest>

MAINTAINER(depreacted)

Dockerfile details provided by the creator

After Dockerfile does not limit the position MAINTAINER arise, but it is recommended put FROM command

grammar:

MAINTAINER <name>

name can be any text message, usually with author name or email

LABEL

Various metadata to specify the mirror

grammar:

LABEL <key>=<value> <key>=<value> <key>=<value>...

A Dockerfile can write multiple LABEL, but this is not recommended, Dockerfile each instruction will generate a layer image, if LABEL is too long you can use \ symbol wrap. Construction inherit LABEL mirror base image, and it will remove repeated, but if the values ​​are different, then the following values ​​overwrite the previous value.

COPY

A new image file from a host copy files to create

grammar:

COPY <src>...<dest>
COPY ["<src>",..."<dest>"]
# <src>:要复制的源文件或者目录,可以使用通配符
# <dest>:目标路径,即正在创建的image的文件系统路径;建议<dest>使用绝对路径,否则COPY指令则以WORKDIR为其起始路径

Note: If there is a blank character in your path, often use the second format

rule:

  • <src>Must be the path build context, it can not be the parent directory of the file

  • If <src>is a directory, its internal file or subdirectory will be recursive copy, but the <src>directory itself will not be copied

  • If more than one <src>, or <src>use a wildcard, it <dest>must be a directory, you must end / symbol

  • If <dest>does not exist, it will be created, including the parent directory path

ADD

And basic usage instructions as COPY, ADD supports the use of TAR file path and URL

grammar:

ADD <src>...<dest>
ADD ["<src>",..."<dest>"]

rule:

  • And the same rules COPY

  • If <src>a URL and <dest>does not end with a /, the <src>specified file will be downloaded to<dest>

  • If the <src>compressed tar file format on a local system, it expands to a directory; however obtained through URL tar files are not automatically expand

  • If <src>there are multiple, direct or indirect use of wildcards to specify multiple resources, the <dest>/ directory and in the end it must be

WORKDIR

Used to Dockerfile all RUN, CMD, ENTRYPOINT, COPY and ADD designated set the working directory, it will only affect the instruction after the current WORKDIR.

grammar:

WORKDIR <dirpath>

In Dockerfile file, WORKDIR can appear multiple times, the path may be a relative path, but it is specified relative to the previous instruction path WORKDIR

Further, WORKDIR ENV variable may be defined to specify

VOLUME

To create a mount point on the roll can be mounted on the host volume or other container

grammar:

VOLUME <mountpoint>
VOLUME ["<mountpoint>"]

Among the host can not specify a directory, the host mounted directory is generated automatically

EXPOSE

Specify the port to open the container to listen to and to enable external communication

grammar:

EXPOSE <port>[/<protocol>] [<port>[/<protocol>]...]

<protocol>Is used to specify transport layer protocol can be TCP or UDP, TCP is the default protocol

EXPOSE disposable can specify multiple ports, for example:EXPOSE 80/tcp 80/udp

ENV

Environment variables used to define the desired image, and may be other instructions Dockerfile file (e.g., ENV, ADD, COPY, etc.) located behind the call, call format: $ variable_name or $ {variable_name}

grammar:

 

ENV <key> <value>
ENV <key>=<value>...

The first format, <key>everything after will be considered <value>an integral part of, so you can only set a variable

The second format may be provided a plurality of variables, if <value>which spaces may be used \ to escape or <value>quotation marks for identification; additional \ continuation line can also be used

ARG

Use the same ENV

grammar:

ARG <name>[=<default value>]

Specify a variable, when you can create a mirror in the docker build, use --build-arg <varname>=<value>to specify the parameters

RUN

To specify docker build process runs the specified command

grammar:

RUN <command>
RUN ["<executable>","<param1>","<param2>"]

The first parameter format which typically is a shell command to /bin/sh -crun it

The second format is an array of JSON format, which <executable>is the command to run, followed by option or parameter passed to the command; but this format will not be used /bin/sh -cto initiate, so common operations like shell variable substitution and wildcards will not be replaced; if you are running a command shell properties dependence, can be replaced with the following type of format

RUN ["/bin/bash","-c","<executable>","<param1>"]

CMD

Command run when the vessel started

grammar:

CMD <command>
CMD ["<executable>","<param1>","<param2>"]
CMD ["<param1>","<param2>"]

The first two identical syntax and RUN

The third parameter is the default grammar for providing instructions ENTRYPOINT

RUN CMD and differences:

  • RUN instructions operate in the image file build process, CMD run on the new image file is constructed Dockerfile start time of a container

  • The main purpose is to CMD command to specify the default container started to run the program, and at the end of the run, the container will be terminated; however, CMD command can be docker run command-line options to cover

  • Dockerfile can contain multiple CMD command, but only the last one will take effect

ENTRYPOINT

CMD command functions similar to specify the default program to the operation container

grammar:

ENTRYPOINT<command>
ENTRYPOINT["<executable>","<param1>","<param2>"]

CMD and the difference is ENTRYPOINT start of the program will not be covered by docker run the command specified parameters, and these command-line parameters are passed as arguments to ENTRYPOINT specified program (however, --entrypoint docker run command parameter can be overridden ENTRYPOINT)

docker run command parameters passed will overwrite the contents of the instruction and the additional CMD command to ENTRYPOINT last used as its argument

Similarly, Dockerfile can contain multiple ENTRYPOINT instructions, but only the last one will take effect

If both CMD in Dockerfile there ENTRYPOINT, and CMD is a complete executable command, then who in the end who enter into force

ONBUILD

It is used to define a trigger in Dockerfile

grammar:

ONBUILD <instruction>

Dockerfile used to build the image file, the image file may be treated as another parameter is the base image as FROM instruction Dockerfile

When this latter Dockerfile the FROM instruction to be executed during the build process, will trigger the base image inside the instruction ONBUILD

ONBUILD not self nesting, ONBUILD not trigger FROM command and MAINTAINER

Use ADD and COPY command in ONBUILD to be careful, because the context of the new build process in the absence of the specified source file will fail when

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/aogujianhanjianming/article/details/102772081