美文网首页Docker和云计算学习
Docker -自底向上构建容器

Docker -自底向上构建容器

作者: 山中散人的博客 | 来源:发表于2019-05-19 18:49 被阅读0次

在过去,如果要构建一个应用程序,例如,利用Python写一个文本正则表达式匹配搜索工具,第一件要做的事就是安装Python解释器和运行环境(当然一般是自带的)。开发者可能还需要测试运行环境是否符合期望,然后编译安装应用程序,再进行测试。

现在,我们可以选择一个Python镜像,依次为基础,构建一个包含应用程序的镜像,同时将其运行环境打包。

构建镜像

Dockerfile defines what goes on in the environment inside your container.

  • Access to virtualized resources like networking interfaces and disk drives
  • Map ports to the outside world
  • Specific about what files you want to “copy in” to that image

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 80

# Define environment variable
ENV NAME World

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

创建文件requirements.txt,指定镜像依赖的Python包

Flask
Redis

在文件app.py中,完成应用程序的逻辑,一个hello world Web程序,通过HTML文档返回本地主机名和主页访问次数。

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=80)

运行Dockerfile构建镜像,指定tag名。

docker build --tag=friendlyhello:v0.0.1 .

构建成功后,得到一个镜像,

docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        4 months ago        1.84kB

#delete the newly built image
docker container stop fce289e99eb9
docker image rm fce289e99eb9

运行容器

构建成功后,通过容器运行应用程序,将本地主机的端口4000映射到容器端口80上。

docker run -p 4000:80 friendlyhello

利用浏览器访问[http://localhost:4000]

访问hello-world 页面

在Shell中访问

curl http://localhost:4000

发布镜像

let’s upload our built image and run it somewhere else. Push to registries when you want to deploy containers to production.

Docker通过DockerHub提供了类似GitHub的版本控制,镜像分发服务。

#login docker hub
docker login

Authenticating with existing credentials...
Login Succeeded

首先登录DockerHub,在Mac OS上可以通过Docker Desktop完成

Docker Desktop
#download nginx image from Docker hub
docker pull nginx

#tag local image
docker tag nginx:latest kevinacoder/my_nginx

#publish the image
docker push kevinacoder/my_nginx

#get the image
docker pull kevinacoder/my_nginx

通过DockerHub发布,获取镜像的方法和GitHub类似。

原文:[https://docs.docker.com/get-started/part2/]

相关文章

  • Docker -自底向上构建容器

    在过去,如果要构建一个应用程序,例如,利用Python写一个文本正则表达式匹配搜索工具,第一件要做的事就是安装Py...

  • docker镜像

    Docker镜像是构建docker容器的基础,容器是docker镜像的运行的实例。构建镜像只用三条命令就可以搭建一...

  • 基于容器的自动构建——Docker在美团的应用

    摘要:自动构建是美团对Docker的首次应用。解决方案可以概括为三点:把构建过程放到Docker容器;提交代码时自...

  • Docker部署(二):MySQL数据库

    Docker 容器构建 使用Dockerfile构建镜像 使用官方提供的Docker镜像 所谓Dockerfile...

  • 101、镜像的构建

    1、镜像构建方式 1.1、docker commit 运行容器 对容器进行修改 通过docker commit 把...

  • Docker常用命令

    docker常用命令 进入容器 docker run -it xxxxx:1.1 /bin/bash 构建容器 d...

  • 使用docker构建nexus3私服

    使用 docker 构建 Nexus 3 Maven仓库 拉取Docker镜像 启动Docker容器

  • Docker深度学习1

    dockerfile 构建镜像docker run 启动容器docker-compose 启动服务 Docker ...

  • Docker

    [TOC] 使用 Docker 镜像 下载镜像 列出镜像 构建镜像 删除本地镜像 操作 Docker 容器 容器是...

  • (四)Docker镜像与仓库之一(3)——构建镜像

    Docker官网 Docker文档地址 构建镜像的好处: 1.构建docker镜像,可以保存对容器的修改,方便...

网友评论

    本文标题:Docker -自底向上构建容器

    本文链接:https://www.haomeiwen.com/subject/buvqzqtx.html