Docker-compose

Docker Compose

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.

Let’s begin;

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.

  1. Development environments

  2. Automated testing environments

  3. 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.

Compose file

Let’s begin to create the compose file. docker-compose.yml

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;

Let’s check the status of containers;

Please note that, you can use the all core docker commands with docker-compose such as above.

Containers log can be seen as below;

You can stop docker-compose with below command;

Shutdown/Clean up;

  • 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;

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;

Let’s connect to the container with exec command;

All containers could connect to others via alias name. For example, we will use the ping to another server;

Some containers depend on another one. For example, web application depended with DB and cache. Create the new configuration file as below;

Outputs look like below;

app service depended to db and redis. Docker engine firstly runs the depended containers and then run the others.

Last updated