How to build a simple docker container using docker-compose
Introduction
Docker is a platform for building, shipping, and running distributed applications. It allows developers to package an application and its dependencies into a single container, which can be run on any Linux or Windows machine. Docker-compose is a tool for defining and running multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a single file, and then start and stop them all with a single command. The use of Docker and Docker-compose has become increasingly important in the IT industry due to their ability to simplify the process of deploying and scaling applications, as well as improve the consistency and reliability of these deployments.
This tutorial is useful just for accelerating hypothesis demonstrations to keep consistency in the conclusions. Always execute the Docker file, you are going to get the same results each time contrary if you use your own local or spent a lot of time decorating a remote environment on each execution.
Hands-On
This post explains how to run a simple Flask application in Docker and orchestrate it with Docker-Compose. A Flask application code is provided that takes a string input and returns the input string reversed. A Dockerfile is also provided that defines a Docker image to run the application. Finally, a docker-compose.yml file is provided that defines a Docker service called “tf_lovecraft_service” that builds an image from the current directory and maps port 8080 of the container to the host machine. At the end, it shows how to build and run the Docker container.
Flask App
The next code section is the Python code for the Flask application that takes a string input and returns the input string reversed. It defines a route “/\<random_string>” that takes in a string input “random_string”. The return statement uses the reversed()
function to reverse the string and then concatenates the characters back together using the join()
function. The if __name__ == '__main__':
statement ensures that the app.run()
function is only executed when the script is run directly (as opposed to being imported as a module). The app.run()
function starts up the Flask application and specifies the host and port to use.
""""Main application to test my points of vieww"""
from flask import Flask
app = Flask(__name__)
@app.route('/<random_string>')
def returnBackwardsString(random_string):
return "".join(reversed(random_string))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Dockerfile
The next section provides the Dockerfile code necessary to build a Docker image for the Flask app. It begins with the FROM command, which specifies the base image to use (in this case, python:3). It then sets the working directory for the application and copies the requirements.txt file to the working directory. The RUN command installs the necessary packages listed in requirements.txt. Finally, the Dockerfile copies the app.py file to the working directory and exposes port 8080, which is the port the Flask app is configured to use. The CMD command specifies the command to run when the container starts, which is to run the app.py file using Python.
# Docker image for run the lovecraft service.
FROM python:3
WORKDIR /usr/src/app
COPY ./requirements.txt ./
RUN pip3 install --no-cache -r requirements.txt
COPY ./app.py ./do
EXPOSE 8080
CMD python app.py
docker-compose.yml
The following docker-compose.yml
file is used to define a Docker service called tf_lovecraft_service
. It is using version 3 of the Docker Compose file format. The service is defined to build an image from the current directory (specified as .
). It also maps port 8080
from the container to the host machine. Once the service is defined in the docker-compose.yml
file, it can be started by running the command docker-compose up
.
version: '3'
services:
tf_lovecraft_service:
build: .
ports:
- 8080:8080
When you ‘run’ the container, docker-compose will build the image and run it then.
docker-compose up
Conclusion
— — — — — — -
Surely this is the most obvious and simple example to use docker-compose, but, as time goes on, the practice of using this model to test demos will push you to more sophisticated examples and tools.
Thanks for your time.