In terms, Docker compose that means as the below;
Compose is a tool for defining and running complex applications with Docker. With Compose, you define a multi-container application in a single file, then spin your application up in a single command which does everything that needs to be done to get it running.
Docker Compose is a tool for defining and running multi-container Docker applications. It’s just that.
With Docker compose, we will run more than one containers together and dependent another one. For example, you can define a container for database, web server and cache with depending on DB container. In this example, we will use Redis.
Note : Docker Compose uses the same API used by other Docker commands((CLI commands)) and tools.
We will use the docker-compose.yml configuration file for creating the application’s services. It’s a simple YAML file. After creating compose configuration file, we will start all the services from the configuration with only one command.
Docker compose can be used in many different requirements and/or in many different ways.
Automated testing environments
Single host deployments
Firstly learn the version of docker compose on your docker installation as below;
And then listing docker images which installed on docker.
Docker compose was already installed on docker installation.
We have four images. We will use all of them in our docker compose example.
Let’s begin to create the compose file. docker-compose.yml
version: '2' #(1)#(2)services:server_a:image: nginx:latestports:- "8001:80" server_b:image: nginx:latestports:- "8002:80" server_c:image: nginx:latestports:- "8003:80"
We’ve defined the sample configuration file. It contains version attribute and service definitions. We can define service at the services section. server_? is the alias name of the service. Image attr is that means which image will be run and ports definitions for the docker container between host.
version means that what version of the docker compose. We're using version 2. We will start all service only via docker-compose up command. Go to the docker-compose.yml file directory and run it;
Starting three nginx server and go to localhost:8001 and etc. You can find that nginx up!
If you want to start the docker-compose with the detached mode you can do it by giving command;
docker-compose up -d
Let’s check the status of containers;
iyzi-it125:docker alican.akkus$ docker-compose psName Command State Ports-----------------------------------------------------------------------docker_server_a_1 nginx -g daemon off; Up 0.0.0.0:8001->80/tcpdocker_server_b_1 nginx -g daemon off; Up 0.0.0.0:8002->80/tcpdocker_server_c_1 nginx -g daemon off; Up 0.0.0.0:8003->80/tcpiyzi-it125:docker alican.akkus$
Please note that, you can use the all core docker commands with docker-compose such as above.
Containers log can be seen as below;
iyzi-it125:docker alican.akkus$ docker-compose logsAttaching to docker_server_a_1, docker_server_c_1, docker_server_b_1server_a_1 | 172.19.0.1 - - [15/May/2017:18:13:34 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 OPR/45.0.2552.635" "-"server_a_1 | 2017/05/15 18:13:34 [error] 5#5: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.19.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8001", referrer: "http://localhost:8001/"server_a_1 | 172.19.0.1 - - [15/May/2017:18:13:34 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://localhost:8001/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 OPR/45.0.2552.635" "-"iyzi-it125:docker alican.akkus$
You can stop docker-compose with below command;
iyzi-it125:docker alican.akkus$ docker-compose stopStopping docker_server_a_1 ... doneStopping docker_server_c_1 ... doneStopping docker_server_b_1 ... doneiyzi-it125:docker alican.akkus$
docker-compose down: will remove the containers.
docker-compose down –volumes: will remove the containers and volumes.
Note: You can see all docker-compose command list by typing docker-compose and output shows as;
iyzi-it125:docker alican.akkus$ docker-composeDefine and run multi-container applications with Docker.Usage:docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]docker-compose -h|--helpOptions:-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)-p, --project-name NAME Specify an alternate project name (default: directory name)--verbose Show more output-v, --version Print version and exit-H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify--tlscacert CA_PATH Trust certs signed only by this CA--tlscert CLIENT_CERT_PATH Path to TLS certificate file--tlskey TLS_KEY_PATH Path to TLS key file--tlsverify Use TLS and verify the remote--skip-hostname-check Don't check the daemon's hostname against the name specifiedin the client certificate (for example if your docker hostis an IP address)Commands:build Build or rebuild servicesbundle Generate a Docker bundle from the Compose fileconfig Validate and view the compose filecreate Create servicesdown Stop and remove containers, networks, images, and volumesevents Receive real time events from containersexec Execute a command in a running containerhelp Get help on a commandkill Kill containerslogs View output from containerspause Pause servicesport Print the public port for a port bindingps List containerspull Pull service imagespush Push service imagesrestart Restart servicesrm Remove stopped containersrun Run a one-off commandscale Set number of containers for a servicestart Start servicesstop Stop servicestop Display the running processesunpause Unpause servicesup Create and start containersversion Show the Docker-Compose version informationiyzi-it125:docker alican.akkus$
Note: All of the docker-compose commands may be not running correctly because yaml file is placed the other directory.
İf you want to remove all container you can use this command;
Sometimes we don’t want to run all containers from the docker-compose.yml configuration file. We will use partial start as following syntax -> docker-compose up service_name other_service_name for example;
iyzi-it125:docker alican.akkus$ docker-compose up server_a server_bCreating docker_server_a_1Creating docker_server_b_1Attaching to docker_server_a_1, docker_server_b_1
Let’s connect to the container with exec command;
iyzi-it125:docker alican.akkus$ docker-compose up -d server_a server_bStarting docker_server_a_1Starting docker_server_b_1iyzi-it125:docker alican.akkus$ docker-compose exec server_a /bin/bash[email protected]:/# uname -aLinux b9bb8ced17be 4.9.27-moby #1 SMP Thu May 11 04:01:18 UTC 2017 x86_64 GNU/Linux
All containers could connect to others via alias name. For example, we will use the ping to another server;
iyzi-it125:docker alican.akkus$ docker-compose exec server_a ping -c 3 server_bPING server_b (172.19.0.3): 56 data bytes64 bytes from 172.19.0.3: icmp_seq=0 ttl=64 time=0.761 ms64 bytes from 172.19.0.3: icmp_seq=1 ttl=64 time=0.222 ms64 bytes from 172.19.0.3: icmp_seq=2 ttl=64 time=0.164 ms--- server_b ping statistics ---3 packets transmitted, 3 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.164/0.382/0.761/0.269 msiyzi-it125:docker alican.akkus$
Some containers depend on another one. For example, web application depended with DB and cache. Create the new configuration file as below;
version: '2' #(1)#(2)services:db:image: mysql:latestvolumes:- db_data:/var/lib/mysqlrestart: alwaysenvironment:MYSQL_ROOT_PASSWORD: 1234MYSQL_DATABASE: challengeMYSQL_USER: rootMYSQL_PASSWORD: 1234ports:- "3306:3306"app:image: aakkus/iyzico-challengeports:- "8080:8080"depends_on:- db- redis redis:image: redis:latestports:- "6379:6379"volumes:db_data:
Outputs look like below;
iyzi-it125:docker alican.akkus$ docker-compose up -dWARNING: Found orphan containers (docker_server_b_1, docker_server_a_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.Creating docker_db_1Creating docker_redis_1Creating docker_app_1iyzi-it125:docker alican.akkus$
app service depended to db and redis. Docker engine firstly runs the depended containers and then run the others.