前言:
此时看官们对“python中image的用法”大致比较注重,大家都想要分析一些“python中image的用法”的相关知识。那么小编在网摘上网罗了一些有关“python中image的用法””的相关知识,希望小伙伴们能喜欢,大家快快来学习一下吧!过去,如果你想写一个python程序,你首先是安装一个python编译器。但是,你的操作系统必须匹配python的版本和你自己的应用。使用docker,你只需要获取一个包含python编译器的image,而不需要安装。你的应用可以包含这个python image和你的app代码,然后打包成一个新的image,这个新的image是通过dockerfile定义的。
dockerfile定义你的容器中的环境。访问各种资源,例如网络接口和磁盘驱动器等,都被虚拟化在环境中,它和你的主机的其他部分是相互独立的。所以你需要把端口映射到外部环境去,并指定那些文件要放在内部环境中。这样,你能预期你的应用可以在任何地方运行。
步骤一:创建dockerfile
创建一个空目录,在这个目录里创建一个文件Dockerfile,把以下内容拷贝到这个文件中去,并保存。
##########################dockerfile start############################
# 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-hostpypi.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"]
#############dockerfile end###############################
这个文件中指定的requirements.txt和app.py并不存在,需要我们后面创建。
步骤二,创建app
首先创建requirements.txt,根据上面的注释,我们知道容器会通过pip安装该文件中指定的应用。因此文件内容如下:
requirements.txt
Flask
Redis
app.py是一个用flask框架写的一个简单的web应用,内容如下:
app.py
from flask import Flaskfrom redis import Redis, RedisErrorimport osimport socket
# Connect to Redisredis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
@app.route("/")def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis, counter disabled</i>"
html = "<h3>Hello {name}!</h3>" \
"<b>Hostname:</b> {hostname}<br/>" \
"<b>Visits:</b> {visits}"
return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
步骤三、创建app
现在以上三个文件都放在同一个目录下面了,然后运行以下命令来创建app,注意后面有个“.” 表示当前路径。
docker build -t friendlyhello .
然后你就可以用docker image ls来查看你的image了。我们来看一下创建过程:
Sending build context to Docker daemon 5.12kB
Step 1/8 : FROM python:2.7-slim
2.7-slim: Pulling from library/python (从library获取image)
f17d81b4b692: Pull complete
7429ec5d1bbc: Pull complete
45b34d043e88: Pull complete
49d33f4617f3: Pull complete
Digest: sha256:3b9c77ba2cdb829f6d41cb64a3e6b3fb7f40a9143648c506864b7fbf272dc77e
Status: Downloaded newer image for python:2.7-slim
---> 804b0a01ea83
Step 2/8 : FROM python:2.7-slim
---> 804b0a01ea83
Step 3/8 : WORKDIR /app
---> Running in 5b32b91a8ea8
Removing intermediate container 5b32b91a8ea8
---> 1b953831dd27
Step 4/8 : COPY . /app (完成目录创建和文件拷贝)
---> be0bb1979cf1
Step 5/8 : RUN pip install --trusted-host pypi.python.org -r requirements.txt (以下根据该文件安装相应的app)
---> Running in 31773c1dcc31
Collecting flask (from -r requirements.txt (line 1))
Downloading 14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
Collecting Redis (from -r requirements.txt (line 2))
Downloading (64kB)
Collecting itsdangerous>=0.24 (from flask->-r requirements.txt (line 1))
Downloading
Collecting Jinja2>=2.10 (from flask->-r requirements.txt (line 1))
Downloading (126kB)
Collecting Werkzeug>=0.14 (from flask->-r requirements.txt (line 1))
Downloading (322kB)
Collecting click>=5.1 (from flask->-r requirements.txt (line 1))
Downloading (81kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask->-r requirements.txt (line 1))
Downloading
Building wheels for collected packages: MarkupSafe
Running setup.py bdist_wheel for MarkupSafe: started
Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46
Successfully built MarkupSafe
Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, Redis
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Redis-2.10.6 Werkzeug-0.14.1 click-7.0 flask-1.0.2 itsdangerous-1.1.0
Removing intermediate container 31773c1dcc31
---> a86d45e2d972
Step 6/8 : EXPOSE 80
---> Running in e760819e9a8f
Removing intermediate container e760819e9a8f
---> 6f3c29b30438
Step 7/8 : ENV NAME World
---> Running in 1adf8e242f84
Removing intermediate container 1adf8e242f84
---> 44eed9b75718
Step 8/8 : CMD ["python", "app.py"]
---> Running in db649f25c46c
Removing intermediate container db649f25c46c
---> d5de346df6ff
Successfully built d5de346df6ff
Successfully tagged friendlyhello:latest
现在,我们有三个image了,一个是hello-world,一个是刚下载的python,一个是我们自己创建的friendlyhello。
ubuntu$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
friendlyhello latest d5de346df6ff 3 minutes ago 132MB
python 2.7-slim 804b0a01ea83 2 weeks ago 120MB
hello-world latest 4ab4c602aa5e 8 weeks ago 1.84kB
简单几句话,就创建了一个132M的应用,是不是很有成就感,哈哈。
步骤四,运行app
80端口很可能被主机占用,因此我们把主机的4000端口映射到我们app的80端口,如下所示。
docker run -p 4000:80 friendlyhello
然后你就可以通过浏览器访问了。
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED
1fa4ab2cf395 friendlyhello "python app.py" 28 seconds ago
你可以使用 CONTAINER ID来关闭一个容器:
docker container stop 1fa4ab2cf395
步骤五、分享你的image
首先去创建一个ID。
然后用这个id登陆docker公共库
docker login
关联本地image到登记处的仓库的标记是username/repository:tag。tag是可选的,但是是推荐的。现在我们给image创建一个tag
docker tag image username/repository:tag
例如: docker tag friendlyhello gordon/get-started:part2
再运行docker image ls你会看到gordon/get-started这个image。
然后是发布你的image
docker push username/repository:tag
现在你可以在任何机器上运行你的app了,如果本机没有这个image,docker会自动下载,是不是很方便呢?
docker run -p 4000:80 username/repository:tag
标签: #python中image的用法