Compose is a tool for defining and running multi-container Docker applications. Thanks to compose, a compose file can configure the application’s services. After that, using a single command to create and start all the services from the configuration. Compose is very useful for development,testing and staging environments, as well as CI workflows.
Products and systems that we use on a daily basis or that we provide to our customers for use are formed by a combination of multiple services such as web servers,application servers,database servers,etc. That is unlikely that medium and large systems will be able to use and maintain using the Docker CLI. In such systems, it is necessary to have a separate vehicle to execute the tasks of the Containers, to stop them, to indicate their relations with each other, to manage them in a simple way.
Docker Compose is used for easily managing test environments and developmnet. The Docker Compose provides easy installation, maintenance and general use in these environments.With new developments by Docker Inc., the system is being used in production environments where stability, performance, consistency, redundancy and high availability are important. The Docker Compose should not be confused with clustering tools such as Docker Swarm and Kubernetes. Swarm and Kubernetes are striving to fulfill the above listed features of production systems.
Services (Containers) that are specified in the docker-compose.yml file and associated with each other can be removed with a single command, stopped with a single command, and removed (deleted) with a single command.
File Structure of docker-compose.yml File
Before begining of docker compose example, This is very necessary to inform about docker compose file structure.
As can be seen from this file extansion ,docker-compose.yml file is in YAML format.YAML is a data serialization format such as JSON and XML. YAML is much easier to read and use than JSON for simple configurations.
Main structure of a compose file consist of three major part. These are service,volume and network configuration.
This configuration is specified with tag “service:”. Each service description specifies the details of the containers running on it. Now let’s sample the configuration options given under the service tag, one by one.
This Tag is configured with which Dockerfile to build the relevant service.This can be used in two ways. Only the relative path of the Dockerfile to be used in the build is sufficient for first one. This can be used as follows
In the second use, the build context (in which folder the build will be relative to the Dockerfile) and the name of which Dockerfile to use will be given. This can be used as follows.
This configuration option is used when you want to use a different command from the command provided by the Image used for the corresponding service.
This option that does not need to be used in simple scenarios but works well in some complex scenarios. As described in previous chapters, Compose Container automatically generates the names from the included folder, service name, and the number of Containers previously running in this service description. This configuration option can be used to specify the Container’s name to be created from the Service definition, instead of automatically generating that.
docker-compose up command starts the services according tı their dependencies in compose file whre more than one service description is found. If there is no relation between the service’s, Starting process begins according to the definition order. depends_on option determine relationship of services with each other. When a service name is given docker-compose uğp with parameter, compose starts the services to which the relevant service is dependent and then starts the corresponding service.
The service creates a specific container. This container uses dns service. With this command helps to chnage dns server.
New environment variables to Containers to be created from the service can be added with this option.
This option is used for opening ports between containers without opening to the host. This command works like expose command in dockerfile
This command defines images. These images start containers that created by services. If there is a build option in the definition of service, that is, if containers are to be created from an image to be build with Dockerfile instead of from a ready image, this option is given to the image to be created. when the command used as follows, this determines the image name which will pull from DockerHub.
That option defines which network the container is connected to.
This option specifies the ports to be forwarded from the containers to the host. Ports command is one of the most used options. In the following example, the first line maps to the same port in the 8080 port Container in the Host. In the second line, all ports between 10000 and 11000 are forwarded from Host to Container.
Volumes option can be used in three diffrent ways. These are :
1- Mounting a folder on the host to the Container
2- Creation of a volume on a container
After the Container has been stopped and even if it has not been intentionally deleted, the option can be used as follows to create a Volume that can be accessed even after removal from the system.
3- A Volume defined in the Volumes configuration can become visible on the Container side
Compose allows to define a Volume (Named Volumes) which can be accessed by Containers created from all the defined services and whose life cycle can be managed independently of the Containers.
The volumes are defined as Container based. The volumes that can be accessed by all services in the Compose file and which can also be viewed with the docker volume command provided by the Docker CLI are named Named Volumes.
Docker Compose Example
In this example, WordPress will be run in an isolated environment built with docker containers. For begining, create an empty directory then name that. After enter that directory, create a docker-compose.yml file. This file starts the WordPress blog and separate mysql instance with a volume for data persistance. The yml file is shown in below.
After creating a yaml file process, open the terminal and find the directory that the compose file is included. To build the projcet, write docker-compose up -d .
After this finished, open web browser and write localhos: 8000. The screen should show as follows if there is no mistake in compose up process.
To close docker compose, write docker-compose down, this helps to remove the containers and default network. However Worpress database will be preserved.