- 安装
#centos
sudo curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
- 加速
到阿里容器镜像服务查看具体操作
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://yours.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
官网给的大题使用流程
- Create a sample Python application
- Create a new Dockerfile which contains instructions required to build a Python image
- Build an image and run the newly built image as a container
- Set up volumes and networking
- Orchestrate containers using Compose
- Use containers for development
- Configure a CI/CD pipeline for your application using GitHub Actions
- Deploy your application to the cloud
Python程序的Docker样例
https://docs.docker.com/language/python/build-images/
简单应用示例
创建一个简单的flask应用和目录python-docker
$ cd /path/to/python-docker
$ pip3 install Flask
$ pip3 freeze > requirements.txt
$ touch app.py
添加代码
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker!'
测试一下代码
$ python3 -m flask run
$ curl http://localhost:5000
127.0.0.1 - - [22/Sep/2020 11:07:41] "GET / HTTP/1.1" 200 -
编写Dockerfile文件,Dockerfile是一个包含打镜像所需的所有命令,执行docker build时会连续执行里面的命令,新建名为Dockerfile的文件
# vim Dockerfile
# 首先添加基础镜像,会继承自其他镜像,这个镜像也可以自己定制一个专属的,方便在做其他项目时复用
FROM python:3.8-slim-buster
# 设置默认路径,这是容器里的路径,并不是本机的
WORKDIR /app
# 复制requirements.txt到/app目录下
COPY requirements.txt requirements.txt
# 安装依赖
RUN pip3 install -r requirements.txt
# 复制代码到/app
COPY . .
# 要执行的命令
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
当前目录结构如下
python-docker
|____ app.py
|____ requirements.txt
|____ Dockerfile
打镜像
$ docker build --tag python-docker .
[+] Building 2.7s (10/10) FINISHED
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 203B
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load metadata for docker.io/library/python:3.8-slim-buster
=> [1/6] FROM docker.io/library/python:3.8-slim-buster
=> [internal] load build context
=> => transferring context: 953B
=> CACHED [2/6] WORKDIR /app
=> [3/6] COPY requirements.txt requirements.txt
=> [4/6] RUN pip3 install -r requirements.txt
=> [5/6] COPY . .
=> [6/6] CMD [ "python3", "-m", "flask", "run", "--host=0.0.0.0"]
=> exporting to image
=> => exporting layers
=> => writing image sha256:8cae92a8fbd6d091ce687b71b31252056944b09760438905b726625831564c4c
=> => naming to docker.io/library/python-docker
查看本地镜像,一个是from的基础镜像,另一个就是我们刚打包的
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-docker latest 8cae92a8fbd6 3 minutes ago 123MB
python 3.8-slim-buster be5d294735c6 9 days ago 113MB
给镜像打tag
$ docker tag python-docker:latest python-docker:v1.0.0
打完tag以后不会增加镜像,只是加了一个tag, IMAGE ID是相同的
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-docker latest 8cae92a8fbd6 4 minutes ago 123MB
python-docker v1.0.0 8cae92a8fbd6 4 minutes ago 123MB
python 3.8-slim-buster be5d294735c6 9 days ago 113MB
如果docker images返回的镜像,可以删除省空间,docker rmi
用于删除
如果提示需要强制,docker rmi -f即可,如果是docker images -a,会看到一堆,这种是正常的,docker是分层的,dockerfile里每条指令都会产生intermediate layer,也就会有,这种就乖乖的留着
将镜像以容器运行
运行容器,注意-p/–publish
$ docker run -p 8000:5000 python-docker
检查是否运行成功
$ curl localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused