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.
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.
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:
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
...
}
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.
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