Docker compose操作指南
2016-04-28 11:50
711 查看
Step 1: Setup
Create a directory for the project:$ mkdir composetest $ cd composetest
With your favorite text editor create a file called
app.pyin your project directory.
from flask import Flaskfrom 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)
Create another file called
requirements.txtin your project directory and add the following:
flaskredis
These define the applications dependencies.
Step 2: Create a Docker image
In this step, you build a new Docker image. The image contains all the dependencies the Python application requires, including Python itself.In your project directory create a file namedDockerfileand add the following:
FROM python:2.7ADD . /codeWORKDIR /codeRUN pip install -r requirements.txtCMD python app.py
This tells Docker to:Build an image starting with the Python 2.7 image.
Add the current directory
.into the path
/codein the image.
Set the working directory to
/code.
Install the Python dependencies.
Set the default command for the container to
python app.py
For more information on how to write Dockerfiles, see the Docker user guideand the Dockerfile reference.Build the image.
$ docker build -t web .
This command builds an image named
webfrom the contents of the current directory. The command automatically locates the
Dockerfile,
app.py, and
requirements.txtfiles.
Step 3: Define services
Define a set of services usingdocker-compose.yml:Create a file called docker-compose.yml in your project directory and add the following:
version: '2'services: web: build: . ports: - "5000:5000" volumes: - .:/code depends_on: - redis redis: image: redis
This Compose file defines two services,
weband
redis. The web service:Builds from the
Dockerfilein the current directory.
Forwards the exposed port 5000 on the container to port 5000 on the host machine.
Mounts the project directory on the host to
/codeinside the container allowing you to modify the code without having to rebuild the image.
Links the web service to the Redis service.
The
redisservice uses the latest public Redis image pulled from the Docker Hub registry.
Step 4: Build and run your app with Compose
From your project directory, start up your application.$ docker-compose up Pulling image redis... Building web... Starting composetest_redis_1... Starting composetest_web_1... redis_1 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3web_1 | * Running on http://0.0.0.0:5000/web_1 | * Restarting with stat
Compose pulls a Redis image, builds an image for your code, and start the services you defined.Enter
http://0.0.0.0:5000/in a browser to see the application running.
If you’re using Docker on Linux natively, then the web app should now be listening on port 5000 on your Docker daemon host. If http://0.0.0.0:5000doesn’t resolve, you can also try http://localhost:5000.If you’re using Docker Machine on a Mac, use
docker-machine ip MACHINE_VMto get the IP address of your Docker host. Then,
open http://MACHINE_VM_IP:5000[/code] in a browser.You should see a message in your browser saying:Hello World! I have been seen 1 times.Refresh the page.
The number should increment.Step 5: Experiment with some other commands
If you want to run your services in the background, you can pass the-dflag (for “detached” mode) todocker-compose upand usedocker-compose psto see what is currently running:$ 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/tcpThedocker-compose runcommand allows you to run one-off commands for your services. For example, to see what environment variables are available to thewebservice:$ docker-compose run web envSeedocker-compose --helpto see other available commands. You can also install command completion for the bash and zsh shell, which will also show you available commands.If you started Compose withdocker-compose up -d, you’ll probably want to stop your services once you’ve finished with them:$ docker-compose stopAt this point, you have seen the basics of how Compose works.
相关文章推荐
- centos6.5上安装docker
- docker容器的网络信息查看
- 在windows下的安装Docker的教程
- 8个你可能不知道的Docker知识
- 在Docker中自动化部署Ruby on Rails的教程
- 搭建基于Docker的PHP开发环境的详细教程
- 利用OpenVSwitch在多台主机上部署Docker的教程
- ubuntu14.04+docker的安装及使用
- Docker 清理命令集锦
- 再Docker中架设完整的WordPress站点全攻略
- 基于 Docker 开发 NodeJS 应用
- 使用Docker来加速构建Android应用的基本部署思路解析
- 在Docker上部署Python的Flask框架的教程
- 在Docker上开始部署Python应用的教程
- 详解在Python和IPython中使用Docker
- python脚本监控docker容器
- 使用IPython来操作Docker容器的入门指引
- OSX下brew安装docker(boot2docker)
- docker 设置TLS远程访问
- mesos + marathon + docker部署