Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 11, 2022 01:09 pm GMT

Docker series (Part 14): Docker Compose

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, 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 .:/sitewhich 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/mysqlvolumes:  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

Let's use it and learn.

Let's use this compose-sample-2 folder from this repo

lets get into the docker-compose.yml file

Image description

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

Image description

Image description

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

Image description

In the terminal, we can see

Image description

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

Image description

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
web_1 & proxy_1 to be added in the logs.

Image description

Image description

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

We can run it again using

docker-compose up -d

Image description
containers will run in the back ground and thus no logs will be there. YOu can check the localhost

Image description
To see the logs use the command

docker-compose logs

Image description

Let's see the containers which are running

docker-compose ps

Image description
We can use

docker-compose top

To see all of the services running inside them.

Image description

Finally, to stop & clean the containers , use

docker-compose down

Image description
Nothing out there now

Image description


Original Link: https://dev.to/mitul3737/docker-series-part-14-docker-compose-2pcd

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To