温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么制作第一个docker镜像

发布时间:2021-10-20 09:17:31 来源:亿速云 阅读:163 作者:柒染 栏目:大数据

本篇文章给大家分享的是有关怎么制作第一个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 = "<h4>Hello {name}!</h4>" \
           "<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
<h4>Hello World!</h4><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

以上就是怎么制作第一个docker镜像,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI