Docker Compose

Docker Compose

Docker Compose is a tool you can use to define and share multi-container applications. This means you can run a project with multiple containers using a single source.

With Docker Compose, you can define the services, networks, and volumes needed for your application in a simple YAML file called docker-compose.yml

docker-compose.yml file provides a way to configure and start all the services that your application depends on with a single command.

Features of Docker Compose

  • Multi-Container Deployment: it facilitates with easily define and run applications with multiple containers using a single YAML file.

  • Service Isolation: Each service runs in its own container, with ensuring the isolation and reducing conflicts between services.

  • Simplified Configuration: It helps in centralizing all the configurations, including networking, volumes, and dependencies, in the docker-compose.yml file.

  • Scalability: It provides the effortlessly scaling of services up or down with a single command .

It is preferred to maintain your docker-compose.yml file in version control (e.g., Git) to track changes and collaborate with your team effectively.

mkdir docker-compose

cd docker- compose

vim docker-compose.yml

Version : we need to specify the version of the docker-compose tool

Services : It is the list of services that our application is going to use

  1. Web : It is used to launch out Nginx Web server with official image that is pulled from docker hub repository .The web service maps port 80 on the host to port 80 on the container.

  2. DB : Used to launch MYSQL database with official image pulled from docker hub repository .The DB service is exposed to 3306 port and the database service sets an environment variable for the MySQL root password.

    Running Docker-compose.yml File

    • Now that we have our docker-compose.yml file, we can run it.

    • To start the application, enter the following command.

        docker-compose up -d
      

Now all the services will start and our website will be ready to be used .

Now our Application is up and running with docker-compose file .

  • To stop the application, either press CTRL + C or

docker-compose stop

Useful commands :

  1. Inspect Container Processes and Ports: docker inspect <container_name>

  2. View Container Logs: docker logs <container_name>

  3. Stop and Start the Container:docker stop <container_name> docker start <container_name>

  4. Remove the Container:docker rm <container_name>

Thanks for visiting my blog , Happy Learning !!