docker----容器

先阅读上一章 docker-基础知识与安装

 

一、介绍

现在是时候开始使用Docker方式构建应用程序了。我们从这样一个应用程序层次结构的底部开始,这是一个容器,我们将在此页面上介绍它。高于此级别的是一项服务,它定义了容器在生产中的行为方式堆

 

 

二、使用定义容器 Dockerfile

Dockerfile定义容器内环境中发生的事情。对网络接口和磁盘驱动器等资源的访问在此环境中进行虚拟化,该环境与系统的其他部分隔离,因此您需要将端口映射到外部世界,并具体说明要复制到哪些文件那个环境。但是,在执行此操作之后,您可以预期Dockerfile在此处定义的应用程序的构建 在其运行的任何位置都会完全相同。

 

Dockerfile

创建一个test目录。进入目录,创建一个名为的文件Dockerfile,将以下内容复制并粘贴到该文件中,然后保存。记下解释新Dockerfile中每个语句的注释。

扫描二维码关注公众号,回复: 3630258 查看本文章

# 使用官方python镜像

FROM python:2.7-slim

 

# 设置工作目录/app

WORKDIR /app

 

# 将当前目录内容复制到/app应用程序容器中

COPY . /app

 

# 安装文件中指定包

RUN pip install --trusted-host pypi.python.org -r requirements.txt

 

# 暴露容器端口给外部使用

EXPOSE 80

 

# 定义环境变量

ENV NAME World

 

# 当容器启动时运行app.py

CMD ["python", "app.py"]

 

 

三、创建其余文件

再创建两个文件,requirements.txt然后app.py将它们放在同一个文件夹中Dockerfile。这完成了我们的应用程序,您可以看到它非常简单。当上述Dockerfile被内置到的图像,app.py并且requirements.txt是因为存在DockerfileCOPY命令,并从输出app.py是通过HTTP得益于访问EXPOSE 命令。

image.png

[root@zabbix-agnet test]# cat requirements.txt

Flask

Redis

 

[root@zabbix-agnet test]# cat app.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

 

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)

现在我们看到pip install -r requirements.txtPython安装FlaskRedis库,应用程序打印环境变量NAME,以及调用的输出socket.gethostname()。最后,因为Redis没有运行(因为我们只安装了Python库,而不是Redis本身),我们应该期望在这里使用它的尝试失败并产生错误消息。

注意:在容器内部访问容器ID时,访问主机名称,这类似于正在运行的可执行文件的进程ID

而已!您不需要Pythonrequirements.txt系统中的任何内容,也不需要构建或运行此映像将它们安装在您的系统上。看起来你并没有真正建立一个PythonFlask的环境,但你有。

 

四、构建应用程序

现在运行build命令。这会创建一个Docker镜像,我们将使用-t它来标记,因此它具有友好的名称。

[root@zabbix-agnet test]# docker build -t friendlyhello .

image.png

此处有可能有报错

image.png

解决方法:

[root@zabbix-agnet test]# service docker restart

image.png

 

五、运行该应用程序

运行应用程序,使用以下方法将计算机的端口4000映射到容器的已发布端口80 -p

image.png 

本机检查和web界面端均可检查

您应该看到Python正在为您的应用程序提供服务的消息http://0.0.0.0:80。但是该消息来自容器内部,它不知道您将该容器的端口80映射到4000,从而生成正确的URL http://localhost:4000

Web浏览器中转到该URL,以查看在网页上提供的显示内容。

image.png

注意:如果您在Windows 7上使用Docker Toolbox,请使用Docker Machine IP而不是localhost。例如,http//192.168.99.1004000 /。要查找IP地址,请使用该命令docker-machine ip


六、启动容器停止容器

 image.png

  

七、分享你的镜像

Docker hub 官网注册不了,下面这位大神解决了

https://blog.csdn.net/m0_37985112/article/details/83013700

 

使用您的Docker ID登录

image.png

 

八、标记图像

将本地映像与注册表上的存储库相关联的表示法是 username/repository:tag。标签是可选的,但建议使用,因为它是注册管理机构用来为Docker镜像提供版本的机制。为上下文提供存储库和标记有意义的名称,例如 get-started:part1。这会将图像放入get-started存储库并将其标记为part1

现在,把它们放在一起来标记图像。docker tag image使用您的用户名,存储库和标记名称运行,以便将图像上载到所需的目标位置。该命令的语法是:

image.png

完成后,此上传的结果将公开发布。如果您登录到Docker Hub,则会在其中看到新图像及其pull命令。

 

九、从远程存储库中拉出并运行映像

如果映像在计算机上不可用,则Docker会从存储库中提取映像。

image.png无论在哪里docker run执行,它都会提取您的图像,以及Python和所有依赖项requirements.txt,并运行您的代码。它们都在一个整洁的小包中一起旅行,你不需要在主机上安装任何东西,以便Docker运行它。

 

回顾和备忘单(摘取自官网)

以下是此页面中基本Docker命令的列表,以及一些相关的命令,如果您想在继续之前稍微探索一下。

docker build -t friendlyhello .           # Create image using this directory's Dockerfile

docker run -p 4000:80 friendlyhello       # Run "friendlyname" mapping port 4000 to 80

docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode

docker container ls                                # List all running containers

docker container ls -a                  # List all containers, even those not running

docker container stop <hash>             # Gracefully stop the specified container

docker container kill <hash>            # Force shutdown of the specified container

docker container rm <hash>              # Remove specified container from this machine

docker container rm $(docker container ls -a -q)          # Remove all containers

docker image ls -a                               # List all images on this machine

docker image rm <image id>                    # Remove specified image from this machine

docker image rm $(docker image ls -a -q)     # Remove all images from this machine

docker login                                         # Log in this CLI session using your Docker credentials

docker tag <image> username/repository:tag      # Tag <image> for upload to registry

docker push username/repository:tag                # Upload tagged image to registry

docker run username/repository:tag                   # Run image from a registry


猜你喜欢

转载自blog.51cto.com/11571922/2306623