Docker 配置 centos7+python3.9.6+jupyter
之前配过一次centos7+python3.10.6,但是python3.10.6里没有ssl,这个弄了好久没成功,不知道哪里不对,用了python3.9.6版本就可以了,记录下。
配置文件
文件夹下就是这些了,Dockerfile就是docker的构建文件,requirements.txt就是python依赖的第三方库,最后.tar.xz就是Python-3.9.6了,也可以进去下载,不过在这下好了,里面就快了。

Dockerfile文件内容如下
FROM centos:7
# yum 更新
RUN set -ex \
&& yum -y install zlib-devel bzip2-devel libffi-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make wget \
&& yum clean all \
&& mkdir /usr/local/python \
&& mkdir /home/data
# 复制所有文件 当前目录
COPY . /home/data
RUN set -ex \
&& cd /home/data \
&& tar -xvJf Python-3.9.6.tar.xz \
&& cd Python-3.9.6 \
&& ./configure prefix=/usr/local/python \
&& make && make install \
&& yum install -y epel-release \
&& yum install -y python-pip \
&& rm -f /usr/bin/python \
&& rm -f /usr/bin/pip \
&& ln -s /usr/local/python/bin/python3 /usr/bin/python \
&& ln -s /usr/local/python/bin/pip3 /usr/bin/pip
# 修复因修改python版本导致yum失效问题
RUN set -ex \
&& sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/bin/yum \
&& sed -i "s#/usr/bin/python#/usr/bin/python2.7#" /usr/libexec/urlgrabber-ext-down \
&& yum install -y deltarpm
# 安装python
RUN set -ex \
&& python -V \
&& python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
# # 安装yum 工具
RUN set -ex \
&& yum install -y lrzsz \
&& yum install -y net-tools \
&& yum install -y git \
&& yum install -y zip unzip
# 启动配置
RUN set -ex \
&& cd /home \
&& pip list \
&& echo "构建成功"
要是docker慢的话,可以把国内的一些源加进去。
docker构建以及自定义
build
docker build -t demo_p:v1 .
run
经过这一步会多一个镜像,运行
docker run -itd --name centos-test demo_p:v1
exec
进去容器
docker exec -it centos-test /bin/bash
查看信息
[root@3fb71a49165c /]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@3fb71a49165c /]# pip list
Package Version
---------- -------
pip 22.3
setuptools 56.0.0
[root@3fb71a49165c /]# python
Python 3.9.6 (default, Oct 17 2022, 07:37:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@3fb71a49165c /]#
自定义
刚刚 requirements.txt在home/data下
pip install --upgrade pip -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
cd home/data
pip install -r requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
下载jupyter
需要有–user
pip install jupyter -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com --force-reinstall --user
下载的时候会报这个警告,就是说没有在环境变量里。
The scripts jupyter-bundlerextension, jupyter-nbextension, jupyter-notebook and jupyter-serverextension are installed in '/root/.local/bin' which is not on PATH.
也会发现jupyter没有加进环境变量,找不到这个命令。
[root@3fb71a49165c data]# jupyter
bash: jupyter: command not found
配置环境变量
添加一下系统变量即可。
打开,插入,刷新
vim /etc/profile
export PATH=/root/.local/bin:${PATH}
source /etc/profile
这下就有了
[root@bdeec8d5baea /]# jupyter notebook
[C 11:27:56.941 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.
将刷新的语句,存在启动里,要不每次进来容器都要刷一次,麻烦。
sudo vi ~/.bashrc
在最后添加
source /etc/profile
就OK了。
导出包
docker commit 3fb71a49165c hahaha:v2
看下镜像
(base) 192:docker_p Hekai$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hahaha v2 2001e99e0378 About a minute ago 4.31GB
demo_p v1 91d6b1aac935 4 hours ago 1.51GB
运行
其中/Users/Hekai/Desktop可替换为本地路径
docker run -it -p 8888:8888 -v /Users/Hekai/Desktop:/workspace hahaha:v2 /bin/bash
再在容器里键入
jupyter notebook --ip 0.0.0.0 --allow-root
[root@2c63de413d84 /]# jupyter notebook --ip 0.0.0.0 --allow-root
[I 11:39:52.251 NotebookApp] Serving notebooks from local directory: /
[I 11:39:52.251 NotebookApp] Jupyter Notebook 6.5.1 is running at:
[I 11:39:52.251 NotebookApp] http://2c63de413d84:8888/?token=fa79fcfc321957887ecd25752ce99daf320caf0327cdaf1c
[I 11:39:52.251 NotebookApp] or http://127.0.0.1:8888/?token=fa79fcfc321957887ecd25752ce99daf320caf0327cdaf1c
[I 11:39:52.251 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 11:39:52.259 NotebookApp] No web browser found: could not locate runnable browser.
[C 11:39:52.260 NotebookApp]
To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-31-open.html
Or copy and paste one of these URLs:
http://2c63de413d84:8888/?token=fa79fcfc321957887ecd25752ce99daf320caf0327cdaf1c
or http://127.0.0.1:8888/?token=fa79fcfc321957887ecd25752ce99daf320caf0327cdaf1c
链接也给出了,复制一个去浏览器就可以了,目录下面有个workspace,就是咱们本地路径了。

就酱。
本文来自博客园,作者:赫凯,转载请注明原文链接:https://www.cnblogs.com/heKaiii/p/17137369.html

浙公网安备 33010602011771号