ディレクトリ 検索
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
テキスト

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

先决条件

确保你已经安装了Docker Engine和Docker Compose。您不需要安装Python或Redis,因为两者都由Docker镜像提供。

第1步:安装

  1. 为项目创建一个目录:$ mkdir composetest $ cd composetest

  2. 创建一个名为app.py你的项目目录中的文件,并将其粘贴到:from flask import从redis导入Flask Redis app = Flask(__ name__)redis = Redis(host ='redis',port = 6379)@ app.route('/') def hello():count = redis.incr('hits')return'Hello World!\ n'.format(count)if __name__ ==“__main__”:app.run(host =“0.0.0.0”,debug = True)在本例中,redisredis的主机名容器在应用程序的网络上。我们使用Redis的默认端口6379

  3. requirements.txt在项目目录中创建另一个文件,并将其粘贴到:flask redis

这些定义了应用程序的依赖关系。

第2步:创建一个Dockerfile

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

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

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

这告诉Docker:

  • 从Python 3.4图像开始构建一个图像。

  • 将当前目录添加./code图像的路径中。

  • 将工作目录设置为/code

  • 安装Python依赖项。

  • 将容器的默认命令设置为python app.py

有关如何编写Dockerfiles的更多信息,请参阅Docker用户指南和Dockerfile参考。

第3步:在撰写文件中定义服务

创建一个docker-compose.yml在您的项目目录中调用的文件并粘贴以下内容:

version: '3'services:
  web:
    build: .
    ports:     - "5000:5000"
  redis:
    image: "redis:alpine"

这个撰写文件定义了两个服务,web并且redis。网络服务:

  • 使用Dockerfile当前目录中构建的图像。

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

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

第4步:使用Compose构建和运行您的应用程序

  1. 从您的项目目录中,运行启动您的应用程序 docker-compose up。$ docker-compose up使用默认驱动程序创建网络“composetest_default”创建composetest_web_1 ...创建composetest_redis_1 ...创建composetest_web_1创建composetest_redis_1 ... done添加到composetest_web_1,composetest_redis_1 web_1 | *在http://0.0.0.0:5000/上运行(按CTRL + C退出)redis_1 | 1:C 17 Aug 22:11:10.480#oO0OoO0OoO0Oo Redis正在启动oO0OoO0OoO0Oo redis_1 | 1:C 17 Aug 22:11:10.480#Redis version = 4.0.1,bits = 64,commit = 00000000,modified = 0,pid = 1,刚刚开始redis_1 | 1:C 17 Aug 22:11:10.480#警告:没有指定配置文件,使用默认配置。为了指定配置文件,请使用redis-server /path/to/redis.conf web_1 | *使用stat redis_1 |重新启动 1:M 17 Aug 22:11:10.483 * Running mode = standalone,port = 6379。redis_1 | 1:M 8月17日22:11:10。483#警告:由于/ proc / sys / net / core / somaxconn设置为128的较低值,因此无法强制执行TCP backlog设置511。*调试器处于活动状态!redis_1 | 1:M 17 8月22日11:10.483#服务器初始化redis_1 | 1:M 17 Aug 22:11:10.483#警告您在内核中启用了透明巨大页面(THP)支持。这将导致Redis的延迟和内存使用问题。要解决此问题,请以root身份运行命令'echo never> / sys / kernel / mm / transparent_hugepage / enabled',并将其添加到您的/etc/rc.local中,以便在重新启动后保留设置。必须在禁用THP后重新启动Redis。web_1 | *调试器PIN:330-787-903 redis_1 | 1:M 8月22日:11:10.483 *准备接受连接撰写拉一个Redis图像,为您的代码建立一个图像,并启动您定义的服务。在这种情况下,

  2. http://0.0.0.0:5000/在浏览器中输入以查看应用程序正在运行。如果您在Linux,Docker for Mac或Docker本地使用Docker,则Web应用程序现在应该在Docker守护程序主机上的端口5000上侦听。指向您的网络浏览器http://localhost:5000以查找Hello World消息。如果这不解决,你也可以尝试http://0.0.0.0:5000。如果您在Mac上使用Docker Machine,请使用docker-machine ip MACHINE_VM获取Docker主机的IP地址。然后,http://MACHINE_VM_IP:5000在浏览器中打开。您应该在浏览器中看到一条消息:Hello World!我已经看过1次了。

  1. 刷新页面。数字应该增加。你好,世界!我已经看过2次了。

  1. 切换到另一个终端窗口,然后键入docker image ls以列出本地图像。此时列出图片应返回redisweb。$ docker image ls REPOSITORY TAG IMAGE ID创建大小composetest_web最新e2c21aa48cc1 4分钟前93.8MB python 3.4-alpine 84e6077c7ab6 7天前82.5MB redis alpine 9d8fa9aa0e5b 3周前27.5MB您可以使用图像检查图像docker inspect <tag or id>

  2. 停止应用程序,可以docker-compose down从第二个终端的项目目录中运行,或者在启动应用程序的原始终端中按CTRL + C。

第5步:编辑Compose文件以添加绑定安装

docker-compose.yml在您的项目目录中编辑以添加服务的绑定挂载web

version: '3'services:
  web:
    build: .
    ports:     - "5000:5000"
    volumes:     - .:/code
  redis:
    image: "redis:alpine"

volumes键将主机上的项目目录(当前目录)装载到/code容器中,允许您即时修改代码,而无需重新生成映像。

第6步:使用Compose重新构建并运行应用程序

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

$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...Creating composetest_redis_1 ...Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)...

Hello World再次查看Web浏览器中的消息,然后刷新以查看计数增量。

Shared folders, volumes, and bind mounts

  • 如果您的项目位于Users目录(cd ~)外部,那么您需要共享您正在使用的Dockerfile和卷的驱动器或位置。如果您收到运行时错误,指示找不到应用程序文件,卷挂载被拒绝或服务无法启动,请尝试启用文件或驱动器共享。批量安装需要共享驱动器用于C:\Users(Windows)或/Users(Mac)以外的项目,并且对于使用Linux容器的 Docker for Windows上的任何项目都需要共享驱动器。有关更多信息,请参阅Docker for Windows上的共享驱动器,Docker for Mac上的文件共享以及有关如何管理容器中的数据的一般示例。

  • 如果您在较旧的Windows操作系统上使用Oracle VirtualBox,则可能会遇到如本VB故障单中所述的共享文件夹问题。较新的Windows系统满足Docker for Windows的要求,并且不需要VirtualBox。

第7步:更新应用程序

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

  1. 更改问候语app.py并保存。例如,将Hello World!消息更改为Hello from Docker!:return'来自Docker的Hello!我已看过{}次。\ n'.format(count)

  2. 刷新浏览器中的应用程序。问候应该更新,并且柜台应该仍然在递增。

第8步:尝试其他一些命令

如果你想在后台运行你的服务,你可以将-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

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

$ docker-compose run web env

请参阅docker-compose --help查看其他可用的命令。您还可以安装bash和zsh shell的命令补全,它还会显示可用的命令。

如果您开始使用撰写作品docker-compose up -d,则可能需要在完成后与其停用服务:

$ docker-compose stop

你可以把所有东西都拿下来,用down命令彻底清除容器。通过--volumes也可以删除Redis容器使用的数据量:

$ docker-compose down --volumes

在这一点上,你已经看到了Compose如何工作的基础知识。

下一步去哪里

  • 接下来,请尝试Django,Rails或WordPress的快速入门指南

  • 浏览Compose命令的完整列表

  • 撰写配置文件参考

  • 要了解有关卷和绑定挂载的更多信息,请参阅管理Docker中的数据

前の記事: 次の記事: