Dockerfile entry and actual cases

1, the concept of

Dockerfile is a text file which contains the instructions and parameters of rows, each row of a layer of build instructions, and therefore each line of instructions is to describe how the layer should be constructed; they greatly simplifies the process from start to finish deployment; Dockerfile from fROM command to start, followed by followers of the various methods, commands and parameters, and the final output image can be used to create a new container.

 

2, command Detailed

instruction

description

Examples

FROM

Must be the first command, specify the base image

FROM centos:latest

MAINTAINER

This command is used to declare the author, should be placed FROM the next line

MAINTAINER wzs

RUN

Construction executed when the mirror shell command is Dockerfile core of the execution of the command;

 

RUN yun install httpd

RUN [yum,install ,httpd]

CMD

With the RUN command similar, execute shell commands; and RUN difference is, RUN executed in the process of building the mirror, CMD is called after building a container with a mirror; if we docker run specified command or when there EntryPoint , then CMD will be covered; Dockerfile multiple CMD command only the last one will

Use one: without the brackets

CMD echo "hello cmd!"

Usage II: with brackets, without any default shell

CMD ["/bin/bash", "-c", "echo 'hello cmd!'"]

Use three: pass arguments to the ENTRYPOINT

CMD [-l]

ENTRYPOINT

And CMD commands, the execution time is running container shell command; and CMD difference is, CMD can perform docker run the specified command covering the time, ENTRYPOINT only use --entrypoint to cover; CMD and ENTRYPOINT can be used with, ENTRYPOINT used as fixed command, CMD used ENTRYPOINT variable parameters in order to carry

Use one: do not use [] any run and CMD is not to be passed, it will cover the CMD

ENTRYPOINT /bin/bash -c start.sh

Usage II: Use [] can be passed CMD the -l parameter, i.e., ls -l

CMD [-l]

ENTRYPOINT [ls]

EXPOSE

Exposed container port; you can also specify a port is listening on TCP or UDP , the default is TCP

EXPOSE 80,443

EXPOSE 80/udp

ENV

Setting environment variables within the container , you can start the container --env <key> = <value> modified values environment variable

ENV MYSQL_ROOT_PASSWD 123456

ADD

Copy files or directories to the mirror, if it is URL or compression or decompression package will automatically download

ADD https://xxx.com/html.tar.gz / var / www / html

ADD html.tar.gz / var / www / html

COPY

Copy the file or directory to the mirror

COPY ./test.txt /tmp/test.txt

USER

To RUN , CMD , EntryPoint , COPY , the ADD specify run user to execute commands

USER wzs

WORKDIR

RUNCMDENTRYPOINTCOPYADD设置工作目录

WORKDIR /data

ARG

在构建镜像时指定一些参数

ARG user

USER $user

 

3、CMDENTRYPOINT用法讲解

说明:因CMDENTRYPOINT的用法比较复杂,这里举几个案例对CMDENTRYPOINT的区别和用法进行解读

案例一:CMD用法1

image.png

docker build -t cmd:v1  -f /var/Dockerfile/cmd-1.txt /var/Dockerfile/

使用Dockerfile构建cmd:v1镜像后,执行docke run cmd:v1可以看到使用了Dockfile中的CMD echo 123 命令

image.png 

执行docke run cmd:v1 echo abc 会将Dockfile中的CMD命令覆盖

image.png 

 

案例二:CMD用法2

image.png

docker build -t cmd:v2  -f /var/Dockerfile/cmd-2.txt /var/Dockerfile/

使用[]中括号方式;这时,命令没有再任何shell终端环境下,如果我们要执行shell,必须把shell加入到中括号的参数中

image.png

执行docker run cmd:v2 ls /tmp/ 会将Dockfile中的CMD命令覆盖

image.png

 

案例三:CMD用法3ENTRYPOINT用法1

CMD的第三种用法用于传参,不携带任何命令;所以要配合ENTRYPOINT使用

image.png

Dockefile文件中ENTRYPOINT中的echo 作为命令,CMDhello cmd作为传参,执行后如下:

image.png

也可以在docker run时携带参数 hello entrypoint 覆盖CMD的内容

image.png


案例四:CMD用法3ENTRYPOINT用法2

image.png

Dockerfile中的ENTRYPOINT不使用中括号后,会覆盖CMD的内容;docker run只会执行ENTRYPOINT

image.png

docker run时携带任何参数或命令都无法被传入到entrypoint

image.png

4、实战案例

案例一、tomcat部署

首先创建一个制定tomcat镜像的目录,存放镜像所需的安装包和Dockerfile文件

image.png

Dockerfile文件内容

#使用基础镜像

FROM centos:latest

#注明作者

MAINTAINER wzs

#创建目录

RUN mkdir -p /usr/local

#添加安装包到镜像

ADD apache-tomcat-7.0.93.tar.gz /usr/local/

ADD jdk-8u221-linux-x64.tar.gz /usr/local/

#定义环境变量

ENV JAVA_HOME /usr/local/jdk1.8.0_221

ENV JRE_HOME $JAVA_HOME/jre

ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.93

ENV PATH $CATALINA_HOME/bin:$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

#暴露端口

EXPOSE 8080

#启动时运行tomcat进程

CMD ["/usr/local/apache-tomcat-7.0.93/bin/catalina.sh","run"]

使用Dockerfile构建镜像

docker build -t tomcat:v1 -f ./Dockfile.txt ./

image.png

使用镜像创建并运行容器

docker run -itd --name tomcat-docker -p 8081:8080 tomcat:v1

image.png


案例二、nginx部署

创建一个制定nginx镜像的目录,存放镜像所需的安装包和Dockerfile文件

image.png

Dockerfile文件内容

#使用基础镜像

FROM centos:latest

#声明作者

MAINTAINER wzs

#安装依赖库

RUN yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssh-devel zlib

#添加安装包到镜像

ADD nginx-1.8.0.tar.gz /usr/local

#编译部署

RUN cd /usr/local/nginx-1.8.0 && \

    ./configure --prefix=/usr/local/nginx && \

    make && \

    make install

#删除安装包文件

RUN rm -rf /usr/local/nginx-1.8.0

#暴露端口

EXPOSE 80

#定义工作目录

WORKDIR /usr/local/nginx

#启动时运行nginx

CMD ["./sbin/nginx","-g","daemon off;"]

使用Dockerfile文件构建镜像

docker build -t nginx:v1 -f ./Dockerfile.txt ./

image.png

使用镜像创建运行容器

docker run -itd --name nginx-docker -p 81:80 nginx:v1

image.png


案例三、mysql部署

创建一个制定tomcat镜像的目录,因为mysql使用yum安装;所以这里只有Dockerfile文件

image.png

Dockerfile文件内容

#使用基础镜像

FROM centos:latest

#作者

MAINTAINER wzs

#添加mysql用户

RUN useradd -d /opt/mysql mysql

#下载mysqlyum安装包文件

ADD http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm /opt/

RUN rpm -ivh /opt/mysql-community-release-el7-5.noarch.rpm

#安装mysql-server

RUN yum install -y mysql-server

#mysql初始化

RUN mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/

#暴露端口

EXPOSE 3306

#这里使用CMDENTRYPOINT组合,ENTRYPOINT中为mysql服务启动的固定命令,CMD指定mysql的运行用户,所在docker run时可以覆盖修改

CMD ["--user=mysql"]

ENTRYPOINT ["/usr/sbin/mysqld"]

使用Dockerfile构建mysql镜像

docker build -t mysql:v1 -f ./Dockerfile ./

image.png

使用镜像创建运行容器

docker run -itd --name mysql-docker mysql:v1

image.png


Guess you like

Origin blog.51cto.com/14129044/2428458