Docker學習筆記:Alpine鏡像+Python3安裝+http服務器


  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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM