installation
1. Why use docker-compose?
Docker Compose easy to use and efficient management of the container, which is used to define and run a multi-application tool container Docker
2, other pre-conditions need to:
yum install -y py-pip python-dev libffi-dev openssl-dev gcc libc-dev make
3, prior to this command if curl is not installed on your computer, you need to install:
yum install -y curl
4, installation Docker Compose (1.20.1 -> docker18.03.1-ce) can use the following command to automatically adapt to download version Compose, and add execute permissions to install script
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
5, see the installation was successful:
docker-compose -v
6, the command is executed in the Compose path where the docker-compose.yml automatically constructed using the mirror image and starting container
docker-compose up
docker-compose up -d // back up and running container
Engineering, service, container
-
Docker Compose will manage the container is divided into three, namely project (project), service (service), container (container)
-
All files (docker-compose.yml) under Docker Compose form a run directory project, a project comprises a plurality of services, each defined image container runs, parameters, dependent, a service may comprise a plurality of containers instances
Common docker-compose commands and configuration:
1, lists all running container
docker-compose ps
2, view the service log output
docker-compose logs
3, port: Print binding common port, the following command can output port eureka public service bound to port 8761
docker-compose port eureka 8761
4, to build or re-build service
docker-compose build
5, start the specified service already exists in the container
docker-compose start eureka
6, stop the service has been running container
docker-compose stop eureka
7, delete the specified container service
docker-compose rm eureka
8, constructed, starting container
docker-compose up
9, to stop the specified service by transmitting a signal SIGKILL container
docker-compose kill eureka
10, pull: Mirror download service
docker-compose pull xxxx
11, the service is provided to specify the number of containers luck, specified in the form of service num =
docker-compose scale user=3 movie=3
12, execute a command on a service
docker-compose run web bash
docker-compose.yml property
Specify docker-: version written in the file format compose.yml
services: a plurality of collection containers
build: build configuration, Compose will automatically use it to construct the mirrors, this value may be a path, an object may be used to specify the parameters Dockerfile
command: override the default implementation of the container after the start command
dns: dns server configuration, can be a value or a list
dns_search: Configuring DNS search domain, can be a value or a list of
environment: the environment variable configuration, you can use an array or a dictionary in two ways
env_file : get environment variables from a file, you can specify a file path or path list, which is a lower priority than environment specified environment variable
expose: expose the port, the port will only serve to expose the connection without exposure to the host
image: specified service mirror used
network_mode: set the network mode
ports: port definition of external exposure, and expose the corresponding
links: the specified vessel is connected to the current connection, can be an alias to avoid container ip embodiment results in restart dynamically changing not connection
volumes: volume mount path
logs: Chi output
Docker Compose Other
Updated container
- When configuring the service changes, you can use docker-compose up command to update the configuration
- At this point, Compose deletes the old container and create a new container, the new vessel will join the network to a different IP address, the name remains the same, pointing to the old content from any of the connection will be closed, re-find and connect up new container
- May be used between the service name of each service access, allowing links define an alias, the alias used to access other such services
version: '2' services: web: build: . links: - "db:database" db: image: postgres
In this way you can use the Web service or database db db visit served as a hostname
docker-compose.yml Case:
# cat docker-compose.yml
version: '2' services: db: image: mysql:5.7 volumes: - "./.data/db:/var/lib/mysql" restart: always environment: MYSQL_ROOT_PASSWORD: wordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest links: - db ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_PASSWORD: wordpress