Docker is the most popular containerization tool in the DevOps world. But, What is Docker Compose? Docker Compose is used to run applications which have multiple containers using a YAML file. There can be several cases where the docker application must run multiple containers for different technology stack. Now building, running, connecting separate dockerfiles for each container can be a difficult task; this is where docker-compose helps you out. Using a single and straightforward docker-compose.yml file, you can build, connect, and launch all the containers by running a single command. This is very useful for enterprise applications in production, where several applications run inside containers. It saves a lot of time by running 100s of application inside docker containers with ease.

Installing Docker Compose

Docker should already be installed on your system before installing compose. Run the command below to install docker-compose. Run the command below to set file permissions. Check if it got installed correctly, it should return the docker-compose version. Below is the list of commands you can use with docker-compose.

Docker Compose File

Here is a sample docker-compose file which does all the magic. The first line in this file specifies the version being used. This number depends on the Docker engine installed on your system. I have Docker 18.09.6 installed which belongs to version 3 of docker-compose. Check out more details on versions here – https://docs.docker.com/compose/compose-file/compose-versioning/ This docker file is running two services/applications, web and Redis. The web service is built through a dockerfile and runs at default flask webserver port – 5000. The Redis service runs by pulling the Redis image from docker hub registry. To execute the docker-compose.yml file, you need to run a very simple command: docker-compose up

Docker Compose Workflow

Below are the three steps to use docker-compose. Let me take an example of the sample docker-compose file we saw earlier and show you how the project structure will be created. my-app is my main project directory. This directory has web and Redis service directories and the docker-compose YAML file. Dockerfile of Web service is present in a web directory. Since Redis service is being pulled from docker hub directly, no dockerfile is required in Redis directory. This is how docker-compose workflow looks like.

Containerizing A MEAN Stack Application using Docker Compose

Now that you have understood fundamental concepts. Let me show a demo on how to containerize a MEAN stack application using docker-compose. MEAN stands for MongoDB, Express, Angular and Node.js. Applications using these services together is also called a MEAN/Full-stack application. For this demo, we will run three docker containers:

Container 1 – Angular Container 2 – NodeJS and ExpressJS Container 3 – MongoDB

Download the complete application here: http://bit.ly/2St7r3A (not tested for production) This is how my docker-compose.yml file will look like to run these three containers:

The first line specifies the docker-compose version being used We are running three services – angular, express and database Angular service will be built using a dockerfile. It will run on port 4200, and the application volume is /var/www/app Express service will be built using a dockerfile. The express server will run at port 3000, and the volume is /var/www/app Database service will pull MongoDB image from dockerhub and start it at post 27017

Extract the project in the home directory and go to the angular-client directory. Run the command below if you don’t have node package manager (npm) installed on your system (ignore if it’s installed). In angular-client directory and run npm install. Now go to express directory and run npm install. Now that everything is set, its time to run docker-compose.yml file which is going to launch all the docker containers and run the MEAN stack application. Go to your browser and check https://localhost:4200, your application will be up and running.

Go to https://localhost:3000 to check if an express server is running.

Also, you can run docker images command to see which all images are present in docker. Run the command below to see the containers running inside docker. So now, all three docker containers are up and running. Docker-compose took care of running all the containers with ease. This was a simple example for running three containers; you can imagine how useful it will be if the applications have to be launched on 100s of containers. Go ahead and try out to see how it works.

How to Install Docker Compose and Setup  - 43How to Install Docker Compose and Setup  - 38