亲宝软件园·资讯

展开

Dockerfile服务自启动

MasonYyp 人气:0

创建搭载flask的python容器,实现容器内服务随容器自启动,需要使用shell脚本Dockerfile中的CMD

1 Docker下载python

# 下载python
sudo docker pull python:3.8.2
# 创建python容器
sudo docker run -itd --name mypython python:3.8.2
# 进入mypython
sudo docker exec -it mypython /bin/bash
# 安装flask和gunicorn
pip install flask
pip install gunicorn

2 搭建flask服务

2.1 创建flask脚本

在宿主机上创建flask脚本文件

touch app.py

app.py的内容如下

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World"

将main.py复制到mypython容器中的/home/myflask目录下

sudo docker cp app.py mypython:/home/myflask

2.2 创建gunicorn启动脚本

在宿主机下创建启动文件

touch start_flask.sh

在start_flask.sh文件中添加

#!/bin/bash
#
# Program:
#       mypython flask
# History
#       2022-06-08 mason
#
# 进入目录
cd /home/myflask

# 执行程序
gunicorn -w 4 -b 0.0.0.0:5000 app:app

将start_flask.sh复制到mypython容器中的/home/myflask目录下

sudo docker cp start_flask.sh mypython:/home/myflask

3 制作镜像

3.1 将mypython容器制作成镜像

# docker commit -m "说明" -a "作者" 容器名 镜像名:版本号
sudo docker commit -m "my python " -a "mason" mypython mypython:v1.0

# 查看镜像列表
sudo docker images

# 返回结果
mypython                             v1.0                           875738200451   9 seconds ago   945MB

3.2 Dockerfile制作

(1)创建mypython-v1.0-dockerfile目录

mkdir mypython-v1.0-dockerfile

(2)在mypython-v1.0-dockerfile创建Dockerfile

# 进入目录
cd mypython-v1.0-dockerfile
# 创建文件
touch Dockerfile

(3)Dockerfile的内容如下

注意:run是在 docker build构建镜像时, 会执行的命令;cmd是在 docker run启动容器时, 会执行的命令

# 设置镜像
FROM mypython:v1.0
# 设置作者
MAINTAINER mason
# 开放5000端口
EXPOSE 5000
# 执行命令
CMD ["sh", "/home/myflask/start_flask.sh"]

(4)执行Dockerfile

sudo docker build -t mypython:v2.0 mypython-v1.0-dockerfile/

3.3 使用自定义的镜像创建容器

# 创建容器
sudo docker run -itd --name mytest -p 5000:5000 mypython:v2.0

# 查看mytest的日志信息 
sudo docker logs -f mytest

4 在联网的情况下,直接使用Dockerfile制作镜像

4.1 设置基本环境

# 创建目录
sudo mkdir mypython-1.0

# 进入mypython-1.0
cd mypython-1.0

# 创建myflask目录
sudo mkdir myflask

4.2 创建脚本

进入myflask目录,创建app.py文件,文件内容如下。

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World"

进入myflask目录,创建start_flask.sh文件,文件内容如下。

#!/bin/bash
#
# Program:
#       mypython flask
# History
#       2022-06-08 mason
#
# 进入目录
cd /home/myflask

# 执行程序
gunicorn -w 4 -b 0.0.0.0:5000 app:app

4.3 创建Dockerfile文件

进入mypython-1.0目录,注意不是myflask目录,创建Dockerfile文件,文件内容如下:

# 设置镜像
FROM python:3.8.2
# 作者
MAINTAINER Maon
# 在容器中安装相关的环境
RUN pip install flask
RUN pip install gunicorn
# 设置工作目录
WORKDIR /home
# 复制文件,注意myflask目录要与Dockerfile在同一个目录
# COPY会将myflask目录下的文件,全部拷贝到/home/myflask目录下
COPY myflask /home/myflask
# 开放端口5000
EXPOSE 5000
# 执行命令
CMD ["sh", "/home/myflask/start_flask.sh"]

执行文件

# 进入mypython-1.0目录
cd mypython-1.0

# 执行文件,注意后面的"."
sudo docker build -t mypython:1.0 .

# 执行过程如下
Sending build context to Docker daemon  4.608kB
Step 1/8 : FROM python:3.8.2
 ---> 4f7cd4269fa9
Step 2/8 : MAINTAINER Maon
 ---> Running in 01bd71948685
Removing intermediate container 01bd71948685
 ---> ddae7ce9a68d
Step 3/8 : RUN pip install flask
 ---> Running in 58f3fcfe573c
Collecting flask
  Downloading Flask-2.1.2-py3-none-any.whl (95 kB)
Collecting Jinja2>=3.0
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting importlib-metadata>=3.6.0; python_version < "3.10"
  Downloading importlib_metadata-4.11.4-py3-none-any.whl (18 kB)
Collecting click>=8.0
  Downloading click-8.1.3-py3-none-any.whl (96 kB)
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.1.2-py3-none-any.whl (224 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting zipp>=0.5
  Downloading zipp-3.8.0-py3-none-any.whl (5.4 kB)
Installing collected packages: MarkupSafe, Jinja2, zipp, importlib-metadata, click, itsdangerous, Werkzeug, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 flask-2.1.2 importlib-metadata-4.11.4 itsdangerous-2.1.2 zipp-3.8.0
WARNING: You are using pip version 20.1; however, version 22.1.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 58f3fcfe573c
 ---> a22c9de6e3d5
Step 4/8 : RUN pip install gunicorn
 ---> Running in 5b635d18e39b
Collecting gunicorn
  Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB)
Requirement already satisfied: setuptools>=3.0 in /usr/local/lib/python3.8/site-packages (from gunicorn) (46.1.3)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.1.0
WARNING: You are using pip version 20.1; however, version 22.1.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 5b635d18e39b
 ---> 8afabe6c3a62
Step 5/8 : WORKDIR /home
 ---> Running in 45040c10af7c
Removing intermediate container 45040c10af7c
 ---> 15ef04626c92
Step 6/8 : COPY myflask /home
 ---> b6736b08b1e3
Step 7/8 : EXPOSE 5000
 ---> Running in 0835fe7f5809
Removing intermediate container 0835fe7f5809
 ---> ebd8bd33c8dc
Step 8/8 : CMD ["sh", "/home/myflask/start_flask.sh"]
 ---> Running in e1f867903175
Removing intermediate container e1f867903175
 ---> 8c1b7d276dbb
Successfully built 8c1b7d276dbb
Successfully tagged mypython:1.0

4.4 使用镜像

# 创建容器
sudo docker run -itd --name mytest1.0 -p 5000:5000 mypython:1.0
# 查看日志
sudo docker logs -f mytest1.0

容器自动重启后,查看结果

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论