Monday, June 22, 2015

Create image for a Python test program in Docker

You have a Python application and you want to be able to containerize it. Let's use a very simple example:
Imagine we have a main.py file in the current working directory:
print "Hello world!"
To create an image out of this, let's create the following Dockerfile in the same folder:
FROM ubuntu:saucy

# Install required packages
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install python

# Add our python app code to the image
RUN mkdir -p /app
ADD . /app
WORKDIR /app

# Set the default command to execute
CMD ["python", "main.py"]
A quick explanation of each step:
  • We define ubuntu:saucy as our base image
  • Then, we install the python package by running apt-get install
  • After that, we create a folder called /app in our image and we copy the contents of the current directory to that folder (which will contain our main.py script)
  • We set the image working directory to /app so it knows where to look for files later on.
  • We define python main.py as the default command to launch our image
Now we can build it by running:
docker build -t username/python-hello-world .
This will trigger a build of the Dockerfile located at . (current folder) and will create an image called username/python-hello-world as the output:
Uploading context 4.608 kB
Uploading context
Step 0 : FROM ubuntu:saucy
 ---> 25593492b938
Step 1 : RUN apt-get update
 ---> Using cache
 ---> 279cb6c3ad0e
Step 2 : RUN DEBIAN_FRONTEND=noninteractive apt-get -y install python
 ---> Running in 4da733103196
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal
  python2.7 python2.7-minimal
Suggested packages:
  python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 22 not upgraded.
Need to get 4888 kB of archives.
After this operation, 16.0 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ saucy-updates/main libpython2.7-minimal amd64 2.7.5-8ubuntu3.1 [498 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ saucy-updates/main python2.7-minimal amd64 2.7.5-8ubuntu3.1 [1506 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ saucy-updates/main libpython2.7-stdlib amd64 2.7.5-8ubuntu3.1 [2481 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ saucy/main libpython-stdlib amd64 2.7.5-5ubuntu1 [7768 B]
Get:5 http://archive.ubuntu.com/ubuntu/ saucy-updates/main python2.7 amd64 2.7.5-8ubuntu3.1 [192 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ saucy/main python-minimal amd64 2.7.5-5ubuntu1 [31.4 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ saucy/main python amd64 2.7.5-5ubuntu1 [171 kB]
Fetched 4888 kB in 1s (3711 kB/s)
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 9981 files and directories currently installed.)
Unpacking libpython2.7-minimal:amd64 (from .../libpython2.7-minimal_2.7.5-8ubuntu3.1_amd64.deb) ...
Selecting previously unselected package python2.7-minimal.
Unpacking python2.7-minimal (from .../python2.7-minimal_2.7.5-8ubuntu3.1_amd64.deb) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Unpacking libpython2.7-stdlib:amd64 (from .../libpython2.7-stdlib_2.7.5-8ubuntu3.1_amd64.deb) ...
Selecting previously unselected package libpython-stdlib:amd64.
Unpacking libpython-stdlib:amd64 (from .../libpython-stdlib_2.7.5-5ubuntu1_amd64.deb) ...
Selecting previously unselected package python2.7.
Unpacking python2.7 (from .../python2.7_2.7.5-8ubuntu3.1_amd64.deb) ...
Selecting previously unselected package python-minimal.
Unpacking python-minimal (from .../python-minimal_2.7.5-5ubuntu1_amd64.deb) ...
Selecting previously unselected package python.
Unpacking python (from .../python_2.7.5-5ubuntu1_amd64.deb) ...
Processing triggers for mime-support ...
Setting up libpython2.7-minimal:amd64 (2.7.5-8ubuntu3.1) ...
Setting up python2.7-minimal (2.7.5-8ubuntu3.1) ...
Setting up libpython2.7-stdlib:amd64 (2.7.5-8ubuntu3.1) ...
Setting up libpython-stdlib:amd64 (2.7.5-5ubuntu1) ...
Setting up python2.7 (2.7.5-8ubuntu3.1) ...
Setting up python-minimal (2.7.5-5ubuntu1) ...
Setting up python (2.7.5-5ubuntu1) ...
 ---> dc9a01374b6d
Step 3 : RUN mkdir -p /app
 ---> Running in 6efdfa982407
 ---> 3d7d70cc1fd1
Step 4 : ADD . /app
 ---> e63757a4467b
Step 5 : WORKDIR /app
 ---> Running in d7d42e2b9069
 ---> f1ef66e631d8
Step 6 : CMD ["python", "main.py"]
 ---> Running in eef2ebdda192
 ---> 20354c36e193
Successfully built 20354c36e193
Removing intermediate container 4da733103196
Removing intermediate container 6efdfa982407
Removing intermediate container c9eeaccdde0f
Removing intermediate container d7d42e2b9069
Removing intermediate container eef2ebdda192
Now we can try and run our brand new image:
$ docker run -i username/python-hello-world
Hello world!
sounds cool ;)

No comments:

Post a Comment