31 May 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