1. 本地新建一个目录config,该目录下新建一个sources.list文件 ,目的是使用国内镜像源安装
sources.list具体内容如下:
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
2.拷贝项目到config目录下:
3. config目录下新建dockerfile文件
dockerfile具体内容如下:
# 指定所创建镜像的基础镜像
FROM ubuntu
LABEL author="xxx"
#用ubuntu国内源替换默认源
RUN rm /etc/apt/sources.list
COPY sources.list /etc/apt/sources.list
WORKDIR /app
ADD . /app
#安装python
RUN apt-get update
RUN apt-get install -y python python-pip
RUN pip install --upgrade pip
# 安装依赖
RUN pip install -r requirements.txt
# 声明镜像内服务监听的端口
EXPOSE 80
CMD ["python", "app.py"]
4. 创建名称为hello的镜像,. 表示dockerfile文件所在目录
docker build -t hello .
5. 启动镜像
docker run -itd -p 4000:80 hello # 守护模式启动镜像
6.python操作脚本启动
test.sh脚本文件
#! /bin/bash
docker run -itd -p 4000:80 test # test为容器名
python代码
import subprocess
status, res = subprocess.getstatusoutput('/bin/bash /Users/Desktop/shell/test.sh') # status为0表示执行成功
print(status)