使用Dockerfile构建flask镜像

之前对dockerfile进行了一些构建参数的学习,今天自己构建了一个常用的flask镜像,我个人认为使用dockerfile构建是为了自定义以及定制化的一些环境需求而使用的,例如自己写的python程序需要预装一些模块,开机启动我写的脚本或者制定位置启动程序等等,然而直接拉取docker仓库的镜像满足不了我的需求,从而需要dockerfile来构建,下面是我构建的flask镜像,很简单,只是简单的初步学习,后面有项目了再分享一些项目上构建的复杂镜像

/data/app目录下主要有三个文件Dockerfile,requirements.txt,app.py

sudo vim /data/app/Dockerfile

FROM python:3.8
WORKDIR /app
COPY ./* /app/
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]

sudo vim /data/app.py

#!/usr/bin/python3
#coding:utf-8
from flask import Flask,Blueprint
app = Flask(__name__)

@app.route("/")
def index():
    return "test flask !"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

sudo vim /data/app/requirements.txt

Flask
requests

然后开始沟将这个镜像

docker build -t flask .

其中RUN命令看了很多教程提示要将多条命令合并一条,这里没有什么多余的其他命令,所以这里写出来,是由于docker镜像是有一个层级的概念,而且层级是有限的,而且多条命令会导致镜像膨胀变的很大,相关文章可以看看这个

https://blog.csdn.net/shengjmm/article/details/77411253

构建完成以后可以通过docker images查看生成的镜像

REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
flask        latest    99627d34fb72   About a minute ago   897MB

这个镜像一共有6个步骤

1.拉取基础镜像python3.8

2.创建工作目录/app

3.copy本地当前目录下所有文件(*代表所有文件)到容器下的/app目录

4.运行pip安装python包

5.声明交互端口,后面用docker命令用来映射的端口

6.容器启动时执行的命令

我们可以使用docker inspect flask命令查看我们构建的镜像内部详情

[
    {
        "Id": "sha256:99627d34fb72dd0f288191def6aac61c0e49938c31516261f870d319ea82b0f7",
        "RepoTags": [
            "flask:latest"
        ],
        "RepoDigests": [],
        "Parent": "sha256:42f300455dc6178288980977905b208d7166ccb8f50e2de0a96f679588bcf028",
        "Comment": "",
        "Created": "2021-07-22T14:42:23.577496313Z",
        "Container": "a26ab38d6cd83d92dcdc31391c742f351e01251b007cf33f699ff2c8588d401f",
        "ContainerConfig": {
            "Hostname": "a26ab38d6cd8",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568",
                "PYTHON_VERSION=3.8.11",
                "PYTHON_PIP_VERSION=21.1.3",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/a1675ab6c2bd898ed82b1f58c486097f763c74a9/public/get-pip.py",
                "PYTHON_GET_PIP_SHA256=6665659241292b2147b58922b9ffe11dda66b39d52d8a6f3aa310bc1d60ea6f7"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"python\" \"app.py\"]"
            ],
            "Image": "sha256:42f300455dc6178288980977905b208d7166ccb8f50e2de0a96f679588bcf028",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.2",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568",
                "PYTHON_VERSION=3.8.11",
                "PYTHON_PIP_VERSION=21.1.3",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/a1675ab6c2bd898ed82b1f58c486097f763c74a9/public/get-pip.py",
                "PYTHON_GET_PIP_SHA256=6665659241292b2147b58922b9ffe11dda66b39d52d8a6f3aa310bc1d60ea6f7"
            ],
            "Cmd": [
                "python",
                "app.py"
            ],
            "Image": "sha256:42f300455dc6178288980977905b208d7166ccb8f50e2de0a96f679588bcf028",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 896523550,
        "VirtualSize": 896523550,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/c03d3bee649b2d667a9035a99478b99bc6e030a3295314050edca7a505561edc/diff:/var/lib/docker/overlay2/41ee036cb9f9998b42d9d74e906b73c0d43e6dc97a6723c8f2e23575db8a6929/diff:/var/lib/docker/overlay2/c9d300ec67f21d3274ebea9f3ddb74f1981288cb97dcdbb56fc945feff21e557/diff:/var/lib/docker/overlay2/d1024b374f47ef6720ae7aff2ff1593a6961635ce92fa8ff3afbf51d9c09e2c1/diff:/var/lib/docker/overlay2/2bb6752762de1d7c424922c395d1b3514ca80c04cf33b64d2c0a36124f3a8d77/diff:/var/lib/docker/overlay2/539ee9535b772924fe7641e5ab56f4de10f9c5cde0da8de253de0df35ae36b2c/diff:/var/lib/docker/overlay2/56647b990520c6d876ada80bbe8c3d443a39f0c651e97b5ee381ede545d86503/diff:/var/lib/docker/overlay2/44fc8c8a721f0fed6289c4802ac0473e9fef129fdcfab24c235179e16fb926af/diff:/var/lib/docker/overlay2/b81cbb68c54837b7198e839aff86055d068ae84ba8f6c9745286fda7f292624f/diff:/var/lib/docker/overlay2/c00baa4a4dfce68531f93ba674e91e4d776de5602f313a520943b7637cbd0f27/diff:/var/lib/docker/overlay2/527c87cfcecad746aea486c0cec1cf58b5dbc6840affec7216cc8a85cecfee7f/diff",
                "MergedDir": "/var/lib/docker/overlay2/c4cee7b391445ec0901069718e361261233fd7c1eb68c03bd58534b2f0e583c5/merged",
                "UpperDir": "/var/lib/docker/overlay2/c4cee7b391445ec0901069718e361261233fd7c1eb68c03bd58534b2f0e583c5/diff",
                "WorkDir": "/var/lib/docker/overlay2/c4cee7b391445ec0901069718e361261233fd7c1eb68c03bd58534b2f0e583c5/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:4e006334a6fdea37622f72b21eb75fe1484fc4f20ce8b8526187d6f7bd90a6fe",
                "sha256:e4d0e810d54a9c47e3dface412b0b88045d156b18809a2e0bbfb0fc8a45d8127",
                "sha256:fe6a4fdbedc0cbc560437fa700b3b034114e31a264f0818d0d32ed2ee6cbe7a3",
                "sha256:7095af798ace32173839a61cbf101048434e1065185c0f29cc888e67158d990b",
                "sha256:cdc9dae211b46c95cfcf70f8c98aa6ad0e114439cb5ce2f229b078338f0a4f64",
                "sha256:4b4c002ee6ca02473de9ed967cc372717121a986f21ea876f9d854284481a6d0",
                "sha256:c75080a21a0af8bfeb4e8fd264e49eb9a17d5d1839fb9880c649448a47f6a98e",
                "sha256:a29b259e81f970c0c40dee4dff348588136c31bafa7f555ddac206c7a936aded",
                "sha256:4bee83e267b55aea251460c93068fdb415268c6284c39d3d3916952af8c25078",
                "sha256:31e16380c90da2586a7a711f736bc19ed1c0b856832329ea889e8b85bfe3ffc3",
                "sha256:e7eb8c883380d778f2afe9f806cbe016a5809360d48bc5ce7ad4e54decc16de4",
                "sha256:d22e4e238142590461066f57c1a07e9e505283041d2baa86e824fa89a53f13cf"
            ]
        },
        "Metadata": {
            "LastTagTime": "2021-07-22T22:42:23.58926702+08:00"
        }
    }
]

最后我们启动命令测试下

docker run -itd -p 5000:5000 --name app-flask flask

然后docker ps查看下

CONTAINER ID   IMAGE     COMMAND           CREATED          STATUS          PORTS                    NAMES
101decdd33e5   flask     "python app.py"   19 minutes ago   Up 19 minutes   0.0.0.0:5000->5000/tcp   app-flask

本地打开http://localhost:5000/

显示test flask !就是部署完成,构建的镜像没有问题了


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/794.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。