|
||||||||||||||
Dockerize your Python Application
Время создания: 16.01.2018 11:16
Автор: br0ke
Текстовые метки: docker, python, container
Раздел: Информационные технологии - Linux - Docker
Запись: and-semakin/mytetra_data/master/base/15160833715hngge313x/text.html на raw.githubusercontent.com
|
||||||||||||||
|
||||||||||||||
Dockerize your Python Application Dockerfiles enable you to create your own images. A Dockerfile describes the software that makes up an image. Dockerfiles contain a set of instructions that specify what environment to use and which commands to run. First, start with a fresh empty directory. In our example, we call this my_new_docker_build – but feel free to use whatever name you like. This directory defines the context of your build, meaning it contains all of the things you need to build your image. Create a new text file in my_new_docker_build called Dockerfile (note no extension; on Windows, you may need to save the file as “All types” and put the filename in quotes to avoid automatically appending an extension); use whatever text file editor you already know (you might use Sublime, Notepad++, emacs, nano, or even vi). In our example, we use the basic Python 3 image as our launching point. Add the following line to your Dockerfile: FROM python:3
We want to run a basic Python script which we’ll call my_script.py. First, we need to add the script to the Dockerfile: ADD my_script.py /
Our script depends on the Python pyStrich library (pyStrich generates 1D and 2D barcodes), so we need to make sure we install that before we run my_script.py! Add this line to your Dockerfile to install random: RUN pip install pystrich
Add this line to your Dockerfile to execute the script: CMD [ "python", "./my_script.py" ]
Your Dockerfile should look like this: FROM python:3
ADD my_script.py /
RUN pip install pystrich
CMD [ "python", "./my_script.py" ]
The Python script my_script.py looks like the following: # Sample taken from pyStrich GitHub repository
# https://github.com/mmulqueen/pyStrich
from pystrich.datamatrix import DataMatrixEncoder
encoder = DataMatrixEncoder('This is a DataMatrix.')
encoder.save('./datamatrix_test.png')
print(encoder.get_ascii())
Now you are ready to build an image from this Dockerfile. Run: docker build -t python-barcode .
After your image has been built successfully, you can run it as a container. In your terminal, run the command docker images to view your images. You should see an entry for “python-barcode”. Run the new image by entering: docker run python-barcode
You should see what looks like a large ASCII QR code. If you only need to run a simple script (with a single file), you can avoid writing a complete Dockerfile. In the examples below, assume you store my_script.py in /usr/src/widget_app/, and you want to name the container my-first-python-script: docker run -it --rm --name my-first-python-script -v "$PWD":/usr/src/widget_app python:3 python my_script.py
docker run -it --rm --name my-first-python-script -v "$PWD":/usr/src/widget_app python:2 python my_script.py
Make sure you do not append an extension to the Dockerfile (i.e., Docker does not recognize Dockerfile.txt). You do not have to read the contents of every Dockerfile you base yours on, but make sure to at least familiarize yourself with them; you can avoid trying to install redundant software (e.g., installing pip when the Python image already loads it), and you can make sure you write your RUN commands appropriately. Docker Hub does not enforce basing all images off only one distribution of Linux; if you use a Debian-based distribution (Debian, Ubuntu, Mint, etc.) you need to call apt-get to install software, and if you use a Red Hat-based distribution (Red Hat Enterprise Linux/RHEL, CentOS) you need to use yum. Gaining familiarity early prevents redoing your work and saves time. You might end up starting with an unfamiliar base image (i.e., if you primarily use CentOS and want to run a Python installation, the Python image extends Debian Jessie, so you need to use caution in how you write your RUN directives). If you maintain familiarity with Ubuntu, using Debian does not offer too many challenges (Ubuntu came from an offshoot of Debian Linux). Avoid putting any unused files in your build directory. Docker makes tarballs of everything in the current directory and sends that to the Docker daemon, so if you have unnecessary files, those are included. Do not attempt to run a script requiring dependencies using the Alternative method, unless those dependencies come with the bare Python installation. Run the following command from your docker console to see a list of your containers: docker ps
# OR #
docker ps -a # to see all containers, including those not running
Note: Removing a Container is FINAL.
To delete all your containers, run: $ docker ps -q -a | xargs docker rm
For example: $ docker rmi 60959f29de3a
This requires a little bit of Linux magic (like deleting all containers above). Docker marks images without tags with "<none>" so we need to process only those images. Run the following command from your terminal (the awk programming language gives you text manipulation tools): docker rmi $(docker images | grep "<none>" | awk '{print $3}')
To delete all of your images, you can simplify the command above: docker rmi $(docker images | awk '{print $3}')
|
||||||||||||||
Так же в этом разделе:
|
||||||||||||||
|
||||||||||||||
|