Docker的services介绍
docker的service是比较好用的一项内容。
官网给出了services的定义:
In a distributed application, different pieces of the app are called “services.” For example, if you imagine a video sharing site, it probably includes a service for storing application data in a database, a service for video transcoding in the background after a user uploads something, a service for the front-end, and so on.
Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.
简单说就是一种service可以代表一种角色,实现不同的功能。比如Redis可以是一个服务,web应用可以是一个服务,nginx是一个服务,MySQL也是一个服务。
docker使用docker compose来管理不同的服务,也可以说管理不同的容器。因此你需要做两件事:
- 安装docker-compose,要按照官网来安装,官网写的非常清楚,我不赘述。我用pip装之后,不能正确执行。
- 编写docker-compose.yml
我就写了非常简单的例子:
工作目录下:
docker-compose.yml Dockefile app.py
Dockerfile:
ROM ubuntu:16.04
RUN apt-get -y update
RUN apt-get install -y python-dev python-pip
RUN apt-get install -y python-setuptools
#RUN apt-get install -y libmysqlclient-dev
WORKDIR /dailyblog
ADD . /dailyblog
RUN pip install Flask redis
#对外暴露端口
EXPOSE 80 8080 8000 5000
CMD ["python","app.py"]
app.py
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis')
@app.route('/')
def hello():
count = redis.incr('visits')
return 'hello world,it is visited for %s times' % count
if __name__ == "__main__":
app.run(host='0.0.0.0',port=80)
docker-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "5000:80"
volumes:
- .:/dailyblog
links:
- redis
redis:
image: redis
上面文件中指定了两个service,一个web引用,一个redis.links建立了web和Redis的连接。如果web有本地镜像或者已经提交的镜像,就不用build,改成:image,比如现在要用我自己仓库的镜像,就改成:image:hbnnforever/flaskapp:2nd
编写好之后,执行:
docker-compose up -d
之后就可以正常访问web应用了 localhost:5000
从上面的例子可以发现,有了services的概念,有了docker-compose,更加方便了我们管理不同的容器,我的最终目标就是将nginx,Redis,memcache,MySQL,web应用等不同容器统一管理起来,完成最后的部署。因此需要学的东西还有很多。
--------EOF---------
微信分享/微信扫码阅读
微信分享/微信扫码阅读