我们使用Dockerfile去构建一个简单的python项目.
一 . 我们先创建随便创建一个文件夹testdocker. 然后在这个文件夹中创建三个文件, 一个空的Dockfile.一个txt文件requirements.txt 和一个python文件app.py.
二. 编辑Dockerfile文件
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
三. 编辑requirements.txt文件
Flask
Redis
四. 编辑app.py文件,在python文件中
from flask import Flask
from redis import Redis, RedisError
import os
import socket