Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

Awesome-Compose: What It is and Why You Should Really Care About It

3 min read

With over 14,700 stars, 2,000 forks, awesome-compose is a popular Docker repository that contains a curated list of Docker Compose samples. It helps developers learn about Docker Compose by providing a starting point for how to integrate different services using a Compose file and to manage their deployment with Docker Compose. This project is hosted over Docker GitHub organization, Inc and publicly open for the community to contribute and submit their compose file.

Most of the Docker compose samples available today are intended for use in local development environments such as project setups, tinkering with software stacks, etc. Tested dozens of these sample apps last week on Docker Desktop for Mac and it worked like a charm.

Please Note: These samples are not recommended for production environments.

How Sample Apps are Categorized?

The setups provided currently in the awesome-compose repository fit mainly in two categories:

1. Application skeletons

It is useful for kicking off project development. You will find different application skeletons with multiple services already wired together and ready to be launched with docker-compose.

2. Open-source software stacks

These setups are intended mostly for personal/home use or simply for developers to get familiar with the awesome-compose sample apps within their local dev environment.

Here’s a quick glimpse of the awesome-compose repository:

Image41

Building a Sample visitor app using Node.js, Nginx and Redis

In this tutorial , I will show how to containerize a NodeJS web application with Redis database and Nginx as a reverse proxy in front of NodeJS app using Docker.

Let’s begin building a project directory structure as shown below:

Project structure

.
├── docker-compose.yml
├── nginx
│   ├── Dockerfile
│   └── nginx.conf
├── web
│   ├── Dockerfile
│   ├── package.json
│   └── server.js



Prerequisite:

– Install Docker Desktop for Mac

Most of the awesome-compose sample apps works on Docker Desktop. Said that, it should also work on Docker distributions on Linux and Windows too. Visit https://docs.docker.com/desktop/mac/install/ to setup Docker Desktop for Mac or Windows on your local system.

Image1

ℹ️ INFO

Docker Desktop comes with Docker compose installed by default, hence you don’t need to install it separately.

Step 1. Create a Docker compose file

Create an empty file with the below content and save it by name – “docker-compose.yml”


services:
  redis:
    image: 'redislabs/redismod'
    ports:
      - '6379:6379'
  web1:
    restart: on-failure
    build: ./web
    hostname: web1
    ports:
      - '81:5000'
  web2:
    restart: on-failure
    build: ./web
    hostname: web2
    ports:
      - '82:5000'
  nginx:
    build: ./nginx
    ports:
    - '80:80'
    depends_on:
    - web1
    - web2

The compose file defines an application with four services - redis, nginx, web1 and web2
When deploying the application, docker-compose maps port 80 of the web service container to port 80 of the host as specified in the file.

ℹ️ INFO

Redis runs on port 6379 by default. Make sure you don’t run another instance of Redis on your system or port 6379 on the host is not being used by another container, otherwise the port should be changed.

Step 2. Create an nginx directory and add the below files

File: nginx/nginx.conf


upstream loadbalancer {
  server web1:5000;
  server web2:5000;
}

server {
  listen 80;
  server_name localhost;
  location / {
    proxy_pass http://loadbalancer;
  }
}

It’s always best practice to pick up the specific Docker image version and tag it as shown below:

File: Dockerfile

FROM nginx:1.21.6
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf

Step 3. Create a web directory

File: web/Dockerfile

FROM node:14.17.3-alpine3.14

WORKDIR /usr/src/app

COPY ./package.json ./
RUN npm install
COPY ./server.js ./

CMD ["npm","start"]

File: web/package.json


  "name": "web",
  "version": "1.0.0",
  "description": "Running Node.js and Express.js on Docker",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.17.2",
    "redis": "3.1.2"
  },
  "author": "",
  "license": "MIT"
}

File: web/server.js

const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
  host: 'redis',
  port: 6379
});

app.get('/', function(req, res) {
    redisClient.get('numVisits', function(err, numVisits) {
        numVisitsToDisplay = parseInt(numVisits) + 1;
        if (isNaN(numVisitsToDisplay)) {
            numVisitsToDisplay = 1;
        }
        res.send('Number of visits is: ' + numVisitsToDisplay);
        numVisits++;
        redisClient.set('numVisits', numVisits);
    });
});

app.listen(5000, function() {
    console.log('Web application is listening on port 5000');
});

Step 4. Creating a web1 directory and add the below files:

File: Dockerfile

FROM node:14.17.3-alpine3.14

WORKDIR /usr/src/app

COPY ./package*.json ./
RUN npm install
COPY ./server.js ./

CMD ["npm","start"]

File: server.js

const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
  host: 'redis',
  port: 6379
});


app.get('/', function(req, res) {
    redisClient.get('numVisits', function(err, numVisits) {
        numVisitsToDisplay = parseInt(numVisits) + 1;
        if (isNaN(numVisitsToDisplay)) {
            numVisitsToDisplay = 1;
        }
        res.send('web1: Total number of visits is: ' + numVisitsToDisplay);
        numVisits++;
        redisClient.set('numVisits', numVisits);
    });
});

app.listen(5000, function() {
    console.log('Web app is listening on port 5000');
});

File: package.json

{
  "name": "web1",
  "version": "1.0.0",
  "description": "Running Node.js and Express.js on Docker",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.17.2",
    "redis": "3.1.2"
  },
  "author": "",
  "license": "MIT"
}

Step 5. Deploy the application

Let us deploy the full-fledged app using docker-compose

$ docker-compose up -d
Creating nginx-nodejs-redis_redis_1 ... done
Creating nginx-nodejs-redis_web1_1  ... done
Creating nginx-nodejs-redis_web2_1  ... done
Creating nginx-nodejs-redis_nginx_1 ... done

Expected result

Listing containers must show three containers running and the port mapping as below:

docker-compose ps
           Name                        Command              State           Ports         
------------------------------------------------------------------------------------------
nginx-nodejs-redis_nginx_1   /docker-entrypoint.sh ngin     Up      0.0.0.0:80->80/tcp    
                             ...                                                          
nginx-nodejs-redis_redis_1   docker-entrypoint.sh redis     Up      0.0.0.0:6379->6379/tcp
                             ...                                                          
nginx-nodejs-redis_web1_1    docker-entrypoint.sh npm       Up      0.0.0.0:81->5000/tcp  
                             start                                                        
nginx-nodejs-redis_web2_1    docker-entrypoint.sh npm       Up      0.0.0.0:82->5000/tcp  
                             start   

Step 6. Testing the app

After the application starts, navigate to http://localhost:80 in your web browser or run:

curl localhost:80
curl localhost:80
web1: Total number of visits is: 1
curl localhost:80
web1: Total number of visits is: 2
$ curl localhost:80
web2: Total number of visits is: 3
$ curl localhost:80
web2: Total number of visits is: 4

Stop and remove the containers

$ docker-compose down

Step 7. Monitoring Redis keys

If you want to monitor the Redis keys, you can use monitor command. Install redis-client in your Mac system using

brew install redis

and then directly connect to Redis container by issuing the below command:

% redis-cli
127.0.0.1:6379> monitor
OK
1646485507.290868 [0 172.24.0.2:34330] "get" "numVisits"
1646485507.309070 [0 172.24.0.2:34330] "set" "numVisits" "5"
1646485509.228084 [0 172.24.0.2:34330] "get" "numVisits"
1646485509.241762 [0 172.24.0.2:34330] "set" "numVisits" "6"
1646485509.619369 [0 172.24.0.4:52082] "get" "numVisits"
1646485509.629739 [0 172.24.0.4:52082] "set" "numVisits" "7"
1646485509.990926 [0 172.24.0.2:34330] "get" "numVisits"
1646485509.999947 [0 172.24.0.2:34330] "set" "numVisits" "8"
1646485510.270934 [0 172.24.0.4:52082] "get" "numVisits"
1646485510.286785 [0 172.24.0.4:52082] "set" "numVisits" "9"
1646485510.469613 [0 172.24.0.2:34330] "get" "numVisits"
1646485510.480849 [0 172.24.0.2:34330] "set" "numVisits" "10"
1646485510.622615 [0 172.24.0.4:52082] "get" "numVisits"
1646485510.632720 [0 172.24.0.4:52082] "set" "numVisits" "11"

Further References

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

47 Replies to “Awesome-Compose: What It is and Why You Should Really…”

  1. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  2. hello!,I love your writing so much! percentage we keep in touch more approximately your article on AOL? I need a specialist in this house to resolve my problem. May be that is you! Looking forward to peer you.

  3. Hi there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My blog looks weird when browsing from my iphone. I’m trying to find a template or plugin that might be able to correct this problem. If you have any recommendations, please share. Thanks!

  4. The subsequent time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to learn, however I truly thought youd have one thing interesting to say. All I hear is a bunch of whining about one thing that you possibly can fix if you werent too busy on the lookout for attention.

  5. My spouse and I absolutely love your blog and find a lot of your post’s to be what precisely I’m looking for. Would you offer guest writers to write content in your case? I wouldn’t mind writing a post or elaborating on a number of the subjects you write with regards to here. Again, awesome web log!

  6. There are definitely loads of particulars like that to take into consideration. That is a nice point to carry up. I provide the thoughts above as normal inspiration however clearly there are questions just like the one you convey up where the most important thing will likely be working in honest good faith. I don?t know if finest practices have emerged around issues like that, however I’m certain that your job is clearly recognized as a fair game. Each girls and boys feel the affect of just a moment’s pleasure, for the rest of their lives.

  7. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something informative to read?

  8. Howdy I am so grateful I found your weblog, I really found you by mistake, while I was researching on Yahoo for something else, Anyhow I am here now and would just like to say kudos for a remarkable post and a all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.

  9. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your weblog? My website is in the very same niche as yours and my users would certainly benefit from a lot of the information you present here. Please let me know if this alright with you. Thanks!

  10. Thank you for your entire hard work on this blog. Betty delights in setting aside time for internet research and it’s simple to grasp why. We know all concerning the lively method you make great secrets via this blog and as well as attract response from people on this situation then my princess is truly being taught so much. Have fun with the remaining portion of the new year. You’re doing a fantastic job.

  11. Thanks for any other informative website. Where else may just I am getting that kind of info written in such an ideal approach? I’ve a challenge that I am simply now running on, and I have been on the glance out for such info.

  12. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a extraordinary job!

  13. I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  14. Hello, Neat post. There’s an issue along with your website in internet explorer, could check thisK IE still is the market chief and a big element of other folks will pass over your great writing because of this problem.

  15. I am not positive the place you are getting your information, however good topic. I must spend some time studying more or understanding more. Thanks for excellent information I was in search of this info for my mission.

  16. Hello very nice website!! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds also?KI’m glad to find a lot of useful information right here within the publish, we need work out more strategies in this regard, thanks for sharing. . . . . .

  17. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  18. Youre so cool! I dont suppose Ive learn something like this before. So good to search out anyone with some authentic thoughts on this subject. realy thank you for starting this up. this web site is one thing that’s needed on the net, somebody with just a little originality. useful job for bringing one thing new to the internet!

  19. Thanks for sharing excellent informations. Your web-site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and simply could not come across. What a great web site.

  20. Hey! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me. Anyways, I’m definitely delighted I found it and I’ll be book-marking and checking back often!

  21. I’ve been surfing online greater than three hours as of late, but I never discovered any attention-grabbing article like yours. It’s beautiful value enough for me. In my view, if all webmasters and bloggers made just right content as you probably did, the internet can be a lot more helpful than ever before. “No nation was ever ruined by trade.” by Benjamin Franklin.

  22. Pretty nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!

  23. What¦s Going down i’m new to this, I stumbled upon this I have found It positively useful and it has aided me out loads. I’m hoping to give a contribution & assist different customers like its helped me. Great job.

Leave a Reply

Your email address will not be published.