当前位置: 首页 > 工具软件 > Gordon Docker > 使用案例 >

docker官方文档翻译(二)

谭学名
2023-12-01

第二部分:

前期准备:

安装docker version1.13 或者更高的版本

阅读在第一部分的步骤

让你的环境跑一个快速的测试程序以确保你全部安装好。

docker run hello-world

介绍:

是时候开始用docker的方式生成一个app了。我们从下面的程序,一个容器的底部开始,这个在这个程序已经在这一页里面包括了。在这个层次之上的是一个服务,这个服务定义了容器是如何在产品中运行的,在第三部分中包含了。

最后,在栈的顶端,定义了服务的互动行为,在第五部分中包含了。

  1. 服务
  2. 容器

你新的开发环境

在以前,如果你开始开发一个python程序,首当其冲的肯定是在你的电脑上安装一个python运行环境。但是,你在电脑上创建的情景所要的环境需要完美地适合你的程序才能如预期的运行,还需要适合你的产品环境。

使用docker,你可以仅仅使用一个轻量python运行时环境作为一个镜像,没有必要安装。然后,你的创建可以包含基本的python镜像,以确保你的程序,它的依赖和运行时环境一直都在。

这些轻量级的镜像是通过被称为dockerfile的文件定义的。

 

 

 

使用dockerfile来定义容器。

dockerfile定义在你的容器的环境中具体运行着什么。资源获取的方式想网络接口并且磁盘驱动在环境中是虚拟化的,这个环境是和你剩下的系统资源隔离开来的,所以你需要映射通向外部世界的端口,并且确定你需要复制哪些文件进入这个环境中。然而,做完了这些,你可以体验到在dockerfile中定义的程序可以在任何环境中都运行一致。

dockerfile

在你的电脑里创建一个空的文件夹。在命令行进入这个新的文件夹,创建一个叫dockerfile的文件,把下面的内容复制粘贴到这个文件中,保存。注意在你的dockerfile中解释了每行语句的注释。

# Use an official Python runtime as a parent imageFROM python:2.7-slim

# Set the working directory to /appWORKDIR /app

# Copy the current directory contents into the container at /appCOPY . /app

# Install any needed packages specified in requirements.txtRUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this containerEXPOSE 80

# Define environment variableENV NAME World

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

 

dockerfile涉及到一些我们还没有创建的文件,app.py和requirements.txt 。接下来让我们创建这些文件。

程序本身

创建另外两个文件,requirements.txt和app.py,并且把他们和dockerfile放在同一个文件夹。这能使得app完备,正如你看到的是非常简单的。当上述的dockerfile被编译进一个镜像,app.py 和requirements.txt 就会呈现,因为dockerfile文件的copy命令并且由于expose命令使得app.py文件的输出成为可能。

requirements.txt

Flask

Redis

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)

现在我们可以看到pip install -r requirements.txt 安装了flask和redis 对于python的依赖包,并且这个程序打印了环境变量name,和对socker.gethostname()的调用的输出。最后,因为redis没有真正的运行(由于我们只是安装了python依赖包,部署redis自身),我们应该预料到这里对它的使用会失败和生成错误信息。

Note: Accessing the name of the host when inside a container retrieves the container ID, which is like the process ID for a running executable.

这就是了,你不需要python或者requirements.txt中的任何其他在你的系统中的东西,也不需要在你的系统中构建或者运行镜像。这似乎你没有真的用python和flask启动环境,但实际上你启动了。

构建程序:

我们已经准备好了安装程序。确保你仍在在你的新文件夹的第一层。如下所示:

ls dockerfile app.py requirements.txt

现在运行构建命令。这样创建了一个docker 镜像,可以使用--tag参数用它名字。使用-t 如果你想用断点的参数。

docker build --tag=friendlyhello .

你创建的镜像放在哪里?它在你电脑的本地docker镜像注册处:

 

$ docker image ls

REPOSITORY            TAG                 IMAGE ID

friendlyhello         latest              326387cea398

注意最后的默认的tag,这个完整的tag参数应该是像--tag=friendlyhello:v0.0.1.

 

Troubleshooting for Linux users

Proxy server settings

Proxy servers can block connections to your web app once it’s up and running. If you are behind a proxy server, add the following lines to your Dockerfile, using the ENV command to specify the host and port for your proxy servers:

# Set proxy server, replace host:port with values for your serversENV http_proxy host:portENV https_proxy host:port

DNS settings

DNS misconfigurations can generate problems with pip. You need to set your own DNS server address to make pip work properly. You might want to change the DNS settings of the Docker daemon. You can edit (or create) the configuration file at /etc/docker/daemon.json with the dns key, as following:

{

  "dns": ["your_dns_address", "8.8.8.8"]}

In the example above, the first element of the list is the address of your DNS server. The second item is Google’s DNS which can be used when the first one is not available.

Before proceeding, save daemon.json and restart the docker service.

sudo service docker restart

Once fixed, retry to run the build command.

 

运行程序

运行程序,将你的电脑的端口号4000映射到容器的发布端口80 使用-p:

docker run -p 4000:80 friendlyhello

你可以看到python正在运行你的程序在 http://0.0.0.0:80 的消息。但是这条消息是来自容器内部,这是不知道你映射了80端口到容器的4000端口,使用正确的地址:

http://localhost:4000.

 

注意:如果你在win7上使用docker工具箱,使用docker machine ip 来替代localhost.例如:

http://192.168.99.100:4000/.使用命令docker-machine ip来查找ip地址。

你还可以使用curl命令来查看相同的内容。

$ curl http://localhost:4000

 

<h3>Hello World!</h3><b>Hostname:</b> 8fc990912a14<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

这个端口关于4000:80重映射展示了在dockerfile中的expose和使用-p运行docker的时候设置的值的区别。在后者中,是将端口的4000映射到容器的80中,并且使用http://localhost.

点击CTRL+C在你的终端退出。

 

在windows中,准确的停止容器运行。

使用 CTRL+C 不能停止容器。所以第一步使用CTRL+C 来获得返回的提示,或者打开另一个shell界面。然后键入docker container ls来列举正在运行的容器,紧接着使用docker container stop <Container NAME or ID> 来停止容器。否则,在下一步中当你重新运行容器的时候,你会得到一个进程的错误响应。

现在我们在后台运行程序吧:

docker run -d -p 4000:80 friendlyhello

你会得到给你的程序分配的一长串的容器id,然后返回你的终端。你的容器在后台运行。你也可以看到缩短的容器id使用命令docker container ls,在运行命令的时候都能够生效。

$ docker container ls CONTAINER

ID                        IMAGE               COMMAND             CREATED

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

注意到container id和http://localhost:4000上是相对应的。

现在使用 docker container stop来停止这个进程,像这样使用container id,docker container stop 1fa4ab2cf395

分享你的镜像

为了演示我们具体创造了什么的轻量性,让我们把我们创建的镜像上传并把它在其他地方运行。毕竟,你应该知道当你想部署容器成为产品的时候如何推送到注册中心。

一个注册中心是一些仓库的集合,一个仓库是一些镜像的集合。除了代码已经被编译过之外,其他有点像github仓库,docker cli默认使用docker的大众注册中心。

注意到,这里我们shiyongdocker的大众注册中心是由于它说免费的并且已经配置好了。但是还是你可以选择是很多的大众注册中心,你甚至可以自己创建你私有的注册中心。Docker Trusted Registry.

使用你的docker id登录

如果你没有一个docker账户,在 hub.docker.com注册一个。记下你的用户名。

使用你本地电脑登陆doker大众注册中心。

$ docker login

标记你的镜像

将本地镜像和注册中心的仓库绑定起来的符号是username/repository:tag这个tag是可选的,不过推荐加上,因为它是注册中心用来给docker 镜像一个版本号的机制。赋予一个仓库和标识以在某个情景中富有含义的名字,例如get-started:part2,这将镜像放入get-started仓库中并且把它标识为part2.

例如

 

docker tag image username/repository:tag

docker tag friendlyhello gordon/get-started:part2

运行docker image ls 来查看你新标识的镜像

$ docker image ls

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE

friendlyhello            latest              d9e555c53008        3 minutes ago       195MB

gordon/get-started         part2               d9e555c53008        3 minutes ago       195MB

python                   2.7-slim            1c7128a655f6        5 days ago          183MB

...

发布镜像

上传你的标识镜像入仓库

docker push username/repository:tag

一旦你完成,你上传的结果将会使得镜像公开。如果你登录了Docker Hub,你可以在这里看到新的镜像,使用pull 命令。

从远处的仓库拉取和运行镜像

现在开始,你可以使用docker run并且可以在任何机器上运行你的程序使用如下命令:

docker run -p 4000:80 username/repository:tag

如果镜像在电脑本地上无法获取,docker会把它从远处仓库中拉取下来。

$ docker run -p 4000:80 gordon/get-started:part2

Unable to find image 'gordon/get-started:part2' locally

part2: Pulling from gordon/get-started

10a267c67f42: Already exists

f68a39a6a5e4: Already exists

9beaffc0cf19: Already exists

3c1fe835fb6b: Already exists

4c9f1fa8fcb8: Already exists

ee7d8f576a14: Already exists

fbccdcced46e: Already exists

Digest: sha256:0601c866aab2adcc6498200efd0f754037e909e5fd42069adeff72d1e2439068

Status: Downloaded newer image for gordon/get-started:part2

 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

第二部分的总结

这章就完了。在下一节中,我们可以学习到如何使用服务运行容器以达到缩放我们程序的目的。

Continue to Part 3 >>

Or, learn how to如何使用digital ocean来在本地中启动你的容器

 

回顾和作弊清单

这里有一个本章中所有的终端记录

这里有一些本章中的杰出docker命令列表,还有一些在你离开之前想了解到相关的一些命令。

docker build -t friendlyhello .          # 使用当前目录下的 Dockerfile 创建镜像
docker run -p 4000:80 friendlyhello      # 运行镜像 "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello   # 后台运行镜像,detached 模式
docker container ls            # 列出所有的运行中的容器
docker container ls -a         # 列出所有的容器,包括不运行的
docker container stop <hash>   # 友好的关闭指定容器
docker container kill <hash>   # 强制关闭指定容器
docker container rm <hash>     # 删除指定容器
docker container rm $(docker container ls -a -q)  # 删除所有容器
docker image ls -a                                # 列出所有镜像
docker image rm <image id>                 # 删除指定镜像
docker image rm $(docker image ls -a -q)   # 删除所有镜像
docker login                               # 用 Docker 凭证在当前 CLI 会话中登录
docker tag <image> username/repository:tag     # 给要上传到 registry 的镜像打标记
docker push username/repository:tag            # 将已经打标记的镜像上传到 registry
docker run username/repository:tag             # 从 registry 运行镜像
 类似资料: