使用Dockerfile實現容器內部服務隨容器自啟動的方法

創建搭載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

容器自動重啟後,查看結果

在這裡插入圖片描述

到此這篇關於使用Dockerfile實現容器內部服務隨容器自啟動的文章就介紹到這瞭,更多相關Dockerfile服務自啟動內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: