您的位置:首页 > 数码常识数码常识

dockerps和dockerimage(docker创建image)

2025-05-10人已围观

dockerps和dockerimage(docker创建image)
  过去,如果你想写一个python程序,你首先是安装一个python编译器。但是,你的操作系统必须匹配python的版本和你自己的应用。

  docker创建image

  使用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 https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b 14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)

  Collecting Redis (from -r requirements.txt (line 2))

  Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl (64kB)

  Collecting itsdangerous>=0.24 (from flask->-r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl

  Collecting Jinja2>=2.10 (from flask->-r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)

  Collecting Werkzeug>=0.14 (from flask->-r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)

  Collecting click>=5.1 (from flask->-r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)

  Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask->-r requirements.txt (line 1))

  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz

  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

  然后你就可以通过浏览器访问http://localhost:4000了。

  $ docker container ls

  CONTAINER ID IMAGE COMMAND CREATED

  1fa4ab2cf395 friendlyhello "python app.py" 28 seconds ago

  你可以使用 CONTAINER ID来关闭一个容器:

  docker container stop 1fa4ab2cf395

  步骤五、分享你的image

  首先去https://hub.docker.com/创建一个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

  上面就是小居数码小编今天给大家介绍的关于(docker创建image)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。

  94%的朋友还想知道的:



  155083
 

很赞哦! ()

随机图文