天地维杰网

人如秋鸿来有信,事若春梦了无痕


  • 首页

  • Redis

  • java

  • linux

  • 日常问题

  • Spring和Springboot

  • Mac相关

  • 中间件

  • 架构

  • python

  • 前端

  • jvm

  • c语言

  • web3

  • 归档

  • 关于

  • 搜索
close

Mac使用Docker搭建python测试执行环境

时间: 2021-12-29   |   分类: mac   python     |   原创   |   阅读: 856 字 ~2分钟

首先拉取 python的镜像

docker pull python:3.5

新建一个文件目录/VscodePythonProjects,并创建python测试文件pyth.py

#!/usr/bin/python
print("Hello, World!");

使用docker run 执行python文件

~% docker run --rm -v /VscodePythonProjects:/usr/src/file -w /usr/src/file python:3.5 python pyth.py
Hello, World!

参数说明:

--rm 执行完后删除容器,避免残留过多容器
-v 将主机的py文件目录挂载到容器中的/usr/src/file
-w 指定容器的/usr/src/file目录为工作目录
python:3.5 指定镜像

在~/.ssh目录里,增加 py文件,内容如下:

#!/bin/bash
echo `docker run --rm -v /VscodePythonProjects:/usr/src/file -w /usr/src/file python:3.5 python $1`

这样在任意的目录里直接执行py命令了

~% py pyth.py
Hello, World!

当然,在mac系统上面直接安装python也能实现相同的效果。但是如果不想污染系统,也能进行Python测试的话,这种也是一种方式。

上述的方式,只能执行没有依赖的python代码,但是比如说我要运行引入了redis模块的代码,就会报redis模块不存在的问题,这种情况,我们就需要在docker的容器中安装 redis模块了。

~% docker run --name "python3.5" -it -v /VscodePythonProjects:/usr/src/file  -w /usr/src/file python:3.5 /bin/bash
root@2870e7cf8e74:/usr/src/file#

执行上面命令将进入 创建的python容器的命令行。然后执行命令安装redis模块,使用国内镜像可以更快执行

root@2870e7cf8e74:/usr/src/file# pip install redis -i https://pypi.tuna.tsinghua.edu.cn/simple
DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting redis
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl (72 kB)
     |████████████████████████████████| 72 kB 668 kB/s
Installing collected packages: redis
Successfully installed redis-3.5.3
WARNING: You are using pip version 20.2.3; however, version 20.3.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

然后就可以正常运行如下引入redis模块的python文件了,此处要注意,host,参考代码是localhost,是无法执行的,要将host换为安装了redis的宿主机的ip。

import redis

r = redis.Redis(host='10.2.147.43', port=6379, decode_responses=True)
r.set('name', 'runoob')
print(r['name'])
print(r.get('name'))
print(type(r.get('name')))
root@2870e7cf8e74:/usr/src/file# python redis-test.py
runoob
runoob
<class 'str'>

我们在容器中执行exit命令以后,使用docker ps -a 可以看到 python容器变为exited状态

VscodePythonProjects % docker ps -a
CONTAINER ID   IMAGE                                       COMMAND                  CREATED         STATUS                      PORTS                                        NAMES
2870e7cf8e74   python:3.5                                  "/bin/bash"              5 minutes ago   Exited (0) 26 seconds ago                                                python3.5

如果我们想再次使用这个容器,只要启动容器就可以了

VscodePythonProjects % docker start 2870e7cf8e74
2870e7cf8e74
VscodePythonProjects % docker ps -a
CONTAINER ID   IMAGE                                       COMMAND                  CREATED         STATUS                      PORTS                                        NAMES
2870e7cf8e74   python:3.5                                  "/bin/bash"              7 minutes ago   Up About a minute                                                        python3.5
VscodePythonProjects % docker exec -it 2870e7cf8e74 /bin/bash
root@2870e7cf8e74:/usr/src/file# python redis-test.py
runoob
runoob
<class 'str'>

参考文档:

https://blog.csdn.net/u013355826/article/details/79963334

https://blog.csdn.net/gf19960103/article/details/109489632

#mac# #python# #docker#
python执行request请求
详解Linux防火墙iptables禁IP与解封IP常用命令
  • 文章目录
  • 站点概览
不与天斗Domino

不与天斗Domino

Programmer & Architect

183 日志
15 分类
224 标签
© 2013 - 2023 天地维杰网 京ICP备13019191号-1
Powered by - Hugo v0.63.2
Theme by - NexT
0%