Docker compose example

Introduction

Docker compose is used to run multiple containers. In this tutorial, Using Docker compose example for NGINX and MYSQL. I used Docker compose version “3”

In today’s digital era, Docker has become an essential tool for developers and DevOps professionals. With its ability to package and deploy applications easily, Docker has revolutionized the way software is managed and deployed. However, as applications grow more complex, managing multiple containers simultaneously can be challenging. This is where Docker Compose shines. This article will guide you through Docker Compose with a practical example, making it easier to manage and coordinate multiple containers within the same application.

“Build, Manage and Secure Your Apps Anywhere. Your Way.” Quote from docker!

Docker compose example

Creating Docker-compose file. All docker compose file is a YAML file. Now, let’s go create the docker-compose.yml file as below

[root@DevopsRoles ~]# mkdir docker-instance
[root@DevopsRoles ~]# cd docker-instance/
[root@DevopsRoles docker-instance]# vim docker-compose.yml

The content docker-compose file as below

version: '3'

services:
 WEB01:
   image: nginx:latest
   container_name: "web01"
   hostname: web01
   ports:
        - "8888:80"
        - "8443:443"
        - "2233:22"
   #deploy:
   # resources:
   # limits:
   # memory: 512M
   #mem_limit: 512m
   volumes:
     - ./data_web01:/mnt_nfs
   networks:
     - bridge
   restart: always

 DB01:
   image: mysql:latest
   container_name: "db01"
   hostname: db01
   ports:
        - "33306:3306"
        - "2234:22"
   command: --default-authentication-plugin=mysql_native_password
   environment:
      MYSQL_ROOT_PASSWORD: 123456789
      #MYSQL_USER=huupv
      #MYSQL_PASSWORD=passforhuupv
      #MYSQL_DATABASE=DevopsRolesDB
   #deploy:
   # resources:
   #    limits:
   #      memory: 512M
   volumes:
      - ./data_db01:/mnt_nfs
   networks:
      - bridge
   restart: always

volumes:
   web_mnt_nfs:
     driver: local
networks:
   bridge:
     driver: bridge
     ipam:
       config:
          - subnet: 192.168.12.0/24

Creating and starting a container

[root@DevopsRoles docker-instance]# docker-compose up -d

The screen output terminal:

Creating network "docker-instance_bridge" with driver "bridge"
Creating db01 ... done
Creating web01 ... done

The result, Docker compose example

Access browser Nginx server

docker compose example nginx

Checking running containers for nginx and mysql

docker compose example

Conclusion

Thought the article, you can use “Docker compose example” as above. Docker Compose is not just a convenient tool but also a key to simplifying and enhancing container management in any software project. Continue reading the article to gain a deeper understanding of how to use this tool in real-world scenarios! I hope will this your helpful. Thank you for reading theΒ DevopsRolesΒ page!

,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.