软件测试|使用docker搞定 Python环境搭建

简介: 软件测试|使用docker搞定 Python环境搭建

前言

当我们在公司的电脑上搭建了一套我们需要的Python环境,比如我们的版本是3.8的Python,那我可能有一天换了一台电脑之后,我整套环境就需要全部重新搭建,不只是Python,我们一系列的第三方库都需要重新安装,那么我们有没有解决问题的方法,当然有,我们可以使用docker解决困扰我们的环境问题。

搜索镜像

docker search : 从Docker Hub(https://hub.docker.com)中搜索指定的镜像,例如我们要搜索一个基于centos7环境安装的Python3.8版本。命令如下:

docker search python
  • NAME 镜像仓库名称
  • DESCRIPTION 镜像描述信息
  • STARS 镜像收藏数
  • OFFICIAL 是否为docker官方发布的镜像
  • AUTOMATED 是否为自动化构建的镜像

输出如下:

[root@xxxx ~]# docker search python
NAME                             DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
python                           Python is an interpreted, interactive, objec…   4288                [OK]                
django                           Django is a free web application framework, …   847                 [OK]                
pypy                             PyPy is a fast, compliant alternative implem…   193                 [OK]                
kaggle/python                    Docker image for Python scripts run on Kaggle   123                                     [OK]
arm32v7/python                   Python is an interpreted, interactive, objec…   37                                      
centos/python-38-centos7         Platform for building and running Python 3.8…   36                                      
joyzoursky/python-chromedriver   Python with Chromedriver, for running automa…   33                                      [OK]
circleci/python                  Python is an interpreted, interactive, objec…   29                                      
nikolaik/python-nodejs           Python with Node.js                             18                                      [OK]
arm64v8/python                   Python is an interpreted, interactive, objec…   17                                      
centos/python-36-centos7         Platform for building and running Python 3.6…   17                                      
centos/python-27-centos7         Platform for building and running Python 2.7…   15                                      
iron/python                      Tiny Python Microcontainer                      9                                       
publicisworldwide/python-conda   Basic Python environments with Conda.           6                                       [OK]
dockershelf/python               Repository for docker images of Python. Test…   4                                       [OK]
i386/python                      Python is an interpreted, interactive, objec…   3                                       
bitnami/python                   Bitnami Python Docker Image                     3                                       [OK]
komand/python-plugin             DEPRECATED: Komand Python SDK                   2                                       [OK]
centos/python-34-centos7         Platform for building and running Python 3.4…   2                                       
muccg/python-base                Base images that use python                     1                                       [OK]
amd64/python                     Python is an interpreted, interactive, objec…   1                                       
ccitest/python                   CircleCI test images for Python                 0                                       [OK]
saagie/python                    Repo for python jobs                            0                                       
qbtrade/python                   python 3.6.5 with requirements last update s…   0                                       
openshift/python-33-centos7      DEPRECATED: A Centos7 based Python v3.3 imag…   0                                       
[root@xxxx ~]# 

拉取镜像

使用docker pull可以拉取我们想要拉取的镜像,命令如下:

docker pull centos/python-38-centos7

输出如下:

[root@xxxx ~]# docker pull centos/python-38-centos7
Using default tag: latest
latest: Pulling from centos/python-38-centos7
8ba884070f61: Pull complete 
c3dca185eb14: Pull complete 
ee720ba20823: Pull complete 
497ef6ea0fac: Pull complete 
ebf1fb961f61: Pull complete 
b8249f70ce00: Pull complete 
ebd817e2efe7: Pull complete 
d3d10dd0937c: Pull complete 
a8ad47ec8182: Pull complete 
Digest: sha256:d10c46b6db436357965a96716e9f5d230d9b1a58c6db1f0c4f43c1fb1994cd79
Status: Downloaded newer image for centos/python-36-centos7:latest
[root@xxxx ~]# 

查看镜像

使用docker images查看本地已经下载好的镜像,命令如下:

docker images

输出如下:

[root@xxxx ~]# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
centos/python-38-centos7   latest              b8d15efaa8ec        2 months ago        651MB
ubuntu                            15.10               9b9cb95443b5        2 years ago         137MB
training/webapp               latest              6fae60ef3446        4 years ago         349MB

运行交互式的容器

Docker会在隔离的容器中运行进程。当运行docker run命令时,Docker会启动一个进程,并为这个进程分配其独占的文件系统、网络资源和以此进程为根进程的进程组。
在容器启动时,镜像可能已经定义了要运行的二进制文件、暴露的网络端口等,但是用户可以通过docker run命令重新定义
最基本的docker run命令的格式如下:

$ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

比如我这里要启动centos7,进入交互模式,通过docker的两个参数 -i -t,让docker运行的容器实现"对话"的能力

  • t: 在新容器内指定一个伪终端或终端
  • i: 允许你对容器内的标准输入 (STDIN) 进行交互
docker run -i -t centos/python-38-centos7 /bin/bash

如下进入centos终端后,进python交互环境打印"hello, i'm Muller.",最后用exit退出,内容如下:

[root@xxxx ~]# docker run -i -t centos/python-36-centos7 /bin/bash
(app-root) python
Python 3.6.3 (default, Mar 20 2018, 13:50:41) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello, i'm Muller.")
hello, i'm Muller.
>>> exit()
(app-root) exit

后台模式启动

run加上-i -t是进交互模式,如果不想进交互模式直接执行脚本,可以直接使用run,比如echo "hello world",屏幕会输出"hello world"

docker run centos/python-38-centos7 /bin/echo "hello world"

如果不想在前台执行,一般我们运行环境会选择挂后台,加个-d 参数即可

docker run -d centos/python-38-centos7 /bin/echo "hello world"
[root@xxxx ~]# docker run centos/python-38-centos7 /bin/echo "hello world"
hello world
[root@xxxx ~]# docker run -d centos/python-38-centos7 /bin/echo "hello world"
1e5c22451bf2215f6c098e066b74363f1db9cde97e9ecea02947ccbbf2fa7e8f
[root@xxxx ~]# 

使用-d后台执行后,会发现下面多了一长串,这个就是容器的唯一id,可以通过这个id找到容器

ps查看容器

先run下 training/webapp

docker run -d -p 5000:5000 training/webapp python app.py

使用docker ps查看正在运行的容器

docker ps

输出如下:

[root@xxxx ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  
c9e8a325b145        training/webapp     "python app.py"     16 hours ago        Up 16 hours         0.0.0.0:32768->5000/tcp
[root@xxxx ~]# 

上面的echo "hello world"只是一个很简单的输出指令,执行完就关闭了,所以ps查找正在运行的查不到,可以加个-a参数,显示所有的容器,包括未运行的

ps 查找参数相关语法

  • -a :显示所有的容器,包括未运行的
  • -f :根据条件过滤显示的内容
  • --format :指定返回值的模板文件
  • -l :显示最近创建的容器
  • -n :列出最近创建的n个容器
  • --no-trunc :不截断输出
  • -q :静默模式,只显示容器编号
  • -s :显示总的文件大小

logs查看日志

可以通过容器id或者容器name去查运行的日志

docker logs [容器id]

docker logs [容器name]

停止容器

停止容器的话,可以用stop容器的id或者容器NAME名称

docker stop [容器id]

docker stop [容器name]

启动容器

docker start启动容器

docker start [容器id]

正在运行的容器,可以使用 docker restart 命令来重启

docker restart [容器id]

删除容器

docker rm 命令来删除不需要的容器

docker rm [容器id]

docker rm [容器name]

注:当删除运行中的容器时,需要先stop停止容器,再执行删除命令

总结

本文主要介绍了使用docker搭建Python环境,以及对于docker拉取镜像,docker容器的主要操作,包括运行容器,停止容器,删除容器等。希望对大家学习docker能有所帮助。

相关文章
|
9天前
|
jenkins 持续交付 Docker
docker之自定义制作镜像(python程序)
docker之自定义制作镜像(python程序)
|
8天前
|
Docker Python 容器
python检测docker compose文件是否正确
python检测docker compose文件是否正确
|
8天前
|
机器学习/深度学习 计算机视觉 Python
opencv环境搭建-python
本文介绍了如何在Python环境中安装OpenCV库及其相关扩展库,包括numpy和matplotlib,并提供了基础的图像读取和显示代码示例,同时强调了使用Python虚拟环境的重要性和基本操作。
|
2月前
|
数据可视化 安全 数据挖掘
streamlit (python构建web)之环境搭建
在微信订阅号中发现了一篇关于Streamlit的文章,激发了我的兴趣。Streamlit是一款专为数据科学家设计的开源Python库,能迅速将数据分析脚本转变为功能完备的Web应用。它简化了开发流程,支持轻松添加交互组件及动态展示图表、图像等,非常适合开发安全扫描工具。Streamlit基于Jupyter Notebook原理,通过Python脚本创建可视化和交互式的Web应用,易于部署分享。安装方法多样,可通过`pip install streamlit`快速安装,或通过Anaconda环境管理依赖。启动示例应用只需运行简单命令,即可体验自带的动画、绘图和数据展示等功能。
streamlit (python构建web)之环境搭建
|
2月前
|
运维 数据安全/隐私保护 Docker
深入浅出Python装饰器《Docker容器化技术在运维中的应用与实践》
【8月更文挑战第29天】装饰器在Python中是一个强大而神秘的存在,它能够轻松地改变一个函数的行为而不修改其源代码。本文将通过浅显易懂的语言和生动的比喻,带你一步步揭开装饰器的神秘面纱,从基本概念到实际应用,让你轻松掌握这一魔法般的工具。
|
2月前
|
Docker Python 容器
[docker]封装python的docker镜像
[docker]封装python的docker镜像
|
2月前
|
Docker Python 容器
5 分钟,教你用 Docker 部署一个 Python 应用!
5 分钟,教你用 Docker 部署一个 Python 应用!
|
2月前
|
Unix 程序员 C++
Day 1/100:Python 环境搭建!
Day 1/100:Python 环境搭建!
32 0
|
2月前
|
Linux Python Windows
超详细!新手友好Python+Django环境搭建
很高兴在这里陪您开始新的Python和Django学习之旅。下面是一个简单的指南,将引导您完成Python环境的安装、虚拟环境的创建、以及如何使用Django搭建一个基本的Web项目。
|
3月前
|
JavaScript 前端开发 区块链
智能合约泰山众筹DAPP系统开发丨Python环境搭建
智能合约的开发与环境搭建:(仅供参考) nodejs和npm node.js安装(含NPM) node.js官网下载16.15.0LTS版本,安装,修改路径,安装包不需要选择 命令行查看是否安装成功 node -v,显示为v16.15.0
下一篇
无影云桌面