Building an Ubuntu VM to host Docker and Building Containers for Python Scripts Launch VM

The first part of these instructions are generic to any vm launch (unless you use a preconfigured image)
Go to the cloud service of your choice (here Microsoft Azure) and launch a VM of a size of your choosing (nothing too tiny, probably 8GB RAM minimum if doing machine learning), running Ubuntu Server (or similar). I recommend using a username/password authentication over ssh keys, as it’s easier.
Under networking settings for the VM, under “Inbound Ports Rules”
Open (that is, Allow) port 22 (for SSH) and port 3389 (for Remoted Desktop Access) as destination ports. During VM launch you may also see a setting to open ports for RDP and SSH, which you can select then, but you’ll want to edit it now:
Protocol is TCP, Any source port, however source should be limited to select IP addresses, those of the organization:
Currently USV addresses are
x.x.x.x,z.z.z.z
These might change. Also, you can tack on your own for use from home, although using the VPN is the recommend option

Configure VM

Some of the problems that will result here may be due to using the locked-down nature of USV-controlled, and corporate computers in general. You can log in using a personal computer which may help, just make sure to add your current IP address to the allowed list.
You can’t yet remote into this desktop because there’s no RDP client or desktop environment installed on Ubuntu server.
So you’ll need to ssh in using command line. I use Anaconda Prompt because it works better than Command Prompt on the USV-controlled computers.
Regardless of OS you are using, this should work on the command line:
ssh <Login username>@<IP.Address of VM>
Azure actually has a nice ‘connect’ button which should give you the necessary ssh command filled in with username and IP.address already.

And you should now be greeted by a standard Linux command line.

Here, you will enter the following line by line, talking to the VM from your command line:

sudo apt-get update
sudo apt-get install xfce4
sudo apt-get install xrdp
sudo systemctl enable xrdp
echo xfce4-session >~/.xsession
sudo service xrdp restart

This updates the system, installs a desktop (here xfce4) so you can see a screen and makes that desktop the default. I have lxde desktop, you can also use the familiar and much prettier gnome desktop.
To use another desktop, you can replace install xfce4 with install <your favorite desktop> and then do not run the echo xfce4-session >~/.xsession line. That’s the line which makes xfce4 the default desktop.
Since there should be no existing desktop, you don’t strictly speaking need that line, so leave it out if you are going custom.
The other thing these lines do is setup xdrp, the software you need to remote into the desktop.

RDP should now work, and you can login and use the GUI to configure as you would normally.

I won’t explain how to RDP into the VM, but the easiest way is to go to Azure and click ‘connect’ go to the RDP tab, and download the RDP file to open with RDP on your computer.
Strictly speaking you could just use command line with ssh to install docker, build containers, and schedule them with cron. But RDP is friendlier, I think.

Install Docker

The following is copied from Docker’s Official Install Guide for Ubuntu. Go there for help. In fact, go there and do what they say, not what I say. But just in case:

sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce -y

Run below to test Docker Installation

sudo docker run hello-world

All of which is run on the command line interface of the VM.

Container Building and Compilation for Python Use

To build a container, you need Docker (or another software, but as of writing that’s the global standard).
There’s a lot to what you can do with docker, but often it will look something like this:

Make a new empty folder where you are planning to work

Write a dockerfile (note there is no extension (no .txt), just ‘Dockerfile’)

Here’s an example. It ain’t elegant but it’s easy to follow: Note python:3.6-slim will eventually not be the cutting edge, you’ll want to check what you want, that works with the latest packages as are installed.

FROM python:3.6.8-slim
WORKDIR /app
COPY . /app
RUN apt-get -y update  && apt-get install -y \
  python3-dev \
  apt-utils \
  python-dev \
  build-essential \
&& rm -rf /var/lib/apt/lists/*
# not a pretty as a requirements.txt, but quick to follow
RUN pip install --upgrade setuptools
RUN pip install cython
RUN pip install numpy
RUN pip install scipy
RUN pip install matplotlib
RUN pip install pystan
RUN pip install fbprophet
RUN pip install azure
RUN pip install scikit-learn
# add additional packages here with the same pip syntax as above, as needed
CMD ["python"]

When this container runs, it is designed to be told to .py script as part of the run command, as you’ll see.
Optionally, use this instead if you just want to run just one .py script with the container:
CMD ["python", "your_script.py"]
and then you can drop the ‘python your_script.py’ argument from the docker run ... command below.
FYI: A docker file can run as large as 2 GB built in this way. Of course,
some time and expertise will let you bring it down to 300 MB in some cases, or even less…

Place your .py scripts in the container’s folder, if you haven’t done so already

Build the Dockerfile…

  1. Navigate to the directory in your command linecd ./Some_Folder/Container_Folder/
  2. Run the following to build (compile, etc) your container. Where –tag= specifies your container namesudo docker build --tag=testpython2 .
  3. Run the following to run the containersudo docker run testpython2 python your_script.py

Scheduling with CRON

In the Linux command line of the VM:

sudo crontab -e

Do you need sudo (granting root/administration access)?
If you can run ‘docker run xxx’ without sudo, then you can run crontab without sudo.
That is just crontab -e. If in doubt, probably include sudo to make sure it runs.
This will open up an old-fashioned command line text editor. At the top, or anywhere really, enter a command like this:

15 2 * * * docker run testpython2 python your_script.py

This command runs your_script out of the Docker container every day at 2:15 am. Google cron syntax if you don’t know it. Basically it’s two parts, the five numbers _ _ _ _ _ for minute, hour, day, month, day of week, then followed by your command. Use asterix for “everyday” or “every minute” etc.
You can also schedule the entire VM using, in Azure’s case, Azure Automation, because if you don’t need it running 24/7, why pay for it?
 

Leave a Comment

Your email address will not be published. Required fields are marked *