目录
目标
把一个代理池项目制作成一个 Docker 镜像,并在远程服务器运行。
准备工作
安装 Docker
创建 Dockerfile
首先在项目的根目录下新建一个 requirements.txt 文件,将整个项目依赖的 Python 环境包都列出来,如果库需要特定的版本,我们还可以指定版本号,如下所示:
scrapy>=1.4.0 pymongo>=3.4.0在项目根目录下新建一个 Dockerfile 文件,文件不加任何后缀名,修改内容如下所示:
FROM python:3.6 ENV PATH /usr/local/bin:$PATH ADD . /code WORKDIR /code RUN pip3 install -r requirements.txt CMD python3 run.py构建镜像
接下来我们便可以构建镜像了,执行如下命令:
docker build -t proxypool:latest .出现类似输出就证明镜像构建成功了,
Sending build context to Docker daemon326.1kB Step 1/6 : FROM python:3.6 3.6: Pulling from library/python e9afc4f90ab0: Pull complete 989e6b19a265: Pull complete af14b6c2f878: Pull complete 5573c4b30949: Pull complete 11a88e: Pull complete 5fbfb8c90ca7: Pull complete 75c8e90441d4: Pull complete efb90023ffe8: Pull complete ad8248c33afb: Pull complete Digest: sha256:a3d3ec8d429ddfeff5303f77049b22e1fca8d945d2a30b68920d67d793bdac89 Status: Downloaded newer image for python:3.6 ---> e0373ff33a19 Step 2/6 : ENV PATH /usr/local/bin:$PATH ---> Running in e56b55b7512c Removing intermediate container e56b55b7512c ---> 4524d Step 3/6 : ADD . /code ---> b1390a35e10d Step 4/6 : WORKDIR /code ---> Running in 45d63cdb8f90 Removing intermediate container 45d63cdb8f90 ---> 4939f1c701ba Step 5/6 : RUN pip3 install -r requirements.txt ---> Running in ece Removing intermediate container ece ---> 29c8239ae29f Step 6/6 : CMD python3 run.py ---> Running in 59521f68a8d1 Removing intermediate container 59521f68a8d1 ---> b05396ba17fb Successfully built b05396ba17fb Successfully tagged proxypool:latest查看一下构建的镜像:
docker images运行测试
docker run -p 5555:5555 proxypool推送至 Docker Hub
docker tag proxypool xxx/proxypool:latest docker push xxx/proxypool远程服务器设置
在远程服务器上,只需要执行运行测试的命令:
docker run -p 5555:5555 -d proxypool现在就可以在任意主机对代理服务器 http://140.143.17.89:5555/ 进行访问了。