制作第一个docker镜像

首先创建一个新目录用于存放我们制作镜像所需的文件 

进入到新目录中 执行touch Dockerfile 创建一个Dockerfile文件,Dockerfile 定义了容器运行的需要的环境,网络端口、磁盘资源、要执行的命令等等。

复制以下内容到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 8081

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

在新目录下执行命令 touch requirements.txt复制以下内容

Flask
Redis

在新目录下执行命令 touch app.py复制以下内容

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = 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=8081)

执行命令 docker build --tag=helloworld:v0.0.1 . 构建你的第一个docker镜像,别忘了命令的最后一个. 。

通过下面命令查看您刚刚制作的镜像

# docker image ls
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
helloworld           v0.0.1              f471662fe76e        2 minutes ago       131MB

该镜像通过Python代码启了一个简单的web服务,下面开始运行您的镜像

#docker run -p 8099:8081 helloworld:v0.0.1

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:8081/ (Press CTRL+C to quit)

看到上面的输出说明您的docker 镜像 helloworld 启动成功,您可以通过curl命令测试容器是否正在正常运行,docker run -p 实际上是将本地的8099端口映射到容器的8081端口。

# curl http://localhost:8099
<h3>Hello World!</h3><b>Hostname:</b> a64e25c2a522<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

您可以通过一下命令查看当前机器正在运行的容器

# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a64e25c2a522        helloworld:v0.0.1   "python app.py"     7 minutes ago       Up 7 minutes        0.0.0.0:8099->8081/tcp   frosty_newton

 

 

转载于:https://my.oschina.net/u/3824982/blog/3071666

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值