http://hongge.blog.51cto.com/
Docker Compose Project
Compose Project Introduction
The compose project originates from the previous Fig project and is written in Python code. The Compose project is primarily used to orchestrate the deployment of Docker-based applications. It has a high degree of coordination with Docker/swarm.
Docker Compose is part of the Docker Orchestration service that allows users to quickly install Docker,swarm on other platforms, allowing Docker containers to function efficiently in a cluster, while Compose allows users to deploy distributed applications in a cluster. Simply put, Docker Compose belongs to an "application layer" service that allows users to define which container group to run which application, which supports dynamic change of the application and expands as needed.
Compared to kubernetes, the function is relatively simple, which is equivalent to a subset of Kubernetes.
Dockerfile Allows users to manage a single application container , and Compose lets the user in a template (yaml format ) defines a set of associated application containers in ( referred to as a project , that is, an item ), For example, a scheduler, two Web the service container plus the backend database service container, and so on.
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/03/wKiom1fRGqrhQc8gAACa9_ Ftpie004.png "" 411 "height=" 269 "/>
Before installing compose, you should install Docker first, and you won't say it here.
Installing Docker-compose
Install PIP First
Note: Pip is similar to Yum in Redhat, it is very convenient to install Python package
#wget https://bootstrap.pypa.io/get-pip.py
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/03/ Wkiom1frgqutx99maabhptfmuaa094.png "" 438 "height=" 102 "/>
#python get-pip.py
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/87/03/wKiom1fRGqzz4kVkAABljc7_3_ W077.png "" 444 "height=" 108 "/>
Re-install Compose
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/87/01/ Wkiol1frgqyc76twaaab4qldnyu521.png "" 445 "height="/>
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M00/87/03/wKiom1fRGqyh2ASDAAAYi_ 9spmc828.png "" 448 "height="/>
After the installation is successful, you can view the version of Docker-compose and the usage of the command.
#docker-compose–v
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/87/01/ Wkiol1frgq2qxqj7aaat0ol6opg608.png "" 447 "height="/>
#docker-compose–h
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/03/ Wkiom1frgq6rdrz0aabhkrgnkl8698.png "" "height=" 104 "/>
First, several terms are introduced.
Service: An application container that can actually run multiple instances of the same mirror.
Project: A complete business unit that consists of a set of associated application containers.
As can be seen, a project may be associated with multiple services (containers), Compose for project management.
Docker-compose Command Description
Most compose commands are run on one or more services, and if the service is not specified, the command will be applied to all services, and if you want to obtain all available information, use the command: docker-compose [COMMAND] --help
Build
Create or re-build services
When the service is created, it is marked as project_service (for example, composetest_db), and if a service is changed dockerfile or the content of the build directory can be used docker-compose build
to reconstruct it
Help
Show help and usage information for commands
Kill
A SIGKILL
container that is forced to stop operation by sending a signal that can be selectively passed, such as:
docker-compose kill -s SIGKINT
Logs
Display the log output of the service
Port
Output public information for port bindings
Ps
Display container
Pull
Pull Service Image
Rm
To delete a stopped container
Run
Run a one-time command on the service, such as:
docker-compose run web
Pythonmanage.py shell
Scale
The number of containers that are set to start for a service is specified in the form of such parameters: Service=num, for example:
docker-compose scale web=2 worker=3
Start
Start a container that already exists as a service
Stop
Stop running containers without deleting them, they can be restarted with commands docker-compose start
Up
Build, create, launch, attach to a container for a service
The connected services will be started unless they are already running
Run docker-compose up -d
will start the container in the background and make them run
–verbose
Show more output
–version
Show version number and exit
-f,–file file
Specify an optional compose Yaml file (default: DOCKER-COMPOSE.YML)
-p,–project-name Name
Specify an optional project name (default: current directory name)
DOCKER-COMPOSE.YML Command Description
Each service defined in DOCKER-COMPOSE.YML must explicitly specify an image or Build option, which corresponds to the same as entered in the Docker Run command line, for Docker run, The options specified in the Dockerfile file (such as CMD, EXPOSE, VOLUME, ENV) are default, so you do not have to specify it again in Docker-compose.yml
Image
Indicates the ID of the image, the image ID can be local or remote, and if the local does not exist, compose will try to pull down
Image:ubuntu
Image:orchardup/postgresql
Image:a4bc65fd
Build
This parameter specifies the path to the Dockerfile file, which is also the build environment that is sent to the daemon (which is a bit), compose will be built and tagged with an existing name and then use this image
Build:/path/to/build/dir
Command
Overrides the default command, overwriting the command that is executed by default after the container starts
Command: Commands to execute
Links
To connect to a container in another service, you can specify the service name and the alias of the link, or specify only the service name
Links
-DB
-Db:database
-Redis
At this point, inside the container, an entry is created with an alias in the/etc/hosts file, just like this:
172.17.2.186 DB
172.17.2.186 Database
172.17.2.186 Redis
Environment variables will also be created, and arguments about the environment variables will be described later
External_links
Connect to a container that is launched outside of this docker-compose.yml file or compose, especially for containers that provide shared and public services. When specifying a container name and alias, External_links follows the same semantic usage as links
External_links:
-Redis_1
-Project_db_1:mysql
-Project_db_1:postgresql
Ports
exposing ports, specifying both ports (Host: Container), or just the port of the container (the host is randomly assigned a port)
Note: When a port is mapped in the form of a host: container, if the port of the container is less than 60, an error may occur because YAML resolves the data in such a format as XX:YY to 60 binary data, for this reason, always remember to explicitly specify the port mapping as a string
Ports
-"3000"
-"8,000:8,000"
-"49,100:22"
-"127.0.0.1:8001:8001"
Expose
Exposing ports without having to publish them to the host, but only to the linked services (linked service), only the internal ports can be specified
Expose
-"3000"
-"8000"
Volumes
The mount path is the most volume, optionally specifying a path (host: Container) on a host, or a mode (host: container: RO) that can be used
Volumes
-/var/lib/mysql
-Cache/:/tmp/cache
-~/configs:/etc/configs/:ro
Volumes_from:
Mount all of its volumes from another service or container.
Volumes_from:
-Service_Name
-Container_name
Environment
Add environment variables, you can use an array or a dictionary, only one key environment variable can find the corresponding value on the machine running compose, which helps the value of the encrypted or special host
Environment:
Rack_env:development
Session_secret:
Environments:
-Rack_env=development
-Session_secret
Env_file
Add an environment variable from a file
If a template file is specified by docker-compose-f files, the path in Env_file is based on the template file path. If there is a conflict between the variable name and the environment directive, the latter will prevail.
Env_file:. env
Env_file:
-./common.env
-./apps/web.env
-/opt/secrets.env
Each row in the environment variable file must conform to the format, and the comment lines that begin with # are supported.
# Common.env:Set Rails/rack Environment
Rack_env=development
Net
Network mode, you can specify these values in the--net parameter of the Docker client
NET: "Bridge"
NET: "None"
NET: "Container:[name or ID"
NET: "Host"
Dns
Custom DNS service, which can be a single value or a list
dns:8.8.8.8
Dns:
-8.8.8.8
-9.9.9.9
Dns_search
Custom DNS search scopes can be individual values or a list
Dns_search:example.com
Dns_search:
-Dc1.example.com
-Dc2.example.com
A simple three-step process is required to use compose:
First, use Dockerfile or Docker commit to define your application environment
The second step is to use a docker-compose.yml to define your application, and they can compose your app in the next isolated container.
The third step is to execute Docker-compose up-d to launch your app, which will pull/run related containers based on docker-compose.yml settings.
Test case
Below, we create a classic Web project: A Haproxy that mounts three web containers.
The Docker-haproxy directory, which is the project working directory, in which you create two subdirectories: Haproxy and web.
#mkdir-P/root/docker-haproxy/{haproxy,web}
Create a docker-compose.yml file under the/root/docker-haproxy/directory
Create the Haproxy Master profile in the/root/docker-haproxy/haproxy directory haproxy.cfg
Storing web site page files in the/root/docker-haproxy/web directory
The directory structure looks like this:
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/01/ Wkiol1frgq7yah11aabyelhmogg189.png "" "height=" 182 "/>
Web Sub-directory:
Generate a index.html file with the contents:
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/01/ Wkiol1frgsorrprqaaatv4k5nh0377.png "" Height= "/>"
Haproxy directory:
In which a haproxy.cfg file is generated
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/87/01/ Wkiol1frgstjoijwaac2q4d90tg231.png "" 443 "height=" 407 "/>
Docker-compose.yml
Write the Docker-compose.yml file, which is the main template file used by compose. The content is simple, specifying 3 web containers, and 1 haproxy containers.
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/87/03/ Wkiom1frgstsm8axaac13mxqff4166.png "" 449 "height=" 424 "/>
To run the Compose project:
Execute Docker-compose up-d in the Docker-haproxy directory to launch your app
#cd Docker-haproxy
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/03/ Wkiom1frgsxi4gfnaablr3sspxe008.png "" "height=" "/>"
To view container startup conditions
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/01/wKioL1fRGsaAropUAAB_ 22tviyu098.png "" Height= "/>"
Or
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/03/ Wkiom1frgsbtgfuzaaba9rgw1jm191.png "" "height=" "/>"
Open Browser native native ip:http://192.168.1.105
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/87/01/ Wkiol1frgses9bdgaaau52cnucm563.png "" 441 "height=" 109 "/>
Open Browser native native ip:http://192.168.1.105:70
650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/87/01/ Wkiol1frgsfjk9qhaaew0ga3qcm406.png "" 441 "height=" 196 "/>
http://hongge.blog.51cto.com/
Docker Compose Project