Get started with Docker Compose

On this page you build a simple Python web application running on Docker Compose. The application uses the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts demonstrated here should be understandable even if you’re not familiar with it.

 

在这个页面上,您将构建一个运行在Docker Compose上的简单Python web应用程序。应用程序使用Flask框架并在Redis中维护一个命中计数器。虽然示例使用Python,但是这里演示的概念应该是可以理解的,即使您不熟悉它。

 

prerequesites

 

预备知识

 

Make sure you have already installed both Docker Engine and Docker Compose. You don’t need to install Python or Redis, as both are provided by Docker images.

 

确保您已经安装了Docker引擎和Docker Compose。您不需要安装Python或Redis,因为它们都是由Docker映像提供的。

 

step 1 setup

 

Define the application dependencies.

定义程序依赖

 

Create a directory for the project:

为项目创建文件夹

 

$ mkdir composetest

$ cd composetest

 

Create a file called app.py in your project directory and paste this in:

创建app.py 文件,粘贴一下内容

 

In this example, redis is the hostname of the redis container on the application’s network. We use the default port for Redis, 6379.

 

在本例中,redis是应用程序网络上redis容器的主机名。我们使用Redis的默认端口6379。

 

step2 Create DockerFile

创建Docker File 文件

 

In this step, you write a Dockerfile that builds a Docker image. The image contains all the dependencies the Python application requires, including Python itself.

 

在这一步中,您将编写一个Dockerfile来构建Docker映像。该映像包含Python应用程序需要的所有依赖项,包括Python本身。

 

In your project directory, create a file named Dockerfile and paste the following:

 

在您的项目目录中,创建一个名为Dockerfile的文件,并粘贴以下内容:

 

FROM python:3.4-alpine

ADD . /code

WORKDIR /code

RUN pip install -r requirements.txt

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

 

This tells Docker to:

 

  1. Build an image starting with the Python 3.4 image.
  2. Add the current directory . into the path /code in the image.
  1. Set the working directory to /code.
  2. Install the Python dependencies.
  3. Set the default command for the container to python app.py.

 

注:docker file 文档参考地址:https://docs.docker.com/engine/reference/builder/

 

step3 Define services in a Compose file

在Compose 文件中定义服务

 

Create a file called docker-compose.yml in your project directory and paste the following:

创建一个名为docker-compose的文件。在您的项目目录下粘贴以下内容:

 

version: '3'

services:

web:

build: .

ports:

- "5000:5000"

redis:

image: "redis:alpine"

This Compose file defines two services, web and redis. The web service:

Compose 文件定义了两个服务, Web 和 redis 。 Web服务:

Uses an image that’s built from the Dockerfile in the current directory.

使用从当前目录中的Dockerfile构建的映像。

Forwards the exposed port 5000 on the container to port 5000 on the host machine. We use the default port for the Flask web server, 5000.

将容器上裸露的端口5000转发到主机上的端口5000。我们使用Flask web服务器的默认端口5000。

 

The redis service uses a public Redis image pulled from the Docker Hub registry.

redis服务使用从Docker Hub注册表中提取的公共redis映像。

 

step4 Build And Run your app with compose

 

From your project directory, start up your application by running docker-compose up.

从项目目录中,通过运行docker- composition启动应用程序。

 

$ docker-compose up

 

第一次运行报错如下:

 

ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-6111be397ec5 -j RETURN: iptables: No chain/target/match by that name.

 

重启服务service docker restart 解决。

 

第二次运行尝试了增加虚拟机内存, 增加镜像加速器均报一下错误,升级Python 版本,配置VPN,还是报一下错误,如有哪位神人知道解决办法还望指教。

 

Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

ERROR: Service 'web' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

 

 

 

Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.

 

Compose提取Redis映像,为您的代码构建映像,并启动您定义的服务。在这种情况下,代码在构建时被静态复制到映像中。

 

2、Enter http://0.0.0.0:5000/ in a browser to see the application running.

在浏览器中输入http://0.0.0.0:5000/以查看应用程序的运行情况。

 

 

If you’re using Docker natively on Linux, Docker for Mac, or Docker for Windows, then the web app should now be listening on port 5000 on your Docker daemon host. Point your web browser to http://localhost:5000 to find the Hello World message. If this doesn’t resolve, you can also try http://0.0.0.0:5000.

 

如果您在Linux上本机使用Docker,在Mac上使用Docker,或者在Windows上使用Docker,那么web应用程序现在应该监听Docker守护进程主机上的端口5000。将web浏览器指向http://localhost:5000以查找Hello World消息。如果不能解决这个问题,也可以尝试http://0.0.0.0:5000。

 

If you’re using Docker Machine on a Mac or Windows, use docker-machine ip MACHINE_VM to get the IP address of your Docker host. Then, open http://MACHINE_VM_IP:5000 in a browser.

 

如果您在Mac或Windows上使用Docker机器,请使用Docker - Machine ip MACHINE_VM获取Docker主机的ip地址。然后,在浏览器中打开http://MACHINE_VM_IP:5000。

 

You should see a message in your browser saying:

你应该在浏览器中看到这样一条消息:

 

Hello World! I have been seen 1 times.

 

3、Refresh the page.

The number should increment.

刷新页面,这个数字会增加

 

step5、Edit the Compose file to add a bind mount

编辑compose文件以添加绑定挂载

Edit docker-compose.yml in your project directory to add a bind mount for the web service:

编辑docker-compose。在您的项目目录中添加用于web服务的绑定挂载:

 

version: '3'

services:

web:

build: .

ports:

- "5000:5000"

volumes:

- .:/code

redis:

image: "redis:alpine"

The new volumes key mounts the project directory (current directory) on the host to /code inside the container, allowing you to modify the code on the fly, without having to rebuild the image.

 

新的volume键将主机上的项目目录(当前目录)挂载到容器内的/code,允许您动态修改代码,而无需重新构建映像。

 

step6 Re-build and run the app with Compose

重新构建并使用Compose运行应用程序

 

From your project directory, type docker-compose up to build the app with the updated Compose file, and run it.

在您的项目目录中,键入docker-compose up以使用更新的compose文件构建应用程序,并运行它。

 

Check the Hello World message in a web browser again, and refresh to see the count increment.

再次检查web浏览器中的Hello World消息,并刷新以查看计数增量。

 

 

重点:

 

Shared folders, volumes, and bind mounts

共享文件夹、卷和绑定挂载

 

If your project is outside of the Users directory (cd ~), then you need to share the drive or location of the Dockerfile and volume you are using. If you get runtime errors indicating an application file is not found, a volume mount is denied, or a service cannot start, try enabling file or drive sharing. Volume mounting requires shared drives for projects that live outside of C:\Users (Windows) or /Users (Mac), and is required for any project on Docker for Windows that uses Linux containers. For more information, see Shared Drives on Docker for Windows, File sharing on Docker for Mac, and the general examples on how to Manage data in containers.

 

如果您的项目位于用户目录(cd ~)之外,那么您需要共享您正在使用的Dockerfile和卷的驱动器或位置。如果出现运行时错误,表明没有找到应用程序文件、卷挂载被拒绝或服务无法启动,请尝试启用文件或驱动器共享。体积越来越需要共享驱动器以外的项目,住C:\Users(Windows)或/用户(Mac),和任何项目需要在Docker Windows使用Linux容器。有关更多信息,请参见Windows的Docker上的共享驱动器、Mac的Docker上的文件共享以及关于如何管理容器中的数据的一般示例。

 

(说的是啥?)

 

 

If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in this VB trouble ticket. Newer Windows systems meet the requirements for Docker for Windows and do not need VirtualBox.

 

如果您在旧的Windows操作系统上使用Oracle VirtualBox,您可能会遇到与共享文件夹有关的问题,如VB故障报告单中所述。新的Windows系统满足Docker对Windows的要求,不需要VirtualBox。

 

step7 Update the application

 

Because the application code is now mounted into the container using a volume, you can make changes to its code and see the changes instantly, without having to rebuild the image.

 

由于现在使用卷将应用程序代码装载到容器中,因此您可以对其代码进行更改并立即看到更改,而无需重新构建映像。

 

  1. Change the greeting in app.py and save it. For example, change the Hello World! message to Hello from Docker!:

更改app.py中的问候语并保存它。例如,改变 “Hello World! " 为 “from docker”

 

return 'Hello from Docker! I have been seen {} times.\n'.format(count)

 

Refresh the app in your browser. The greeting should be updated, and the counter should still be incrementing.

 

刷新看一下结果改变没

 

step8 Experiment with some other commands

 

尝试一些其他命令

 

If you want to run your services in the background, you can pass the -d flag (for “detached” mode) to docker-compose up and use docker-compose ps to see what is currently running:

 

如果您想在后台运行服务,可以将-d标志(“分离”模式)传递给docker-compose up,并使用docker-compose ps查看当前正在运行的服务:

 

$ docker-compose up -d

Starting composetest_redis_1...

Starting composetest_web_1...

 

$ docker-compose ps

Name Command State Ports

-------------------------------------------------------------------

composetest_redis_1 /usr/local/bin/run Up

composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp

 

 

The docker-compose run command allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:

docker-compose run命令允许您为您的服务运行一次性命令。例如,要查看web服务可用的环境变量:

 

$ docker-compose run web env

 

See docker-compose --help to see other available commands. You can also install command completion for the bash and zsh shell, which also shows you available commands.

请参见docker-compose --help 查看其他可用命令。您还可以为bash和zsh shell安装命令补全,这也显示了可用的命令。

 

If you started Compose with docker-compose up -d, stop your services once you’ve finished with them:

如果您开始使用docker- up -d组合,则在使用完服务后停止服务:

 

$ docker-compose stop

 

You can bring everything down, removing the containers entirely, with the down command. Pass --volumes to also remove the data volume used by the Redis container:

您可以使用down命令将所有内容都清除,完全删除容器。Pass--valumes 还可以删除Redis容器使用的数据卷:

 

$ docker-compose down --volumes

 

At this point, you have seen the basics of how Compose works.

至此,您已经了解了作曲的基本原理。

你可能感兴趣的:(Docker,官方文档翻译,docker-compose)