目录
1、先了解下Dockerfile作用
2、直奔主题
2.1、工程代码
2.2 可能遇到的问题:(反正我遇到了)
2.2.1 requirements.txt找不到
2.2.2 pip install -i xxxx 换源换不了
2.2.3 docker创建镜像失败后咋办
1、先了解下Dockerfile作用
Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。
具体可以看
菜鸟教程:https://www.runoob.com/docker/docker-dockerfile.html Docker Dockerfile
b站视频学习:使用Docker封装python应用_哔哩哔哩_bilibili 使用Docker封装python应用
2、直奔主题
看了还是得自己上手部署才有效果,我是在上面的学习后来实践的,视频可能有部分问题
视频项目结果如下:
视频讲的也就是我上面2个图的内容,主要编写左侧2个文件
requirements.txt 可以根据这个命令获取
pip freeze >requirements.txt
因此Dockerfile文件编写比较重要了!!!
我来分享下,简单模拟
2.1、工程代码
其实视频的up主的fruit是空文件夹,但我们可以把它看作是其他子模块,而外面的fruit.py是我们的init.py,requirements.txt是我们要给项目安装的库
于是,我的模拟是这样的
学习做的Dockerfile模板:(这个是百度后改了一点的)
# Use an official Python runtime as a parent image
FROM python:3.6
# Set the working directory to /app
WORKDIR /home/student/wubindong/
# Copy the current directory contents into the container at /app
COPY . /home/student/wubindong/
# Install any needed packages specified in requirements.txt
RUN pip install -r /home/student/wubindong/requirements.txt
# Make port 80 available to the world outside this container
# EXPOSE 80
# Run app.py when the container launches
CMD ["python", "main.py"]
基本命令是这样的,详情查看Docker Dockerfile | 菜鸟教程
main.py如下
随便写个就行了,但是要有死循环,因为docker容器会因为程序结束而结束
import numpy as np
def bubbleSort(lst:list):
n = len(lst)
print('before sort')
print(lst)
mark = True
for i in range(n-1):
mark = True
for j in range(n-1,i,-1):
if lst[i]>lst[j]:
lst[i],lst[j]=lst[j],lst[i]
mark = False
if mark:
break
print('after sort')
print(lst)
def main():
lst = np.random.randint(1,11,10).tolist()
# [1,3,2,6,3,8,2,457,65,224,45,67,24,86,24,757,24,534] # np.random.randint(1,11,10).tolist()
bubbleSort(lst)
if __name__ == '__main__':
main()
while True:
pass
我故意使用numpy,因为下载的python一开始是没有numpy,刚好可以试试让它下载numpy
requirements.txt内容如下
2.2 可能遇到的问题:(反正我遇到了)
2.2.1 requirements.txt找不到
那说明文件路径不对了。 我直接选择绝对路径,绝对可以了。
对了,我怀疑这个下面的指令有问题。”/home/student/wubindong/“是我虚拟机存储项目的位置,我真的按它那样写,然后一直没找到,反过来就有了,不知道是不是版本问题,知道的朋友可以评论区指点我一下.
2.2.2 pip install -i xxxx 换源换不了
先展示下pip install -i xxx 成功应该用的方式,这个是临时的,每次都得来一下
https://pypi.tuna.tsinghua.edu.cn/simple 包名
嫌麻烦,那我直接改系统镜像源了,一了百了
1. 做个副本,防止失败
cp /etc/apt/sources.list /etc/apt/sources.list
2. 修改
sudo vim /etc/apt/sources.list
3. 更新 apt 本地索引
sudo apt update
要在sources.list增加的内容,放到最前面吧,这里的链接我也是抄的,管他的,虽然多,只要有一个能加速就行。要是你觉得没有变化,那你去找下下一个,替换,反正我在找的时候也是这样。
# deb cdrom:[Ubuntu 20.04 LTS _Focal Fossa_ - Release amd64 (20200423)]/ focal main restricted
##阿里云
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
##Ubuntu官方中国
deb http://cn.archive.ubuntu.com/ubuntu/ zesty main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ zesty-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ zesty-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ zesty-backports main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ zesty-proposed main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ zesty main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ zesty-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ zesty-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ zesty-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ zesty-proposed main restricted universe multiverse
##网易源
deb http://mirrors.163.com/ubuntu/ zesty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-backports main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ zesty-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ zesty-proposed main restricted universe multiversedeb
deb https://mirrors.ustc.edu.cn/ubuntu/ cosmic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ cosmic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ cosmic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ cosmic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ cosmic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ cosmic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ cosmic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ cosmic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ cosmic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ cosmic-proposed main restricted universe multiverse
2.2.3 docker创建镜像失败后咋办
看看docker ps -a -q有啥
直接上 docker rm -f $(docker ps -a -q)
再看看docker images 有啥
对着Image ID就是删
好了,今天搞个简单测试遇到的问题解决了。