Imagine you have the following existing python2 application "hello.py" with the following contents:
print "hello"
You must follow these steps to dockerize this application:
Create the folder where you want to save the Docker file.
Create a file called "Dockerfile"
A Dockerfile consists of several parts that you must define as described below:
Like a virtual machine, the image has an operating system. In this example, I am using ubuntu 16.04. Thus, the first part of the Docker file:
FROM ubuntu:16.04
Imagine you have a new Ubuntu - VM, now you need to install some things to make your application work, right? This is done using the following part of the Docker file:
RUN apt-get update && \ apt-get upgrade -y && \ apt-get install -y python
For Docker, you need to create a working directory on the image. The commands you want to execute later to start the application will look for files (for example, in our case a python file) in this directory. Thus, the following part of the Dockerfile creates a directory and defines this as a working directory:
RUN mkdir -p /usr/src/app WORKDIR /usr/src/app
As a next step, you copy the contents of the folder in which the Dockerfile is stored in the image. In our example, the hello.py file is copied to the directory that we created in the previous step.
COPY . /usr/src/app
Finally, the following line executes the command "python hello.py" in your image:
CMD [ "python", "hello.py" ]
The full Docker file is as follows:
FROM ubuntu:16.04 RUN apt-get update && \ apt-get upgrade -y && \ apt-get install -y python RUN mkdir -p /usr/src/app WORKDIR /usr/src/app COPY . /usr/src/app CMD [ "python", "hello.py" ]
Save the file and create an image by entering it into the terminal:
$ docker build -t hello .
It will take some time. Then check if the βhelloβ image was successfully created, as we called it on the last line:
$ docker images
Launch the image:
docker run hello
The output cry will be "hello" in the terminal.
This is the first launch. When you use Docker for web applications, you need to configure ports, etc.