Showing posts with label compose. Show all posts
Showing posts with label compose. Show all posts

Tuesday, March 1, 2022

Docker Compose

 


Docker Compose
it is a microservice approach to create containers and manage containers for orchestration.
docker-compose is a tool for defining and running multi-container Docker applications. With docker-compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Using Compose is basically a three-step process:
1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
3. Run docker-compose up and Compose starts and runs your entire app.

Docker Compose commands:

docker-compose up -d

This will create a network, and create the containers as per defined in the docker-compose.yml file

docker-compose down

This will stop and remove the containers then it will remove the network. The containers stopped and removed are as of defined in the docker-compose.yml file.

docker-compose scale db=2

If for exam[le in the docker-compose.yml file, you specified a db container, using the scale command, it will create for you 2 db containers.


Install Docker compose file

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

Example 1:


version: '3.3' 
services: 
         db: 
                 image: mysql:5.7 
                 volumes: 
                   - db_data:/var/lib/mysql 
                 restart: always 
                 environment: 
                     MYSQL_ROOT_PASSWORD: somewordpress 
                     MYSQL_DATABASE: wordpress 
                     MYSQL_USER: wordpress 
                     MYSQL_PASSWORD: wordpress 

         wordpress: 
             depends_on: 
                     - db 
                         image: wordpress:latest 
                         ports:
                          - "80:80" 
                         restart: always 
                         environment: 
                             WORDPRESS_DB_HOST: db:3306 
                             WORDPRESS_DB_USER: wordpress 
                             WORDPRESS_DB_PASSWORD: wordpress 
                             WORDPRESS_DB_NAME: wordpress 
                        volumes: 
                          db_data: {}


docker-compose up -d
docker-compose down


Example 2
version: '3.3' 
services: 
     db: 
         image: ramansharma95/mysql 
     webapp: 
         image: ramansharma95/webapp 
         ports: 
             - "80:80"

Run the following command in mysql container:

docker exec -it <<containerid>> bash 
mysql -uroot -pwhizlabs
create database company;
use company;
create table employee ( name varchar(30), mobile varchar(30));
select * from employee;

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...