摘要:的安装可以参考官网,如果安装了或者则直接就存在了。创建项目目录创建的程序,功能就是利用的方法进行访问计数。还有很多实用的命令,比如等,可以通过来查看更多例子参考官网文档
</>复制代码
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.
Dockerfile 可以让用户管理一个多带带的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等,就拿官网的 Python 例子来说,功能很简单,利用 redis 的 incr 的功能对页面的访问量进行统计。
docker-compose 的安装可以参考官网,如果安装了 Docker for Mac 或者 Docker for windows 则直接就存在了。
创建项目目录:
</>复制代码
$ mkdir composetest
$ cd composetest
创建 Python 的程序 app.py ,功能就是利用 redis 的 incr 方法进行访问计数。
</>复制代码
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host="redis", port=6379)
@app.route("/")
def hello():
redis.incr("hits")
return "Hello World! I have been seen %s times." % redis.get("hits")
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
由于 Python 依赖的 flask、redis 组件都需要另外安装,比如使用 pip来安装,多带带设置一文件 requirements.txt,内容如下:
</>复制代码
flask
redis
创建 service 依赖的第一个 image,app.py 程序的运行环境,利用 Dockerfile 来制作,内容如下:
</>复制代码
FROM python:2.7 #基于 python:2.7 镜像
ADD . /code #将本地目录中的内容添加到 container 的 /code 目录下
WORKDIR /code #设置程序工作目录为 /code
RUN pip install -r requirements.txt #运行安装命令
CMD python app.py #启动程序
Dockerfile 创建好就可以制作镜像了,运行 docker build -t compose/python_app . ,成功后通过 docker images查看即能看到:
</>复制代码
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose/python_app latest 1a92fed00abd 59 minutes ago 680.4 MB
接下来制作 docker-compose 需要的配置文件 docker-compose.yml, version 要选择 2 ,1的版本很古老了,配置中可以看到创建了 2 个 service,web 和 redis ,各自有依赖的镜像,其中 web 开放 container 的5000端口,并与 host 的5000端口应对,方便通过 localhost:5000 来访问, volumes 即将本地目录中的文件加载到容器的 /code 中,depends_on 表名 services web 是依赖另一个 service redis的,完整的配置如下:
</>复制代码
version: "2"
services:
web:
image: compose/python_app
ports:
- "5000:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
前提都搞定了,就差最后一步启动了,命令 docker-compose up ,成功后如下:
</>复制代码
Attaching to composetestbypython_redis_1
redis_1 | 1:C 04 Nov 10:35:17.448 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1 | _._
redis_1 | _.-``__ ""-._
redis_1 | _.-`` `. `_. ""-._ Redis 3.2.5 (00000000/0) 64 bit
redis_1 | .-`` .-```. ```/ _.,_ ""-._
redis_1 | ( " , .-` | `, ) Running in standalone mode
redis_1 | |`-._`-...-` __...-.``-._|"` _.-"| Port: 6379
redis_1 | | `-._ `._ / _.-" | PID: 1
redis_1 | `-._ `-._ `-./ _.-" _.-"
redis_1 | |`-._`-._ `-.__.-" _.-"_.-"|
redis_1 | | `-._`-._ _.-"_.-" | http://redis.io
redis_1 | `-._ `-._`-.__.-"_.-" _.-"
redis_1 | |`-._`-._ `-.__.-" _.-"_.-"|
redis_1 | | `-._`-._ _.-"_.-" |
redis_1 | `-._ `-._`-.__.-"_.-" _.-"
redis_1 | `-._ `-.__.-" _.-"
redis_1 | `-._ _.-"
redis_1 | `-.__.-"
redis_1 |
redis_1 | 1:M 04 Nov 10:35:17.450 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | 1:M 04 Nov 10:35:17.450 # Server started, Redis version 3.2.5
redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add "vm.overcommit_memory = 1" to /etc/sysctl.conf and then reboot or run the command "sysctl vm.overcommit_memory=1" for this to take effect.
redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command "echo never > /sys/kernel/mm/transparent_hugepage/enabled" as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | 1:M 04 Nov 10:35:17.451 * The server is now ready to accept connections on port 6379
此时通过compose 的 ps 命令也能看到 docker-compose ps :
</>复制代码
docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
composetestbypython_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
composetestbypython_web_1 /bin/sh -c python app.py Up 0.0.0.0:5000->5000/tcp
ok ,在浏览器中访问 http://localhost:5000 就能看到最终的样子啦。
{% qnimg docker/2016-11-04-22-40-11.jpg title: alt: "class:" %}
docker-compose 还有很多实用的命令,比如 logs、build、start、stop 等,可以通过 docker-compose --help来查看:
</>复制代码
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
--verbose Show more output
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don"t check the daemon"s hostname against the name specified
in the client certificate (for example if your docker host
is an IP address)
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pulls service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
更多 docker-compose 例子参考官网 doc 文档 :
Docker-compose with Django : https://docs.docker.com/compo...
Get started with Rails : https://docs.docker.com/compo...
Get started with WordPress : https://docs.docker.com/compo...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/26754.html
摘要:列出项目中目前的所有容器。删除所有停止状态的服务容器。一般的,当指定数目多于该服务当前实际运行容器,将新创建并启动容器反之,将停止容器。命令说明恢复处于暂停状态中的服务。 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。其代码目前在 https://github.com/docker/com... 上开源。 介绍 Compose 定位...
摘要:列出项目中目前的所有容器。删除所有停止状态的服务容器。一般的,当指定数目多于该服务当前实际运行容器,将新创建并启动容器反之,将停止容器。命令说明恢复处于暂停状态中的服务。 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。其代码目前在 https://github.com/docker/com... 上开源。 介绍 Compose 定位...
摘要:使用不再需要使用脚本来启动容器。通过配置安装可以通过修改中的版本,自定义您需要的版本。 本篇将使用 DaoCloud 源在 Ubuntu 上简单快速安装 docker 及 docker-compose并添加了通过 Dockerfile 及 docker-compose.yml 使用 nginx 的示例本篇文章所用系统信息如下 Distributor ID: Ubuntu Descri...
摘要:使用不再需要使用脚本来启动容器。通过配置安装可以通过修改中的版本,自定义您需要的版本。 本篇将使用 DaoCloud 源在 Ubuntu 上简单快速安装 docker 及 docker-compose并添加了通过 Dockerfile 及 docker-compose.yml 使用 nginx 的示例本篇文章所用系统信息如下 Distributor ID: Ubuntu Descri...
摘要:命令对象与格式命令的对象既可以是项目本身,也可以指定为项目中的服务或者容器。使用指定的模版文件,默认为指定项目名称,默认使用所在目录名称。 命令对象与格式 命令的对象既可以是项目本身,也可以指定为项目中的服务或者容器。 docker-compose [-f=...] [options] [COMMAND] [ARGS ...] -f, --file FILE 使用指定的Compose...
阅读 2014·2021-11-23 09:51
阅读 1269·2019-08-30 15:55
阅读 1645·2019-08-30 15:44
阅读 785·2019-08-30 14:11
阅读 1176·2019-08-30 14:10
阅读 944·2019-08-30 13:52
阅读 2658·2019-08-30 12:50
阅读 647·2019-08-29 15:04
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要