测试用到的python文件:

#!/usr/bin/env python3
#conding: utf-8
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import sys
import socket

def 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]))

方式一

1、方式一所用到的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"]

ENTRYPOINT ["python3","/tmp/app.py"]

CMD ["localhost","8888"]

  在该Dockerfile文件中,ENTRYPOINT中定义了需要在镜像中执行的py文件;而CMD定义了py文件所需要的默认参数;

2、构建镜像

  在Dockerfile存在的同级目录下放入构建所需的app.py、CentOS-Base.repo、epel.repo文件;

  在Dockerfile目录下执行构建命令

docker build -t zhang/python:v0.3 ./

3、测试效果

#查看镜像
[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

方式二

1、方式二所用到的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文件;

2、构建镜像

  在Dockerfile存在的同级目录下放入构建所需的app.py、CentOS-Base.repo、epel.repo文件;

  在Dockerfile目录下执行构建命令

docker build -t zhang/python:v0.6 ./

3、测试效果

#查看镜像
[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