Docker --- mirroring and Dockerfile Detailed

table of Contents

.Docker a mirror made of three ways

Based on the existing image creation (docker commit)

Create a template-based local mirroring

Custom image based Dockerfile

Detailed instructions two .Dockerfile

Dockerfile concept

FROM

MAINTAINER

RUN

CMD

EXPOSE

ENV

ADD

COPY

ENTRYPOINT

VOLUME

USER

WORKDIR

ONBUILD


.Docker a mirror made of three ways

Based on the existing image creation (docker commit)

  • View existing images
[root@cloud ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              6678c7c2e56c        2 weeks ago         127MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
[root@cloud ~]# 
  • Create a container and run

[root@cloud ~]# docker create -it centos /bin/bash
7e750101b0eef5720205f6b4816d23f66815bb0bda5bc4f38935ad0fe03f80aa
[root@cloud ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7e750101b0ee        centos              "/bin/bash"         14 seconds ago      Created                                 hopeful_dirac
[root@cloud ~]# docker start 7e750101b0ee
7e750101b0ee
[root@cloud ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7e750101b0ee        centos              "/bin/bash"         40 seconds ago      Up 13 seconds                           hopeful_dirac
[root@cloud ~]# 
  • Create a mirror on the container
//-m指定该镜像的信息
//-a指定制作该镜像的作者
//centos:new 为镜像名和标签名
[root@cloud ~]# docker commit -m "new images" -a "androot" 7e750101b0ee centos:new
sha256:94b86b016dc0313e8e6d628d960e0910d130243674d585419b611a2c3e47920d

//查看新生成的镜像
[root@cloud ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              new                 94b86b016dc0        5 seconds ago       237MB
nginx               latest              6678c7c2e56c        2 weeks ago         127MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
[root@cloud ~]# 

Create a template-based local mirroring

  • The Internet to download a module, downloaded the debian module
wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz

 

  • Create a mirror

[root@cloud ~]# ls
debian-7.0-x86-minimal.tar.gz  mysql
[root@cloud ~]# cat debian-7.0-x86-minimal.tar.gz | docker import - debian:local
sha256:72fa7440a81c379c884c4e43638a0bac3eef47c4f455a7f6746da4852abaa836
[root@cloud ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian              local               72fa7440a81c        2 minutes ago       215MB
centos              new                 94b86b016dc0        9 minutes ago       237MB
nginx               latest              6678c7c2e56c        2 weeks ago         127MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
[root@cloud ~]# 

docker import: create a mirror from the archive

Usage is as follows:

docker import [OPTIONS] file | URL | - REPOSITORY[:TAG]

OPENTIONS Description:

-c: Application docker command to create a mirror

-m: caption when submitted

Custom image based Dockerfile

  • Create a file in the local Dockerfile
[root@cloud ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian              local               72fa7440a81c        21 minutes ago      215MB
centos              new                 94b86b016dc0        28 minutes ago      237MB
nginx               latest              6678c7c2e56c        2 weeks ago         127MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
[root@cloud ~]# 

  • Creating execute scripts, and web interface
[root@cloud docker]# pwd
/docker
[root@cloud docker]# vi run.sh
[root@cloud docker]# cat run.sh 
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND

[root@cloud docker]# echo "this is web test" > index.html
[root@cloud docker]# ls
Dockerfile  index.html  run.sh
[root@cloud docker]# 

  • Generate Mirror
[root@cloud docker]# docker build -t new_httpd:centos .
。。。。。。
Successfully built bbbb46a6633e
Successfully tagged new_httpd:centos
[root@cloud docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
new_httpd           centos              bbbb46a6633e        46 seconds ago      414MB
debian              local               72fa7440a81c        31 minutes ago      215MB
centos              new                 94b86b016dc0        38 minutes ago      237MB
nginx               latest              6678c7c2e56c        2 weeks ago         127MB
httpd               latest              c5a012f9cf45        3 weeks ago         165MB
centos              latest              470671670cac        2 months ago        237MB
[root@cloud docker]# 
  • The use of image creation and operation of the vessel
[root@cloud docker]# docker run -d -p 1216:80 new_httpd:centos 
3147e2b184ada8128d4ed4dad219c139d2da84bca7036560168b0ad289d1fbb6
[root@cloud docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
3147e2b184ad        new_httpd:centos    "/run.sh"           6 seconds ago       Up 5 seconds        0.0.0.0:1216->80/tcp   lucid_blackwell
[root@cloud docker]# 

-p port specify the mapping, network traffic will next docker detailed explanation

  • Test container, http: //123.57.242.90: 1216

Detailed instructions two .Dockerfile

Dockerfile concept

  • Dockerfile is used to build docker mirrored build file, is a series of commands and parameters, similar to shell scripts
  • Dockerfile raw materials software and docker mirror is software deliverables.
  • Docker containers can be considered software running state
  • Each instruction Dockerfile reserved words must be followed by capital letters and a parameter
  • Dockerfile of each instruction will create a new image layer, and mirror commit

FROM

  • 用法:FROM <image>
  • FROM construct basis for specifying the source of the images in the image, if not specified local mirror, the mirror will automatically pull down from the public repository Docker
  • FROM must be the first line of the comment instruction Dockerfile Africa, the first instruction that is Dockerfile must start FROM
  • If there is a demand must create multiple mirrors in a Dockerfile, the FROM can appear multiple times
  • If RFOM statement does not specify the mirror tag, the default label use latest

MAINTAINER

  • 用法:MAINTAINER <name>
  • Create a user to specify the mirror

RUN

  • RUN action command is to be executed on the basis of the current image as a new image and submit mirror after RUN submitted before the subsequent RUN are based. Mirroring is layered, can be submitted by any of the points to create a historical image, similar to the source code version control
  • RUN command implemented in two ways

The first

  • RUN "scripting", "parameter 1" and "parameter 2"

The second

  • RUN ["sh","-c","echo","$HOME"]

Note: The next time you build the cache when RUN is generated does not fail, it will be reused, can be used docker build --no-cache command

CMD

  • CMD command specifies Dockerfile used only once in the file, if there multiple times, only the last one will work
  • CMD purpose is to provide a default command option when you start the container. Specifies the command to run if the user starts the container, CMD command instruction will overwrite
  • CMD is used in three ways
1.CMD "执行文件","参数1","参数2"
2.CMD "参数1","参数2"
3.CMD command 参数1 参数2(shell from)

Note: CMD will perform at startup container, build is not executed, and RUN only perform when building a mirrored, after a subsequent image build is complete, the vessel will start regardless of the RUN

EXPOSE

  • EXPOSE command is designated port mapping Docker container outside, you need to run in the docker -p / -P take effect
  • EXPOSE usage: EXPOSE <port> [<port> ...]

ENV

  • ENV command to specify an environment variable, will be used in subsequent instruction RUN, and remain in the container runtime
  • ENV usage
1.ENV <key> <value>    #只能设置一个变量
2.ENV <key>=<value>    #允许一次设置多个变量
  • Examples are as follows
##例子
ENV myName="John Doe" myDog=rex\the\dog \
myCat=fluffy
##等同于
ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy

ADD

  • ADD Copy local host file, directory, or from a remote file URLS added to the container and the specified path
  • GO supported by regular fuzzy matching, specific rules can be found in Go filepath.Match
  • Road King Road King must be absolute, if not present, will automatically create the corresponding directory
  • Road King must be a relative path where the path Dockerfile
  • If it is a directory, only the contents of the directory replication, and the directory itself will not be copied
  • ADD usage: ADD <src> ... <test>

COPY

  • COPY copy files or directories, and added to the container specified path. Use with ADD, the only difference is that you can not specify a remote file URLS
  • COPY Usage <src> .. <test>

ENTRYPOINT

  • Configuration command after starting the container, and may not be covered docker run parameters supplied, and CMD can be covered. If you need to cover, you can use docker run --entrypoint
  • Each Dockerfile can have only one ENTRYPOINT, when you specify more, only the last one will work
  • ENTRYPOINT usage
1.ENTRYPOINT "exectable","param1","param2"
2.ENTRYPOINT command param1 param2(shell from)

VOLUME

  • VOLUME effect is to mount the machine to the target directory vessel or other container mount mount mount point to the target vessel
  • Usage VOLUME: VOLUME [ "/ data"]

USER

  • UID specifies the user or when the container is running, subsequent RUN, CMD, ENTRYPOINT also specified user
  • USER usage: USER daemon

WORKDIR

  • WORKDIR for subsequent RUN, CMD, ENTRYPOINT instructions to configure the working directory. WORKDIR plurality of instructions may be used, if the follow-up parameter is a relative path command, it specifies based on the previous command path
  • WORKDIR used as follows:
##例1
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
最终路径为/a/b/c
##WORKDIR指令可以在ENV设置变量之后调用环境变量
ENV DIRPATH /path
WORKDIR $DIRPATH/$DIRNAME
最终路径为/path/$DIRNAME

ONBUILD

  • ONBUILD usage: ONBUILD [INSTRUCTION]
  • Dockerfile using the generated image A, does not perform ONBUILD command again if there is a DOckerfile image A as the base image to generate the image B will execute the command ONBUILD
Published 139 original articles · won praise 168 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_42761527/article/details/104976721