[Docker custom Dockerfile mirroring] --2019-08-06 15:20:55

Original link: http://106.13.73.98/__/105/

Customized image, that is, each layer adding custom configuration files, if each layer can be modified, installation, construction, operation commands are written into a script, their feet have been constructed, customized image, this script isDockerfile

DockerfileIs a text file that contains an inner section instruction (Instruction), each instruction to build one layer, the content of each instruction is not how it should be constructed for each layer.

Dockerfile --------- ⬇️


the FROM
specify on which to build a new image based on the image file, try to use the official bash image
example:
the FROM name Mirror / Mirror ID [: Tag]


LABEL
containers yuan news, help information, Metadata , similar to the code comments
example:
LABEL version = "1.0" version labeled
LABEL maintainer = "name @ 163.com" declared producer


RUN
command to execute when creating a mirrored


WORKDIR
switch the working directory


COPY
to add files to the machine container
example :
cOPY flask_web.py / app / copy files flask_web.py the machine to the next container / app directory, will automatically create / app directory


ADD
addition cOPY features include decompression function, should give priority to using the cOPY command


ENV
settings environment variable, using ENV possible to set the environment variables to increase the maintainability
example:
ENV MYSQL_VERSION 5.6 provided a MySQL constant
RUN yum install -y mysql-server = "$ {MYSQL_VERSION}" call MySQL constant


CMD
is provided after the container has started and default parameters command execution
⚠️ If defining a plurality of CMD, only the last one is executed
⚠️ if docker run command specifies other (e.g.: / bin / bash, python flask_web.py ), the CMD command It will be ignored



eXPOSE
network settings
example:
EXPOST 8080 8080 port of exposed container


VOLUME
storage settings


ENTRYPOINT
command to run when you start setting the container
so that the container runs as an application or service will not be ignored, will perform

About Shell Exec format and format


Relative command format contrast Shell Exec format are as follows.


Shell format

RUN yum -y install vim
CMD echo "Hello Docker"
ENTRYPOINT echo "Hello Docker"


Exec format

RUN ["apt-get", "install", "-y", "vim"]
CMD ["/bin/echo", "Hello Docker"]
ENTRYPOINT["/bin/echo","Hello Docker"]

have to be aware of is:
通过Shell格式运行的命令可以读取设置的变量,而Exec格式则不可以读取,Exec格式仅仅是执行一个命令.

Dockerfile will use to make a Flask Web service


First, prepare a script calls Flask web services for:

"""flask_web.py文件代码如下"""
from flask import Flask
app=Flask(__name__)
@app.route('/')
def run():
    return 'is ok'
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)  # 我们这里使用的是8080端口


Then, start writing Dockerfile:

# 指定基于哪个基础镜像来构建
FROM python:2.7

# 制作人
LABEL maintainer='zyk [email protected]'

# 安装flask第三方库
RUN easy_install flask

# 将本机的flask_web.py文件复制到容器的/app目录下,会自动生成/app目录
COPY flask_web.py /app/

# 进入/app目录
WORKDIR /app

# 暴露容器的8080端口
EXPOSE 8080

# 设置容器启动后默认执行的命令和参数,以Exec格式执行,注意:要用双引号
CMD ["python", "flask_web.py"]


Finally, mirrored:

# ⚠️ Dockerfile与flask_web.py文件在同级目录
[root@fedora docker]# ls
Dockerfile  flask_web.py

# 开始制作镜像
[root@fedora docker]# docker build -t zyk/flask .


Once created, to view mirror and start:

# 查一下镜像ID
[root@fedora docker]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
zyk/flask                   latest              c4e156f3222a        50 seconds ago      911 MB

# 启动镜像
[root@fedora docker]# docker run -d -p 9000:8080 c4e156f3222a


Well, open your browser to access the test:
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/gqy02/p/11309170.html