Packaging the audio transcoder

The next step is to put the audio encoding server in a container. Unlike the previous containers that have been used, this requires us to build a custom image. To build an image, its specifications must be declared in a file. A specification file contains a list of statements to execute so that files are added to the image. Here is the specification file of the encoding server:

FROM python:3.6

RUN apt-get update && apt-get install -y sox libsox-fmt-mp3

ENV config /opt/audio-encoder/config.json

WORKDIR /tmp/audio-encode-server
COPY ./ /tmp/audio-encode-server
RUN python setup.py install

CMD ["sh", "-c", "/usr/local/bin/audio-encode-server --config ${config}"]

The first line indicates that this image inherits from the python:3.6 image. This image is based on the Debian distribution. Other Python images are available, based on Alpine. Alpine is a lightweight distribution, smaller than the Debian one. The FROM statement must always be the first one in a Docker file. See later for more details on how images are composed together.

The second statement adds the sox packages necessary for the audio transcoder.

Then, the ENV statement sets an environment variable for the configuration file. This variable is used later to specify the configuration file to use, and can be overloaded when the container is started. Using an environment variable is not strictly needed here because the configuration file can also be specified with a file binding.

The next block copies the Python package from the host to the image. First, the WORKDIR statement sets the current working directory inside the image being built. The COPY statement copies the content of the current directory on the host to /tmp/audio_encode-server on the image. Then, the RUN statement installs the package on the image via the setup tools.

Finally, a default command is configured to start the daemon when the container is started.

This configuration file should be saved in the source directory of the audio encoder, with the name Dockerfile. This name is the default one used to build an image. This file is available in the GitHub repository of this book (https://github.com/PacktPublishing/Hands-On-Reactive-Programming-with-Python).

Building the image is done with the docker build command:

$ docker build . -t audio-encoder

The dot (.) parameter indicates that the build is done from the current directory. The t parameter is the name of the image being generated.

From that point, the image is available locally:

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
audio-encoder latest fc8f6e605ef5 4 minutes ago 988MB

Note how big the image is. This is because it is based on Debian. If an application depends only on pure Python code, or on binary distributions of Python packages, then the use of the Alpine distribution allows us to create images of only a few megabytes instead of almost one gigabyte.

This container can be started manually in a similar way to Minio:

docker run -d -p 8000:8000 --name audio-encoder 
-v /home/alice/config.json:/opt/audio-encoder/config.json
audio-encoder

This command starts the container with port 8000 exposed. This should match the one declared in the configuration file. The previous chapters used port 8080, but it is now used by the Traefik web interface. Here, a file binding is declared so that the configuration file is located on the host, but visible to the container.

All the required Docker images are now available to complete the definition of the application stack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.204.5