使用Dockerfile生成docker自定义镜像

Dockerfile常用指令

• FROM 构建镜像基于哪个镜像
• MAINTAINER 镜像维护者姓名或邮箱地址
• RUN 构建镜像时运行的指令,执行一条RUN镜像就会叠加一层,因此RUN尽可能一条写完
• ADD 拷贝文件或目录到容器中,如果是URL或压缩包便会自动下载或自动解压
• COPY 拷贝文件或目录到容器中,跟ADD类似,但不具备自动下载或解压的功能
• CMD 运行容器时执行的shell命令,CMD指令可以被run时的命令覆盖
• ENTRYPOINT 运行容器时执行的shell命令, ENTRYPOINT不会被覆盖
PS注意: 如 CMD ENTRYPOINT 都存在,则 CMD 为 ENTRYPOINT 参数
• EXPOSE 声明容器的服务端口(仅仅是声明)
• VOLUME 指定容器挂载点到宿主机自动生成的目录或其他容器
• USER 为RUN、CMD、和 ENTRYPOINT 执行命令指定运行用户
• WORKDIR 切换工作目录(cd)
• ARG 构建时指定的一些参数,ARG定义的变量仅在Dockerfile中可用
• ENV 设置容器环境变量,ENV定义的变量不仅在Dockerfile中可用,而且在运行的容器中都可用
• HEALTHCHECH 健康检查

通过编写dockerfile实现定制镜像

首先创建一个空白目录

然后编写Dockerfile文件

[root@server159 python]# ls
app.py  Dockerfile
[root@server159 python]# cat Dockerfile
FROM centos:7
MAINTAINER "[email protected]"
USER root

RUN mkdir -p /app && yum install -y python3 python3-devel
RUN pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN cp -r /usr/local/lib64/python3.6/site-packages/flask /app

COPY ./app.py /app/

WORKDIR /app
EXPOSE 8080

CMD ["python3","app.py"]

还有脚本文件,脚本文件要跟Dockerfile文件放在同一目录下才行

[root@server159 python]# cat app.py 
#coding=utf-8
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello docker,I am python flask."
if __name__ == "__main__":
    app.run(host='0.0.0.0',port=8080)


使用Dockerfile生成docker自定义镜像_第1张图片

然后构建镜像

[root@server159 python]# docker build -t centos7-python:v1 .
[+] Building 53.7s (11/11) FINISHED                      docker:default
 => [internal] load build definition from Dockerfile               0.0s
 => => transferring dockerfile: 350B                               0.0s
 => [internal] load .dockerignore                                  0.0s
 => => transferring context: 2B                                    0.0s
 => [internal] load metadata for docker.io/library/centos:7        0.0s
 => CACHED [1/6] FROM docker.io/library/centos:7                   0.0s
 => [internal] load build context                                  0.0s
 => => transferring context: 235B                                  0.0s
 => [2/6] RUN mkdir -p /app && yum install -y python3 python3-de  36.4s
 => [3/6] RUN pip3 install flask -i https://pypi.tuna.tsinghua.e  15.7s 
 => [4/6] RUN cp -r /usr/local/lib64/python3.6/site-packages/flas  0.1s 
 => [5/6] COPY ./app.py /app/                                      0.0s 
 => [6/6] WORKDIR /app                                             0.0s 
 => exporting to image                                             1.3s 
 => => exporting layers                                            1.3s 
 => => writing image sha256:e6a497c56c1c697ddf24eef187cc0d625e0cf  0.0s 
 => => naming to docker.io/library/centos7-python:v1               0.0s

查看生成的镜像

[root@server159 python]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
centos7-python   v1        e6a497c56c1c   2 minutes ago   526MB
php-fpm          v1        0474bd7488fa   23 hours ago    698MB
debian           latest    676aedd4776f   2 weeks ago     117MB
centos           7         eeb6ee3f44bd   2 years ago     204MB
nginx            1.18.0    c2c45d506085   2 years ago     133MB

用镜像构建容器,然后访问网页映射的80端口

[root@server159 python]# docker run --name pythontest -d -p 80:8080 centos7-python:v1 
c3ef2e932730eaf3fcfb268e6ca4627361af37041ae8d8760e65dedd86bc7f79
[root@server159 pythen]# curl 192.168.121.159
Hello docker,I am python flask.[root@server159 python]# 

可以看到是可以访问到的,去浏览器试试

使用Dockerfile生成docker自定义镜像_第2张图片

可以看到没问题

dockerfile实现定制镜像的原理

Dockerfile构建镜像相当于shell脚本,将我们要执行的命令一次性完成

下面用单步操作来展现上面的过程

用基础镜像centos7创建容器并进入

[root@server159 python]# docker run -dt --name pytest -p80:8080  centos:7
4263466478e64e02ce0a19db4c6e4ad9f284785ff5a54b6fc19a930e2c0e7507
[root@server159 python]# docker exec -it pytest bash

创建目录工作目录

[root@4263466478e6 /]# mkdir /app

然后下载python环境

[root@4263466478e6 /]# yum install -y python3 python3-devel

然后下载flask模块

[root@4263466478e6 /]# pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple

然后将flask拷贝到/app工作目录下

[root@4263466478e6 /]# cp -r /usr/local/lib64/python3.6/site-packages/flask /app

然后将app.py文件从宿主机拷贝到容器中的工作目录

[root@server159 ~]# ls
anaconda-ks.cfg  default.conf  original-ks.cfg  Python-3.9.7.tgz
app.py           index.html    pythen           www.conf
[root@server159 ~]# cat app.py 
#coding=utf-8
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello docker,I am python flask."
if __name__ == "__main__":
    app.run(host='0.0.0.0',port=8080)
[root@server159 ~]# docker cp ./app.py pytest:/app
Successfully copied 2.05kB to pytest:/app

回到容器里查看工作目录

[root@4263466478e6 /]# ls /app
app.py  flask

然后运行python脚本 ,启动后我直接去浏览器访问的,所以下面有一条访问记录

[root@4263466478e6 app]# python3 ./app.py 
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://172.17.0.2:8080/ (Press CTRL+C to quit)
192.168.121.3 - - [01/Nov/2023 12:30:28] "GET / HTTP/1.1" 200 -

使用Dockerfile生成docker自定义镜像_第3张图片

Dockerfile定制容器不过是将我们执行的动作写到文件里

相当于shell脚本,容器的定制就是这样了

你可能感兴趣的:(docker容器,docker,容器,运维,python)