Data Science Dev

Design, develop, deploy

Docker on Ubuntu Tutorial

I experimented with Docker lately.  Here are a few commands, notes and issues I resolved while working on Ubuntu 16.04.  I'm assuming you have docker installed (sudo apt-get update && apt-get install docker-ce)

Tutorial on the web for "dockerizing" python applications.

Some python example code I made to test with (  This is the code I wanted to place in a docker image.  I basically makes a random image and saves it.

import numpy as np
from PIL import Image
import os
img = np.random.randint(0,255,size=(1024,768))
img = img.astype('float')
img = Image.fromarray(img)
img = img.convert('RGB')'/app/test.jpg')

Here is an example Dockerfile I used.  I saved as Dockerfile and placed in same folder as my python script.

FROM python:2
RUN pip install numpy
RUN pip install pillow
RUN mkdir /app
CMD ["python","./"]

To build the docker image I moved to the folder where both the Dockerfile and python script reside.  And then typed:
sudo docker build .  

When building, had to add the DNS to the following (from  I tried other methods mentioned, but none of them seemed to work except this one.  I also commented out the DockerOpts line in /etc/default/docker.

"For Ubuntu users

You need to add new DNS addresses in the docker config

sudo nano /lib/systemd/system/docker.service

Add the dns after ExecStar.

--dns --dns

Should look like that:

ExecStart=/usr/bin/dockerd -H fd:// --dns --dns

systemctl daemon-reload sudo service docker restart


After your build you should be able to list the docker images available using the following command:

sudo docker images

I wanted to run the docker image and make a container.  If I ran it with:
sudo docker run -it <image_id>
It would just execute, create the random image output in the container, and then, the container would disappear with no random image output persisted in storage.  Instead, I used a bind mount.  This bind mount, lets me connect a container's directory to a host's directory.  The resulting random image is placed on the docker host.
sudo docker run -d -it -v <host directory>:<container directory> <image_id>
So on my computer it would look something like:
sudo docker run -d -it -v /home/npropes/Desktop:/app  4a823423bca
The random image output should appear on my Desktop.

Additional commands below:

I can see the list of docker containers running or have run by using this command:

sudo docker ps -a
To stop docker containers use these commands:
sudo docker stop <container>
sudo docker kill <container>
To delete a docker container:
sudo docker rm <container>
To delete an docker image:
sudo docker rmi <image>

Go Back


Blog Search

Blog Archive