Docker compose-simplifies complex container applications

Source: Internet
Author: User
Tags docker compose docker run

Compose is a tool for defining and running complex Docker applications. You can define a multi-container application in a file, then use a command to launch your app, and all related actions will be automatically completed.

1. Installing Docker and compose
# Currently the latest Docker is 1.6.2,compose for 1.2.0curl-s https://get.docker.io/ubuntu/| sudo shsudo apt-get updatesudo apt-get Install Lxc-docker# Reference http://docs.docker.com/compose/install/#install-composecurl-l https://github.com /docker/compose/releases/download/usr/local/usr/local/bin/docker-compose above this method really slow out of Xiang, Can be installed via Python pip. Apt-get Install Python-pip python-devpip install-u docker-compose        

So compose installed, look at the Compose version information:

/usr/local1.2.  0 
2. Using Compose

Using compose requires a simple three-step process: First, use Dockerfile to define your application environment:

From Python:2.7ADD ./codeworkdir /codeRUN pip install-r requirements.txt  

Among them, the contents of Requirements.txt include:

Flaskredis

And then write a simple app.py in Python.

From Flask importflaskfrom Redis importredisimport osapp =flask (__name__) Redis =redis (host=' Redis ', port=6379) @app. Route ('/')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) 

The second step is to use a compose.yaml to define your application, and they can compose your app in the next isolated container.

WeB:  build:.  python app.py  Ports:-"5000:5000" Volumes:-.:/ Code links:-redisredis:image:redis        

The third step is to execute docker-compose up to launch your app, which will pull/run the two containers according to the Compose.yaml settings.

Creating myapp_redis_1...creating myapp_web_1...building web ... Step0:fromPython:2.72.7:pulling fromPython... Status:downloaded Newer imageForPython:2.7---> D833e0b23482step1:add. /code--->1c04b1b15808removing Intermediate container 9dab91b4410dStep Span class= "Hljs-number" >2:workdir/code---> Running in f495a62feac9---> ffea89a7b090attaching to myapp_redis_1, Myapp_web_1......redis_1 | [1] 17 may  10: 42:38.147 * The server is now ready Span class= "Hljs-keyword" >to Accept connections on port 6379web _1 | * Running on http://0.0. 0.0:5000/(press CTRL + C to quit) web_1 | * Restarting with stat              
3. yaml File Reference

In the Yaml file above, we can see the basic structure of the compose file. The first is to define a service name, and here are some of the option entries in the YAML service:

image: The ID of the mirror

build: Build directly from the dockerfile of PWD, rather than pull by the image option

links: Connect to those containers. Each row, formatted as Service[:alias], for example –db[:d atabase]

external_links: Connect to a container other than the Compose.yaml file, such as a container service that provides shared or generic services. Format with links

command: Replace the default command commands

ports: Export port. The format can be:

"ports:-"-"8000:8000"-"127.0.0.1:8001:8001"  

expose: Export the port, but not to the host's port. It is open only to links ' containers. Format to specify the port number directly.

volumes: Load path as volume, you can specify read-only mode:

volumes:-/var/lib/mysql-cache/:/tmp/cache-~/configs:/etc/configs/:ro   

volumes_from: Load all volumes of other containers or services

environment:-rack_env=development  -Session_secret

env_file: Import environment variable from a file, file format is rack_env=development

extends: Extend another service to override some of these options. A sample is as follows:

Common.ymlwebapp:  build:./webapp  environment:-debug=false-send_emails=  Falsedevelopment.ymlweb:extends: file:common.yml service:webapp ports:-"8000:8000"  links:-db environment:-debug=truedb: image:postgres      

net: The network mode of the container, which can be "bridge", "none", "Container:[name or id", and "host" one.

dns: You can set one or more custom DNS addresses.

dns_search: You can set the scan domain for one or more DNS.

The other working_dir, entrypoint, user, hostname, domainname, mem_limit, privileged, restart, stdin_open, tty, cpu_shares , and the docker run command is the same, these commands are single-line commands. For example:

Cpu_shares:working_dir:/code/code/entrypoint.shuser:postgresqlhostname:fooDomainName : foo.commem_limit:1000000000Privileged:truerestart:alwaysstdin_open:True TTY:true              
4. Docker-compose Common Commands

In the second section, both of docker-compose up these containers are running in the foreground. We can specify the-d command to start the container in a daemon manner. In addition, Docker-compose supports the following parameters:

--verbose: Output Details

-fDevelop a non-docker-compose.yml named Yaml file

-pSet a project name (default is directory name)

Docker-compose's actions include:

build: Build Services

kill -s SIGINT: sends a specific signal to the service.

logs: Output Log

port: Output bound port

ps: Output Running Container

pull: Image of the pull service

rm: Remove a stopped container

run: Run a service, such as Docker-compose run Web python manage.py shell

start: Runs a container that exists in a service.

stop: Stops a container that exists in a service.

up: Create + run + Attach container to service.

scale: Sets the number of containers that the service runs. Example: Docker-compose scale web=2 worker=3

Reference:

Compose Document

Docker compose-simplifies complex container applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.