[PYTHON] How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment

image.png

Introduction

Docker is one of the most popular containerization technologies. It's an easy-to-use, easy-to-use tool for developers, and has the advantage of being smoother and easier to use than other similar technologies. Since its first open source release in March 2013, Docker has been the focus of attention from developers and operations engineers. According to Docker Inc., Docker users have downloaded over 105 billion containers to Docker Hub and docked 5.8 million containers. This project has more than 32K stars on Github.

Since then, Docker has become mainstream, with over 100,000 third-party projects using this technology, increasing the demand for developers with containerization skills.

This blog post describes how to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment. Use the Python API as the main app.

MetricFire Free demo Reservation Find out how to monitor your Kubernetes, Python setup.

Development environment setup

Before you start, install some requirements. Here we use the mini Python API developed in Flask. Flask is a Python framework and is a great choice for quickly prototyping APIs. Our application is developed using Flask. If you are new to Python, here are the steps to create this API.

Start by creating a Python virtual environment and keeping your dependencies separate from other system dependencies. Before that, you need PIP, a popular Python package manager.

Installation is very easy. You need to run two commands:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

For reference, Python3 must be installed. You can check this by typing:

​python --version​

After installing PIP, use the following command to install the virtual environment.

​pip install virtualenv​

You can find other installation methods by following the official guide. Then create a project in the folder where you need to create a virtual environment and activate it. Also, create a folder for your app and a file called app.py.

mkdir app
cd app
python3 -m venv venv
. venv/bin/activate
mkdir code
cd code
touch app.py

Create a simple API to display the weather for a particular city. For example, suppose you want to view the weather in London. Must be requested using the route:

/london/uk

You need to use PIP to install Python dependencies called "flask" and "requests". We will use them later:

pip install flask requests​

The requirements file looks like this:

certifi==2019.9.11
chardet==3.0.4
Click==7.0
Flask==1.1.1
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
requests==2.22.0
urllib3==1.25.7
Werkzeug==0.16.0

This is the initial code for the API.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'App Works!'

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

To test, you need to run python app.py and go to http: //127.0.0.1:5000/. "AppWorks!" Is displayed. On a web page. Be sure to create an account on the same website and generate an API key as we will be using data from openweathermap.org.

Next, we need to add some useful code to the API to display the weather data for a particular city.

@app.route('/<string:city>/<string:country>/')
def weather_by_city(country, city):
    url = 'https://samples.openweathermap.org/data/2.5/weather'
    params = dict(
        q=city + "," + country,
        appid= API_KEY,
    )
    response = requests.get(url=url, params=params)
    data = response.json()
    return data

The overall code looks like this:

from flask import Flask
import requests

app = Flask(__name__)

API_KEY = "b6907d289e10d714a6e88b30761fae22"

@app.route('/')
def index():
    return 'App Works!'

@app.route('/<string:city>/<string:country>/')
def weather_by_city(country, city):

    url = 'https://samples.openweathermap.org/data/2.5/weather'
    params = dict(
        q=city + "," + country,
        appid= API_KEY,
    )

    response = requests.get(url=url, params=params)
    data = response.json()
    return data

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

127.0.0.1:5000/london/ukにアクセスすると、次のようなJSONが表示されるはずです。

{
  "base": "stations",
  "clouds": {
    "all": 90
  },
  "cod": 200,
  "coord": {
    "lat": 51.51,
    "lon": -0.13
  },
...

The mini API is working. Let's containerize using Docker.

Create an app container using Docker

Let's create a container for the API. The first step is to create a Dockerfile. A Dockerfile is a useful text file that contains various steps and instructions that the Docker daemon must follow to build an image. After building the image, you will be able to run the container.

The Dockerfile always starts with a FROM instruction.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD [ "python", "app.py" ]

In the above file, I did the following:

  1. Use the base image "python: 3"
  2. Also, set PYTHONUNBUFFERED to 1. Setting PYTHONUNBUFFERED to 1 allows log messages to be dumped to a stream instead of being buffered.
  3. Also create a folder / app and set it as workdir
  4. Copy the requirements and use it to install all dependencies
  5. Copy all the files that make up the application, the app.py file, to workdir
  6. Since the app uses this port, finally expose port 5000 and launch the python command with app.py as an argument. This will launch the API when the container starts.

After creating the Dockerfile, you need to build the Dockerfile with the image name and the tags you choose. In this case, use "weather" as the name and "v1" as the tag.

docker build -t weather:v1 .

Make sure you are building from within the folder that contains the Dockerfile and app.py files.

After building the container, you can run it using:

docker run -dit --rm -p 5000:5000 --name weather weather:v1

The container runs in the background because it uses the -d option. The container is called "weather" (-name weather). We have mapped host port 5000 to exposed container port 5000, so port 5000 is also reachable.

If you want to confirm the creation of the container, you can use:

docker ps

You should see an output that is very similar to the following output.

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
0e659e41d475        weather:v1          "python app.py"     About a minute ago   Up About a minute   0.0.0.0:5000->5000/tcp   weather

You can now query the API. Let's test using CURL.

curl http://0.0.0.0:5000/london/uk/

If the last command needs to return JSON:

{
  "base": "stations",
  "clouds": {
    "all": 90
  },
  "cod": 200,
  "coord": {
    "lat": 51.51,
    "lon": -0.13
...
}

Using Docker Compose for development

Docker Compose is a Docker Inc for defining and running multi-container Docker applications. An open source tool developed by. Docker Compose is also a tool intended for use in development environments. This allows you to automatically reload the container as your code updates, without having to manually restart the container or rebuild the image with every change. It would be frustrating without Compose, which develops using only Docker containers.

The implementation uses the "docker-compose.yaml" file.

This is the "docker-compose.yaml" file used by the API.

version: '3.6'
services:
  weather:
    image: weather:v1
    ports:
      - "5000:5000"
    volumes:
      - .:/app

In the above file you can see that we have configured the service "weather" to use the image "weather: v1". Map host port 5000 to container port 5000 and mount the current folder in the "/ app" folder inside the container.

You can also use a Dockerfile instead of an image. I already have a Dockerfile, so I recommend this in this case.

version: '3.6'
services:
  weather:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app

Now, run "docker-compose up" to start running the service, or run "docker-compose up --build" to build and then run.

Summary

In this post, I saw how to create a Docker container for the mini Python API and used Docker Compose to create a development environment. If you're using another programming language like Go or Rails, you usually follow the same steps, with a few minor differences. MetricFire Free demo Reservation MetricFire](https://www.metricfire.com/japan/?utm_source=blog&utm_medium=Qiita&utm_campaign=Japan&utm_content=Develop%20and%20Deploy%20a%20Python%20API%20with%20Kubernetes%20and%20Docker) is suitable for the monitoring environment. Please check if.

Recommended Posts

How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
How to run a Django application on a Docker container (development and production environment)
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Put Jupyter and Docker Compose on your Chromebook and use it as a light development environment!
How to use pyenv and pyenv-virtualenv in your own way
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
How to use jupyter notebook without polluting your environment with Docker
Learn how to use Docker through building a Django + MySQL environment
How to use is and == in Python
Use WebDAV in a Portable Docker environment
How to make a container name a subdomain and make it accessible in Docker
[Note] How to create a Ruby development environment
[Note] How to create a Mac development environment
Use a free GPU in your favorite environment
How to run AutoGluon in Google Colab GPU environment
A memorandum on how to use keras.preprocessing.image in Keras
How to build a Django (python) environment on docker
How to use template engine in pyramid 1 file application
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to use jupyter lab in Windows 10 local environment
How to set up and compile your Cython environment
[Linux] How to put your IP in a variable
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Build a Docker environment that can use PyTorch and JupyterLab
Pros and cons of converting Django's development environment to Docker
How to use the __call__ method in a Python class
How to use VS Code in venv environment on windows
How to create and use static / dynamic libraries in C
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
Use mitmproxy to force your app's API into your development environment
How to build a python2.7 series development environment with Vagrant
How to build a LAMP environment using Vagrant and VirtulBox Note
How to install OpenCV on Cloud9 and run it in Python
Run eclipse in Docker environment (noVNC)
How to compare lists and retrieve common elements in a list
How to use classes in Theano
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
How to debug Dash (Flask) in Docker + VSCode + remote connection environment
How to use functions in separate files Perl and Python versions
How to use Serverless Framework & Python environment variables and manage stages
How to use .bash_profile and .bashrc
How to install and use Graphviz
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
I want to create a pipfile and reflect it in docker
How to delete a Docker container
How to run TensorFlow 1.0 code in 2.0
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
Create a simple Python development environment with VS Code and Docker
How to log in to Docker + NGINX
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
[Python] How to save the installed package and install it in a new environment at once Mac environment
Create a shortcut to run a Python file in VScode on your terminal
I wanted to use jupyter notebook with docker in pip environment (opticspy)
How to get a specific column name and index name in pandas DataFrame
How to install python package in local environment as a general user
How to use PyCharm with Glue development endpoints running inside a VPC