Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dockerize an existing application...the basics

I am using windows and have boot2docker installed. I've downloaded images from docker hub and run basic commands. BUT How do I take an existing application sitting on my local machine (lets just say it has one file index.php, for simplicity). How do I take that and put it into a docker image and run it?

like image 706
user2808895 Avatar asked Aug 25 '14 18:08

user2808895


1 Answers

Imagine you have the following existing python2 application "hello.py" with the following content:

print "hello"

You have to do the following things to dockerize this application:

Create a folder where you'd like to store your Dockerfile in.

Create a file named "Dockerfile"

The Dockerfile consists of several parts which you have to define as described below:

Like a VM, an image has an operating system. In this example, I use ubuntu 16.04. Thus, the first part of the Dockerfile is:

FROM ubuntu:16.04

Imagine you have a fresh Ubuntu - VM, now you have to install some things to get your application working, right? This is done by the next part of the Dockerfile:

RUN     apt-get update && \ 
        apt-get upgrade -y && \
        apt-get install -y python

For Docker, you have to create a working directory now in the image. The commands that you want to execute later on to start your application will search for files (like in our case the python file) in this directory. Thus, the next part of the Dockerfile creates a directory and defines this as the working directory:

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

As a next step, you copy the content of the folder where the Dockerfile is stored in to the image. In our example, the hello.py file is copied to the directory we created in the step above.

COPY . /usr/src/app

Finally, the following line executes the command "python hello.py" in your image:

CMD [ "python", "hello.py" ]

The complete Dockerfile looks like this:

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 build the image by typing in the terminal:

$ docker build -t hello .

This will take some time. Afterwards, check if the image "hello" how we called it in the last line has been built successfully:

$ docker images

Run the image:

docker run hello

The output shout be "hello" in the terminal.

This is a first start. When you use Docker for web applications, you have to configure ports etc.

like image 172
Jan Clemens Stoffregen Avatar answered Oct 16 '22 16:10

Jan Clemens Stoffregen