软件测试 您所在的位置:网站首页 如何用python搭建服务器 软件测试

软件测试

2023-04-03 03:40| 来源: 网络整理| 查看: 265

前言

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

搜索镜像

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

docker search pythonNAME 镜像仓库名称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能有所帮助。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有