Docker学习(7)部署搭建python开发环境,运行应用程序

搭建开发环境使用Docker Hub实践操作流程

在官网下载你需要的Python版本镜像,本地

nvidia@nvidia-desktop:~$ docker image pull python:3.8.0-alpine3.10
3.8.0-alpine3.10: Pulling from library/python
8bfa91304040: Pull complete
1b8b9474c28b: Pull complete
9f7206d76a9f: Pull complete
66cb16ce0720: Pull complete
690c65360a3e: Pull complete
Digest: sha256:7f465d82a49e092b609fa97cea8ea761c9aa4fa6cab05f4876150e28bf16bcc3
Status: Downloaded newer image for python:3.8.0-alpine3.10
docker.io/library/python:3.8.0-alpine3.10

进入交互终端

nvidia@nvidia-desktop:~$ docker container run --rm -it python:3.8.0-alpine3.10
Python 3.8.0 (default, Nov 15 2019, 02:06:45)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>
>>>
>>> ls
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ls' is not defined
>>> help()

Welcome to Python 3.8's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.8/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> exit()

python和pip版本确认

nvidia@nvidia-desktop:~$ docker container run --rm -it python:3.8.0-alpine3.10 python -V
Python 3.8.0
nvidia@nvidia-desktop:~$ docker container run --rm -it python:3.8.0-alpine3.10 pip -V
pip 19.3.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

Linux版本确认

nvidia@nvidia-desktop:~$ docker container run --rm -it python:3.8.0-alpine3.10 cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.10.3
PRETTY_NAME="Alpine Linux v3.10"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

执行本地python应用程序

import numpy as np
myarray = np.array([1,2,3,4,5])
print(myarray)
nvidia@nvidia-desktop:~$ mkdir tmp
nvidia@nvidia-desktop:~$ cd tmp/
nvidia@nvidia-desktop:~/tmp$ vi main.py

import numpy as np
myarray = np.array([1,2,3,4,5])
print(myarray)

nvidia@nvidia-desktop:~/tmp$ pwd
/home/nvidia/tmp
nvidia@nvidia-desktop:~/tmp$ docker container run --rm -it -v /home/nvidia/tmp:/workfolder:ro python:3.8.0-alpine3.10 python /workfolder/main.py
Traceback (most recent call last):
  File "/workfolder/main.py", line 1, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

执行到这一步虽然报错了,但是说明程序生效了,因为他报的错是说没找到numpy,而不是找不到程序文件。

建立自己的Python镜像,之前使用atlas500小站做过,再演示一次。

nvidia@nvidia-desktop:~/tmp$ vi Dockerfile
nvidia@nvidia-desktop:~/tmp$ dock\er image build -t goodpython:v0.1 .
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM python:3.8
3.8: Pulling from library/python
80dae1b9d879: Pull complete
13ae98695cf4: Pull complete
09a74e69230a: Pull complete
0e04cd4cd2ee: Pull complete
d9ce95a1e837: Pull complete
5f071eca0617: Pull complete
937b771f1a56: Pull complete
c162638ea94f: Pull complete
dd82b923e6a7: Pull complete
Digest: sha256:09fb8210b6822d357da41ccd7f8f2a164c6fe8f268b230575456d12c31a216fb
Status: Downloaded newer image for python:3.8
 ---> b8eb001ea805
Step 2/5 : RUN pip3 install install numpy
 ---> Running in e222d01e1995
Collecting install
  Downloading install-1.3.5-py3-none-any.whl (3.2 kB)
Collecting numpy
  Downloading numpy-1.24.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (14.0 MB)








     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.0/14.0 MB 997.4 kB/s eta 0:00:00
Installing collected packages: numpy, install
Successfully installed install-1.3.5 numpy-1.24.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 22.0.4; however, version 23.0.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container e222d01e1995
 ---> c87bc4df0532
Step 3/5 : RUN mkdir -p /workfolder
 ---> Running in 7e327a299d35
Removing intermediate container 7e327a299d35
 ---> 7219908f5a59
Step 4/5 : COPY ./main.py /workfolder/
 ---> ffa6a91796dc
Step 5/5 : CMD [ "python", "/workfolder/main.py" ]
 ---> Running in d422d20860a1
Removing intermediate container d422d20860a1
 ---> c3f36abddba6
Successfully built c3f36abddba6
Successfully tagged goodpython:v0.1

这样自己的镜像就建立好了,

nvidia@nvidia-desktop:~/tmp$ docker images
REPOSITORY        TAG                IMAGE ID       CREATED          SIZE
goodpython        v0.1               c3f36abddba6   35 seconds ago   935MB
python            3.8                b8eb001ea805   13 days ago      858MB
nvidia@nvidia-desktop:~/tmp$ docker container run --rm -it goodpython:v0.1
[1 2 3 4 5]

这样的话,开发起来主要管理源码和Dockerfile文件即可环境配置不在繁琐,提高工作效率。

猜你喜欢

转载自blog.csdn.net/weixin_46151178/article/details/129191126