首页 > 编程语言 >软件测试|使用docker搞定 Python环境搭建

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

时间:2023-03-17 19:05:51浏览次数:47  
标签:容器 centos Python centos7 python docker 软件测试

前言

当我们在公司的电脑上搭建了一套我们需要的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能有所帮助。


标签:容器,centos,Python,centos7,python,docker,软件测试
From: https://blog.51cto.com/u_15640304/6127829

相关文章

  • docker图形化界面管理与监控
    一、cadvisordockerpull google/cadvisordockerrun-it-p8890:8080-v/var/run:/var/run-v/db/docker:/var/lib/docker:ro-v/sys:/sys:rogoogle/cadvisor/bi......
  • 除了docker外,还有哪些容器运行时?
    引用来源:https://landscape.cncf.io/card-mode?category=container-runtime&grouping=category除了dockder外,还有containerd、cri-o等下图列举了一些常见的containerrun......
  • md5解密 python
    MD5是一种不可逆的哈希算法,这意味着您不能直接从MD5哈希值“解密”出原始数据。然而,您可以尝试使用暴力破解或查找表(如彩虹表)来猜测原始数据。暴力破解是一种尝试所有可能......
  • 用 DolphinDB 和 Python Celery 搭建一个高性能因子计算平台
    因子挖掘是量化金融研究和交易的核心工作。传统的开发流程中,通常使用Python从关系型数据库(如SqlServer,Oracle等)读取数据,在Python中进行因子计算。随着证券交易规模......
  • 解放双手,python实现自动刷抖音短视频
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • python利用ffmpeg实现声音视频传输
    1.背景由于项目需求,需要用到视频音频同步传输到服务器并获取播放,这里用到了推流的知识,由于项目是python项目,自己django框架还不熟悉,这里代码等着后续给补上2.介绍直播......
  • Python解析url
    #coding:utf-8try:fromurllib2importurlparseexcept:fromurllibimportparseasurlparsed=urlparse.urlparse("http://www.test.com/a/b/c?name=......
  • 如何在Docker下部署nacos并注册Java服务
    在Docker下部署nacos并注册Java服务并不是一个困难的事,难就难在碰到Java服务注册碰到的异常,下面分享一下如何在Docker中部署Docker,如何注册Java服务,并且来解决这个异常的......
  • Docker安装Centos7
    dockerrun-itd--namecentos22024-p22021:21-p22022:22-p22024:23-p22025:25-p22080:80-p27000:7000-p27001:7001-p27002:7002-p27003:7003-p27004......
  • python 设置代理 proxy
    一、安装pip在较高的python版本中,pip会随着一起发布。但也有用到低版本python的情况,此时就需要手动安装pip。python安装pip的时候非常慢,如果局域网内有代理可......