Docker series (Part 14): Docker Compose
Tags: docker
Published: 2022-07-11 13:09:54

Till now we have used CLI to run containers, now we can run multiple containers with just a file . This file is .yaml file basically. 
In the [Part 13](https://dev.to/mitul3737/docker-series-part-13-edit-codes-running-in-containers-using-bind-mounts-8ih), we used

```
docker container run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve
```
in .yaml file, we will use:

```
services:
  jekyll:
    image: bretfisher/jekyll-serve
    volumes:
      - .:/site
    ports:
      - '80:4000'
```
`jekyll` is the container name . we have set the image to `bretfisher/jekyll-serve`. For volume , we used `.:/site`which means `$(pwd):/site` . Here "." means the current directory which $(pwd) also refers too. We have set ports to `80:4000`.


You can compare the command and the .yaml file and see the difference. 

Also, you can see another example of .yaml file

```
services:

  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: example
      WORDPRESS_DB_PASSWORD: examplePW
    volumes:
      - ./wordpress-data:/var/www/html

  mysql:
    # we sue mariadb here for arm support
    # mariadb is a fork of MySQL that's often faster and better multi-platform
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: examplerootPW
      MYSQL_DATABASE: wordpress
      MYSQL_USER: example
      MYSQL_PASSWORD: examplePW
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:

```
Here 2 services used and 1 volumes used . You can see ENvironment variables here
`environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: example
      WORDPRESS_DB_PASSWORD: examplePW`

You can also see that, we have used volume 

` volumes:
      - ./wordpress-data:/var/www/html`
where . means current directory and `wordpress-data:` is to set the volume name . Also the rest part (/var/www/html) is to set the volume path. 

Also for the other container, we used 
` environment:
      MYSQL_ROOT_PASSWORD: examplerootPW
      MYSQL_DATABASE: wordpress
      MYSQL_USER: example
      MYSQL_PASSWORD: examplePW
   volumes:
      - mysql-data:/var/lib/mysql

volumes:
  mysql-data:`

Here we also set this environmental variables and also volumes. 

**Compose CLI**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2ighxbnj0r5du5pz3jp.png)
 
Let's use it and learn.

Let's use this compose-sample-2 folder from this [repo](https://github.com/bretfisher/udemy-docker-mastery)

lets get into the docker-compose.yml file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8uxbpn3zzijgq7vtrt5m.png)

Look that we have nginx.config file and in our .yaml file, we used this as volume

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/094xbhby98398u471nok.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwwo1u8068lpxw40jtk0.png)
 

```
volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro

```
`./nginx.conf:` means the local file called nginx.conf and we have sent all files from "/etc/nginx/conf.d/default.conf:ro" this to "./nginx.conf".

Also, we are using 2 containers here and used images "nginx:1.13" and "httpd". 

Now from CLI, if we compose this .yaml file, automatially the containers will run

```
docker-compose up
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk5z8djistzd5w2i1ogz.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/am5sahdf4pgwe97z3e58.png)

In the terminal, we can see

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iez972pcbukzybcy467.png)

Look carefully! web_1 & proxy_1 is mentioned. Remember, we did set the container names as web & proxy


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ei5pyj1tsf97vrrk7igc.png)
    
If you reload the localhost, automatically, the proxy_1 & web_1 will be added. 

Note: in the localhost, we actually have apache server (httpd) not the nginx server. 
The traffic is actually goring from nginx server (proxy) to apache(httpd) and apache is responding with its default index.html. So, everytime you refresh, you will see this

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgb4lge2eq86wa8epckf.png)
web_1 & proxy_1 to be added in the logs.  



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zt0haz66m2zqtkscgr9.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzqo539lwat4fwbqfos6.png)

EVerytime, it will hit the proxy first and then the back end web server apache (httpd) . 

Let's stop it using Ctrl+C



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8wmgqp9g2tsofwhu4ng.png)

We can run it again using

```
docker-compose up -d
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rc0xobwf3tptpowyfj8j.png)
 containers will run in the back ground and thus no logs will be there. YOu can check the localhost


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocwo77j6spd6c01ff3z9.png)
 To see the logs use the command

```
docker-compose logs
```
  
  
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq499qsskx0ogfijwmr6.png)


Let's see the containers which are running

```
docker-compose ps
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb0fg2lp85n87cyj7ks2.png)
We can use 

```
docker-compose top
```
To see all of the services running inside them.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o55j364wf0qgcnlzskvx.png)
 
Finally, to stop & clean the containers , use

```
docker-compose down
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqashqkmr5ejb8c3oka8.png)
Nothing out there now

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0aocx3f88v5tr2h8a5kl.png)