1. 编写Dockerfile文件使用最新的Alpine镜像并安装Python3环境,如下:

    因为python高于3.4则不会默认安装pip,需要手动安装。

    试了很多其他办法都没安装上,唯有下载get-pip.py进行安装。

    这里说一下cherrypy版本不能高于9.0.0,负责等下import wsgiserver会出错,因为wsgiserver后面移出cherrypy了。
FROM alpine
RUN mkdir /install
COPY get-pip.py /install
RUN apk update
RUN apk add bash
RUN apk add python3
RUN python3 /install/get-pip.py
RUN pip install bottle
#RUN pip3 install cherrypy
RUN pip3 install "cherrypy>=3.0.8,<9.0.0"
ADD server.py /root/server.py
EXPOSE 8080:8080
CMD /usr/bin/python3 /root/server.py
  1. 在Dockerfile文件目录下执行下面命令可以创建基于python3的镜像:
$ docker build -t test_python38_http .





  1. 这样基于alpine的python3镜像创建成功了,用下面命令可以查看:
$ docker images

  1. 镜像创建成功后输入命令则可以启动镜像服务了
$ docker run -p 8080:8080 -it test_python38_http

  1. 最后打开浏览器,输入url测试:
http://192.168.99.100:8080/HTTP_SET_COOKIE_LIST_3

关于这个IP也是一个坑,大家可以在启动Docker时看到一个default IP,并不是其他的哦。

  1. 最后贴上http代码
import bottle
from bottle import post, get, delete, route, request
from cherrypy import wsgiserver
import time
import signal @route('/HTTP_SET_COOKIE_LIST_3')
def set_cookie():
print("VA_HTTP HTTP_SET_COOKIE_LIST_3")
count = int( bottle.request.cookies.get('counter', '0') )
count += 1
bottle.response.set_cookie('counter', str(count))
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_COOKIE_LIST_3" @route('/HTTP_SET_COOKIE_LIST_4')
def verify_cookie():
count = int( bottle.request.cookies.get('counter', '0') )
if count == 0 :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 HTTP_SET_COOKIE_LIST_4"
else :
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_COOKIE_LIST_4" @route('/<id>')
def HttpRoute_handle(id):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 "+ id @route('/HTTP_TIMEOUT')
def HttpTime_handle():
time.sleep(30)
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 TIME" #Get for Test HTTP_DO_REQUEST_1, HTTP_SET_POST_DATA_5, HTTP_SET_METHOD_3
@get('/<id>')
def HttpGet_handler(id):
if id == "HTTP_SET_METHOD_3":
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 GET"
elif (id == "HTTP_SET_HEADER_2_0") or (id == "HTTP_SET_HEADER_2_1") or (id == "HTTP_SET_HEADER_2_2"):
if(request.headers.get('FirstHeader') == 'value') and not(request.headers.get('SecondHeader')) and (id == "HTTP_SET_HEADER_2_0"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 " + "FirstHeader"
elif (request.headers.get('FirstHeader') == 'value') and (request.headers.get('SecondHeader') == 'value') and (id == "HTTP_SET_HEADER_2_1"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 " + "FirstHeader" + "SecondHeader"
elif not(request.headers.get('FirstHeader')) and not(request.headers.get('SecondHeader')) and( id == "HTTP_SET_HEADER_2_2"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 "
else:
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 HTTP_SET_HEADER_2"
else:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 "+ id #Post for Test : HTTP_SET_POST_DATA_3, HTTP_SET_POST_DATA_4, HTTP_SET_POST_DATA_5, HTTP_SET_METHOD_3
@post('/<id>')
def HttpSetPostData_handler(id):
if id == "HTTP_SET_POST_DATA_5" :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id
elif id == "HTTP_SET_METHOD_3":
Data = bottle.request.forms.get('data')
Cata = bottle.request.forms.get('cata')
if Data:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 POST " + "data=" + Data
elif Cata:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 POST " + "cata=" + Cata
else:
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id
else:
testName = bottle.request.forms.get('testname')
if testName == "HTTP_SET_POST_DATA_4":
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 " + id
elif testName == id:
#HTTP_SET_POST_DATA_3
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 " + testName
else :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id def stop_server(*args, **kwargs):
server.stop() app = bottle.default_app()
server = wsgiserver.CherryPyWSGIServer(("0.0.0.0", 8080), app)
signal.signal(signal.SIGINT, stop_server)
signal.signal(signal.SIGTERM, stop_server)
signal.signal(signal.SIGHUP, stop_server)
print("VA_HTTP TestSuite Server Started")
server.start()

Docker学习笔记:Alpine镜像+Python3安装+http服务器的更多相关文章

  1. Docker学习笔记一 概念、安装、镜像加速

    本文地址:https://www.cnblogs.com/veinyin/p/10406378.html  Docker 是一个容器,可以想象成一个轻便的虚拟机,但不虚拟硬件和操作系统. 优点:启动快 ...

  2. Docker学习笔记【三】安装Redis

    项目中使用到Redis,平常都是别人搭建的,今天试着在Google Cloud Platform 上搭建一个学习环境. 1.使用 docker pull redis 从docker hub中下载镜像 ...

  3. Docker学习笔记:镜像、容器、数据卷

    核心概念 镜像:一个只读的模板,类似虚拟机的镜像. 容器:可以理解为镜像的一个运行实例.运行时类似于沙箱,多个容器互相独立. 仓库:存放镜像文件的地方. 镜像 命令表格 命令 解释 选项 docker ...

  4. Docker学习笔记之镜像与容器

    0x00 概述 镜像和容器作为 Docker 里最基础的概念,我们很有必要了解 Docker 对它们的很多定义以及其他与它们有关的知识.在这一小节里,我们就专门针对镜像与容器两个概念展开,细致的梳理与 ...

  5. docker学习笔记2--对镜像/容器的命令操作

    Docker启动一个Centos镜像 我们下载完成一个Centos镜像之后,开始启动 docker run -d -i -t <imageID> /bin/bash 这样就能启动一个一直停 ...

  6. docker学习笔记-常用镜像相关命令

    docker images # 1.使用 [root@iZbp13qr3mm4ucsjumrlgqZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED ...

  7. docker学习笔记一:基本安装和设置容器静态ip

    docker是一个lxc升级版的容器类虚拟环境,具有快速部署,灵活,易迁移的虚拟机模式,现在各大公司已经开始广泛使用为了自己方便学习linux,需要多台虚拟机环境,但是vmware开启多台虚拟机时需要 ...

  8. Docker学习笔记--2 镜像的创建

    如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等应用服务环境,有下面三种方法: 1,根据系统镜像创建Docker容器,这时容器就相当于是一个虚拟机,进入容 ...

  9. Docker学习笔记-CentOS7镜像

    前言: 环境:centos7.5 64 位 正文: 第一步:下载centos7镜像 docker pull centos 第二步:建立centos7的容器 sudo docker run --priv ...

随机推荐

  1. 2014年 实验五 Internet与网络工具的使用

    实验五 Internet与网络工具的使用 [实验目的]   ⑴.FTP服务器的架设和客户端的使用. ⑵.使用云盘和云笔记应用 ⑶.运用QQ的远程协助功能. (4).默认安装foxmail软件,进行邮件 ...

  2. Azure Cosmos DB (三) EF Core 操作CURD

    一,引言 接着上一篇使用 EF Core 操作 Azure CosmosDB 生成种子数据,今天我们完成通过 EF Core 实现CRUD一系列功能.EF Core 3.0 提供了CosmosDB 数 ...

  3. Get提交方式中文乱码

    Get提交方式中文乱码 今天在servlet使用中,在Get方法中获取提交的中文参数,发现是乱码,我用的是Tomcat7. 在Tomcat9中: get方式的参数是放在请求头中,而Tomcat9对请求 ...

  4. 【Luogu】P1613 跑路

    [Luogu]P1613 跑路 一.题目 题目描述 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是为了保住自己的工资 ...

  5. Harbor 安装教程

    Harbor 安装教程 一. CentOS设置 1. 更换阿里源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com ...

  6. 第三十四章 Linux常规练习题(一)参考答案

    一.练习题一 1.超级用户(管理员用户)提示符是___#_,普通用户提示符是___$_. 2.linux关机重启的命令有哪些 ? 关机命令 重启命令 shutdown -h now shutdown ...

  7. <!DOCTYPE>,<address>,<applet>的用法

    希望以下内容能让大家有所收获 HTML <!DOCTYPE> 标签 实例 <!DOCTYPE html> <html> <head> <title ...

  8. 集合与map

  9. vue项目 封装api

    设计思路 为了加强项目的可维护性,建议将请求接口api进行统一封装, 一个常规项目的基础地址一般为唯一,所以考虑将基础地址设定一个变量 let  baseUrl: "xxxxxx" ...

  10. robotframework执行自动化不能转换为h5页面的问题解决

    电脑换成win10后,搭建了robotframework环境,执行自动化发现页面不支持h5页面了.请教了大佬,解决办法如下: 1.切换到DOS环境下,执行pip list命令,查看selenium2l ...