1 环境
1.0 系统
- Ubuntu 18.04
- Docker-ce 18.03.1
1.2 目录结构
|-- Docker
| `-- first_flask_test
| `-- app
| |-- Dockerfile
| |-- app.py
| `-- requirements.txt
2 文件解析
【Dockerfile】
配置运行环境,安装各种需要的包.
# 安装Ubuntu
FROM ubuntu
LABEL author = 'xdq'
# 更新源
RUN apt-get update
# 安装Ubuntu包
RUN apt-get install -y apt-transport-https vim iproute2 net-tools ca-certificates curl wget software-properties-common
RUN apt-get update
# 安装python和pip
RUN apt-get install -y python2.7
RUN apt-get install -y python-pip
RUN apt-get install -y python3.6
RUN apt-get install -y python3-pip
# pyhton2.x和python3.x共存
RUN update-alternatives --install /usr/bin/python2 python2 /usr/bin/python2.7 1
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
RUN update-alternatives --config python3
# 工作路径
WORKDIR /app
# 将当前目录. 生成的image复制到/app路径
COPY . /app
# 安装运行服务需要的包
RUN pip3 install -r requirements.txt
# image的Ubuntu系统对外开放的端口
EXPOSE 80
# 终端可显示中文
ENV PYTHONIOENCODING=utf-8
# 启动container时运行服务
CMD ["python3.6", "app.py"]
【app.py】
服务逻辑.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/connect", methods=["GET"])
def connect():
return jsonify({"data":"200", "desc":"bazinga"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
【requirements.txt】
服务需要的包.
Flask
3 创建app
3.1 新建image
sudo docker build --tag=test_flask /home/xdq/xinPrj/python_web/Docker/first_flask_test/app/
参数 | 描述 |
---|---|
build | 创建image |
–tag | image标签ReopsitoryName |
/home/… | 保存image的路径 |
【开始创建image】
Sending build context to Docker daemon 4.608kB
Step 1/16 : FROM ubuntu
---> 94e814e2efa8
Step 2/16 : LABEL author = 'xdq'
---> Using cache
---> f914055c795a
Step 3/16 : RUN apt-get update
...
3.2 查看image
sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test_flask latest c80daa51de5f 3 minutes ago 637MB
4 运行app
4.1 直接前台运行
sudo docker run -p 8090:80 test_flask
参数 | 描述 |
---|---|
run | 运行 |
-p | 绑定端口,80为image的container对外的端口,8090是对外的接口 |
test_flask | image名称 |
4.2 后台运行container
sudo docker run -dit --name=test_flask -p 8090:80 test_flask:latest
【返回 ID】
d59812bf5cbb7c0dacf1f97c90bcf87cc3cc6138ca8ed850a9b3749b4ccf060e
参数 | 描述 |
---|---|
run | 运行 |
dit | 守护进程运行并开启前台进程(确保不会exit) |
-p | 绑定端口,80为image的container对外的端口,8090是对外的接口 |
test_flask | image名称 |
latest | image标签 |
5 分享image
5.1 登录Docker ID
sudo docker login
5.2 image打包
【格式】
sudo docker tag image username/repository:tag
参数 | 描述 |
---|---|
image | 本地image的Repository名称 |
username | Docker用户名 |
repository | Docker云端建立的仓库名称 |
tag | 类似于image云端版本 |
【例子】
sudo docker tag test_flask myname/repository_name:tag_name
【查看image】
sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test_flask latest c80daa51de5f 26 minutes ago 637MB
test/connect upload c80daa51de5f 26 minutes ago 637MB
5.3 推送到云端
sudo docker push myname/repository_name:tag_name
【上传信息】
The push refers to repository [docker.io/myname/test_flask]
a9997398912c: Pushed
701f7121b55c: Pushed
1838ad104791: Pushed
3cba4757f572: Pushed
6d6faae27f42: Pushed
8b69054ce635: Pushed
1cbfdca28530: Pushed
f04aa4a82ff3: Pushed
395d7eed0b80: Pushed
b694a04d82c4: Pushed
753e9587d8ff: Pushed
459543aab850: Pushed
808bddae43b1: Pushed
b57c79f4a9f3: Mounted from library/ubuntu
d60e01b37e74: Mounted from library/ubuntu
e45cfbc98a50: Mounted from library/ubuntu
762d8e1a6054: Mounted from library/ubuntu
part1: digest: sha256:d588f5e97300a48b2ccb921f710a54b1c0e7752c41e78084c51660f2c1e0f404 size: 3876
5.4 从云端获取image
sudo docker pull myname/repository_name:tag_name
5.5 仓库内容重复上传
上传同一个image,仅改变tag,信息如下:
The push refers to repository [docker.io/myname/test_flask]
a9997398912c: Layer already exists
701f7121b55c: Layer already exists
1838ad104791: Layer already exists
3cba4757f572: Layer already exists
6d6faae27f42: Layer already exists
8b69054ce635: Layer already exists
1cbfdca28530: Layer already exists
f04aa4a82ff3: Layer already exists
395d7eed0b80: Layer already exists
b694a04d82c4: Layer already exists
753e9587d8ff: Layer already exists
459543aab850: Layer already exists
808bddae43b1: Layer already exists
b57c79f4a9f3: Layer already exists
d60e01b37e74: Layer already exists
e45cfbc98a50: Layer already exists
762d8e1a6054: Layer already exists
part2: digest: sha256:d588f5e97300a48b2ccb921f710a54b1c0e7752c41e78084c51660f2c1e0f404 size: 3876
上传新的amd64架构的Ubuntu系统,信息如下:
b57c79f4a9f3: Layer already exists
d60e01b37e74: Layer already exists
e45cfbc98a50: Layer already exists
762d8e1a6054: Layer already exists
Ubuntu18.04: digest: sha256:f2557f94cac1cc4509d0483cb6e302da841ecd6f82eb2e91dc7ba6cfd0c580ab size: 1150
Linux系统在Docker中是分层存储的.
上传的image如下图所示(虽然有冲突,但是仓库仍会保存):
[参考文献]
[1]https://docs.docker.com/get-started/part2/
[2]https://www.cnblogs.com/xuanmanstein/p/7630606.html