Menu

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.https://runnable.com/docker/python/dockerize-your-python-application


Some python example code I made to test with (imgtest.py).  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')
img.save('/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
 
ADD imgtest.py /
 
RUN pip install numpy
RUN pip install pillow
RUN mkdir /app
 
CMD ["python","./imgtest.py"]

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 https://stackoverflow.com/questions/28668180/cant-install-pip-packages-inside-a-docker-container-with-ubuntu).  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 10.252.252.252 --dns 10.253.253.253

Should look like that:

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

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

Comment

Blog Search

Comments

There are currently no blog comments.