Dockerization¶
Build/Store Docker images¶
Ideally, the source code of your LT tool/service already resides on GitLab where a built-in Continuous Integration (CI) Runner can take care of building the image; GitLab also offers a container registry that can be used for storing the built image. For this, you need to add on the root level of your GitLab repository a .gitlab-ci.yml
file as well as a Dockerfile
; i.e, the recipe for building the image. Here you can find an example. After each new commit, the CI Runner is automatically triggered and runs the CI pipeline that is defined in .gitlab-ci.yml
.
You can see the progress of the pipeline on the respective page in GitLab UI (“CI / CD -> Jobs”); also when it completes successfully, you can find the image at “Packages -> Container Registry”.
Your image can also be built and tagged in your machine by running the docker build
command. Then it can be uploaded (with docker push
) to GitLab registry, in
DockerHub (which is a public Docker registry) or any other Docker registry.
E.g for this GitLab hosted project the commands would be:
docker login registry.gitlab.com
For logging in and be allowed to push an image.
docker build -t registry.gitlab.com/european-language-grid/dfki/elg-jtok
For building an image (locally) for the project. Before running docker build
you have to download (clone) a copy
of the project and be in the top-level directory (elg-jtok
).
docker push registry.gitlab.com/european-language-grid/dfki/elg-jtok
For pushing the image to GitLab.
In the following links you can find some more inforrmation on docker commands plus some examples:
Dockerization of a Python-based LT service/tool¶
From Python script to Service¶
First you need to ensure that your python script provides either a std i/o messaging or a RESTful API.
- A ) create a REST API using Flask
Example of a Shakespeare Bot
from flask import Flask from flask import render_template from flask import request # creates a Flask application, named app app = Flask(__name__) from chatterbot import ChatBot # a route where we will display a welcome message via an HTML template @app.route("/", methods=['POST','GET']) def hello(): if request.method == 'POST': # this block is only entered when the form is submitted user_message = request.form['user_message'] chatbot = ChatBot("Frank") bot_message = chatbot.get_response(user_message).text data = { 'bot_message': bot_message, 'user_message': user_message, 'user_message_visibility': '', } return render_template('index.html', **data) data = { 'bot_message': "Speak. I am bound to hear.", 'user_message': '', 'user_message_visibility': 'style=visibility:hidden;', } return render_template('index.html', **data) # run the application if __name__ == "__main__": app.run(debug=True)
- B ) create a std i/o messaging
- Example to be added soon.
From Python Service to Docker image¶
Then you can make your service a Docker image by taking the following steps:
- choose python environment, e.g. python:3.6.4-slim-jessie
- add/copy python scripts
- add/copy other resources
- install missing modules
- define entrypoint
We provide you with some dockerfile examples to see its simplicity:
Example 1: Shakespeare Bot
from python:3.6.4-slim-jessie
COPY shakespearebot.py .
COPY corpora/hamlet.csv corpora/
RUN pip install pandas
RUN pip install chatterbot
RUN pip install chatterbot-corpus
ENTRYPOINT ["python","shakespearebot.py"]
Example 2: Legal Entity Recognition, install all requirements from .txt-file
FROM python:3.7
COPY . .
RUN pip install -r requirements.txt
EXPOSE 8080
ENTRYPOINT ["python", "ler-ws.py"]