Introduction to Docker Compose - CNDRO.LLC
3221
post-template-default,single,single-post,postid-3221,single-format-standard,wp-custom-logo,theme-bridge,bridge-core-2.9.4,woocommerce-no-js,tribe-no-js,ehf-template-bridge,ehf-stylesheet-bridge-child,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,columns-4,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

Introduction to Docker Compose

In this tutorial, we will discuss what Docker Compose is all about. In one of our previous posts, we mentioned that Docker is a containerization platform developed to help package applications and their dependencies in the form of containers. This allows these applications to work effortlessly in any environment, either development, test, or Production. We also stated that Docker was introduced to tackle many problems developers face on staging servers and development.
Here now, we will discuss how Docker Compose is majorly used. Docker compose helps to facilitate multiple services running simultaneously. We run multiple containers as a single service here, and each of these containers runs in isolation but communicate with each other when required.

Installation

Firstly, we need to install Docker Compose on our machine. To install Docker Compose on Linux, Windows, or Mac, you can visit this link.

After installing it, verify with the command docker-compose –version on your terminal.

How to Create a Compose File

Hello World Docker Compose

Let’s look at this basic example of  creating Hello World with Docker Compose. In the code below, we created a container from the ubuntu image, whereby the “Hello World” service is initialized from the ubuntu image, which will print “Hello World” when run.

1 version: '2'
2 services:
3  hello_world:
4   image: ubuntu
5   command: [/bin/echo, 'Hello world']

We use docker-compose up to run the code;

Let’s try out another example of how much further we can use the Docker Compose for our application.

Using Redis in Flask App

We use Redis to handle our requests in this application, whereby we pass our data via Redis. What we have below is our requirements.txt file which is flask and Redis.

Requirements.txt

Our Flask App

Here is the code to our flask app ,we’re using the POST and GET method. The POST method was used to send data which is in form of {‘name’: your_input} via the Redis as a collection of string keys and values.

Whenever the GET method endpoint is called, we can get our items, which will be returned via the redis.lrange().

 


from flask import Flask, request, jsonify
from redis import Redis

app = Flask(__name__)
redis = Redis(host="redis", db=0, socket_timeout=5, charset="utf-8", decode_responses=True)

@app.route('/', methods=['POST', 'GET'])
def index():

    if request.method == 'POST':
        name = request.json['name']
        redis.rpush('customers', {'name': name})
        return jsonify({'name': name})

    if request.method == 'GET':
        return jsonify(redis.lrange('customers', 0, -1))

The DockerFile

This is our DockerFile which contains series of instructions on how we want our image to be created.

 


FROM python:3.7.0-alpine3.8

COPY requirements.txt /app/


WORKDIR /app

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

ENV FLASK_APP=app.py

CMD flask run --host=0.0.0.0

Docker Compose File

Under our docker compose file, we tell docker how to build our image and we specify Flask development environment. So whenever we run the docker-compose up, our flask app will run based on the port we specified there as well.

version: "3"

services:
  app:
    build: .
    image: flask-redis:1.0
    environment:
      - FLASK_ENV=development
    ports:
      - 5000:5000

  redis:
    image: redis:4.0.11-alpine

Running Docker Compose File

We use the docker-compose up in our terminal to run our compose file, then we have our application running as seen below. We can move further to test with Postman.

Project structure

Your project structure should look like this

No Comments

Post A Comment