測試用到的python文件:python
#!/usr/bin/env python3#conding: utf-8from http.server import HTTPServer, BaseHTTPRequestHandlerimport jsonimport sysimport socketdef hostname(): return socket.gethostname()def ipa(): return socket.gethostbyname(hostname())data = {hostname(): ipa()}def httpserver(ipadd='127.0.0.1',sport=8888): host = (ipadd, sport) server = HTTPServer(host, Resquest) server.serve_forever()class Resquest(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() self.wfile.write(json.dumps(data).encode())if len(sys.argv) == 1: httpserver()elif len(sys.argv) == 3: httpserver(sys.argv[1],eval(sys.argv[2]))
方式一docker
一、方式一所用到的Dockerfile文件json
FROM centos:7 MAINTAINER "1072051214@qq.com" COPY CentOS-Base.repo epel.repo /etc/yum.repos.d/ RUN yum install python3 net-tools -y && \ yum clean all COPY ["app.py","/tmp"] ENTRYPOINT ["python3","/tmp/app.py"] CMD ["localhost","8888"]
在該Dockerfile文件中,ENTRYPOINT中定義了須要在鏡像中執行的py文件;而CMD定義了py文件所須要的默認參數;centos
二、構建鏡像bash
在Dockerfile存在的同級目錄下放入構建所需的app.py、CentOS-Base.repo、epel.repo文件;app
在Dockerfile目錄下執行構建命令socket
docker build -t zhang/python:v0.3 ./
三、測試效果tcp
#查看鏡像 [root@master dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhang/python v0.3 ef2f8644ab79 6 hours ago 278 MB
#前臺運行,先測試默認值是否生效 [root@master dockerfile]# docker run --rm zhang/python:v0.3#再開一個終端 [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cb38dedce26d zhang/python:v0.3 "python3 /tmp/app...." 2 minutes ago Up 2 minutes reverent_kowalevski [root@master ~]# docker exec -it cb38dedce26d /bin/bash [root@cb38dedce26d /]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN
#前臺運行,測試傳遞參數進去是否生效 [root@master dockerfile]# docker run --rm zhang/python:v0.3 172.17.0.3 8080#再開一個終端 [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f43e54f80660 zhang/python:v0.3 "python3 /tmp/app...." 24 seconds ago Up 24 seconds laughing_torvalds [root@master ~]# docker exec -it f43e54f80660 /bin/bash [root@f43e54f80660 /]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 172.17.0.3:8080 0.0.0.0:* LISTEN
方式二ide
一、方式二所用到的Dockerfile文件測試
FROM centos:7 MAINTAINER "1072051214@qq.com" COPY CentOS-Base.repo epel.repo /etc/yum.repos.d/ RUN yum install python3 net-tools -y && \ yum clean all COPY ["app.py","/tmp"] ENV HOST="localhost" ENV PORT="8888" CMD python3 /tmp/app.py $HOST $PORT
在該Dockerfile文件中,用ENV定義變量默認值;用CMD定義要運行的py文件;
二、構建鏡像
在Dockerfile存在的同級目錄下放入構建所需的app.py、CentOS-Base.repo、epel.repo文件;
在Dockerfile目錄下執行構建命令
docker build -t zhang/python:v0.6 ./
三、測試效果
#查看鏡像 [root@master dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zhang/python v0.6 ccb36fd6ad19 2 hours ago 278 MB zhang/python v0.3 ef2f8644ab79 6 hours ago 278 MB
#前臺運行,先測試默認值是否生效 [root@master dockerfile]# docker run --rm zhang/python:v0.6#再開一個終端 [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f577e9f651bb zhang/python:v0.6 "/bin/sh -c 'pytho..." About a minute ago Up About a minute nifty_cray [root@master ~]# docker exec -it f577e9f651bb /bin/bash [root@f577e9f651bb /]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN
#前臺運行,測試傳遞參數進去是否生效(-e和--env均可以) [root@master dockerfile]# docker run --rm --env HOST=172.17.0.3 --env PORT=8085 zhang/python:v0.6#再開一個終端 [root@master ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0c631fd54ad0 zhang/python:v0.6 "/bin/sh -c 'pytho..." 35 seconds ago Up 34 seconds eager_yalow [root@master ~]# docker exec -it 0c631fd54ad0 /bin/bash [root@0c631fd54ad0 /]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 172.17.0.3:8085 0.0.0.0:* LISTEN