Python for Algorithmic Trading (1)
Docker Container
This section goes into the details of what the docker technology can do in the context of Python deployment.
Building a Ubuntu and Python Docker Image
This dockerfile control the building procedure for the image itself. The Bash script in below will be installing three parts:
- Linux housekeeping
- Miniconda
- optional Python packages
Script installing Python and optional packages
#!/bin/bash
#
# Script to Install
# Linux System Tools and
# Basic Python Components
#
# Python for Algorithmic Trading
# (c) Dr. Yves J. Hilpisch
# The Python Quants GmbH
#
# GENERAL LINUX
apt-get update # updates the package index cache
apt-get upgrade -y # updates packages
# installs system tools
apt-get install -y bzip2 gcc git # system tools
apt-get install -y htop screen vim wget # system tools
apt-get upgrade -y bash # upgrades bash if necessary
apt-get clean # cleans up the package index cache
# INSTALL MINICONDA
# downloads Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O \
Miniconda.sh
bash Miniconda.sh -b # installs it
rm -rf Miniconda.sh # removes the installer
export PATH="/root/miniconda3/bin:$PATH" # prepends the new path
# INSTALL PYTHON LIBRARIES
conda install -y pandas # installs pandas
conda install -y ipython # installs IPython shell
# CUSTOMIZATION
cd /root/
wget http://hilpisch.com/.vimrc # Vim configuration
Dockerfile to build the image
FROM ubuntu:latest
# information about maintainer
MAINTAINER sean
# add the bash script
ADD install_miniconda.sh /
# change rights for the script
RUN chmod u+x /install_miniconda.sh
# run the bash script
RUN /install_miniconda.sh
# prepend the new path
ENV PATH /root/miniconda3/bin:$PATH
# execute IPython when container is run
CMD ["ipython"]
To do that, I’m going to create a folder on my computer(host)
git clone https://github.com/sikgyu/trading_algo.git
cd trading_algo
docker build -t pyalgo:basic .
# Docker images can be listed via docker images
docker images
You have built the image successfully. To run the image:
docker run -it pyalgo:basic
Exiting IPython will exit the container as well, since it is the only application running within the container. However, you can detach from a container via the following:
Ctrl+p –> Ctrl+q
you can attach the container by this commnad
docker attach $CONTAINER_ID