How To Build and Run an Application on Docker Compose?
How To Build and Run an Application on Docker Compose

Introduction

In this tutorial, you build a simple Python web application running on Docker Compose. The application uses the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts demonstrated here should be understandable even if you’re not familiar with it.

Prerequisites

Both, the Docker Engine and Docker Compose must already be installed. It is not required or needed to install Python or Redis, as both are provided by Docker images.

Setup

Define the dependencies for applications

  1. Create a directory for the project:

Create a file e.g. app.py in your project directory and paste this in:

Here, redis is the hostname of the redis container on the application’s network. We use the default port for Redis, 6379.

Create another file e.g. requirements.txt in your project directory and paste this in:

Dockerfile Creation

Write a file named Dockerfile that builds a Docker image. All the dependencies which the Python application requires, including Python itslef will contain in this Image.
Inside your project directory, create a new file named Dockerfile and paste the following:

In the above file:

  • It will let the Docker build an image starting with the Python 3.7 image.
  • Docker will set the working directory to /code.
  • It will then set environment variables used by the flask command.
  • It will then install gcc and other dependencies
  • Then it will copy requirements.txt and install the Python dependencies.
  • Also, it will add metadata to the image to describe that the container is listening on port 5000
  • And it will copy the current directory . in the project to the workdir . in the image.
  • Then finally, it will set the default command for the container to flask run.

Define Services in a Compose file

Create a file e.g. docker-compose.yml in your project directory and paste the following:

This Compose file will defines two services: web and redis.

From the current directory’s Dockerfile, the image will be used by the web service. It then binds the host machine and the container to the exposed port, 5000. In This example service uses the default port for the Flask web server, 5000.

Redis Service

The redis service will use a public Redis image pulled from the Docker Hub registry.

Build and run your app with Docker Compose

From your project directory, start your application by running docker-compose up.

Docker Compose pulls an Image of Redis, builds an image for the code, and starts the services you have defined. Here, the code is statically copied or deployed into the image at build time.

Open your browser and enter the URL “http://localhost:5000/” to see the application running.
If you’re using Docker Desktop for Windows, Docker natively on Linux, or Docker Desktop for Mac then the web app should be listening on port e.g. 5000 on your Docker daemon host. In your web browser open url http://localhost:5000 to find the message e.g. Hello World. If it doesn’t resolve, you can also try another url e.g. http://127.0.0.1:5000.
If you’re using Docker Machine on a Windows or Mac, use docker-machine ip MACHINE_VM to grep the IP address of your Docker host/machine. Then, open http://MACHINE_VM_IP:5000 in your web browser.
The below message will be shown in your browser:

3. Refresh the page

The number should be incremented:

4. Switch to the another terminal window, and type docker image ls to list local images.
Listing images at this point should return redis and web.

If you want to inspect images then run docker inspect .

5. Stop the application using the below steps:

  • Either by running docker-compose down within your project directory in the second terminal
  • Or by hitting CTRL+C in the original terminal where you started the app.

Related Posts