How do I dockerize an existing application...the basics
Asked Answered
S

5

8

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?

Sexless answered 25/8, 2014 at 18:37 Comment(1)
What have you tried so far? Where are you stuck? As you haven't even shared what the container should do, it's nearly impossible to provide helpSexology
B
9

Your index.php is not really an application. The application is your Apache or nginx or even PHP's own server.

Because Docker uses features not available in the Windows core, you are running it inside an actual virtual machine. The only purpose for that would be training or preparing images for your real server environment.

There are two main concepts you need to understand for Docker: Images and Containers.

An image is a template composed of layers. Each layer contains only the differences between the previous layer and some offline system information. Each layer is fact an image. You should always make your image from an existing base, using the FROM directive in the Dockerfile (Reference docs at time of edit. Jan Vladimir Mostert's link is now a 404).

A container is an instance of an image, that has run or is currently running. When creating a container (a.k.a. running an image), you can map an internal directory from it to the outside. If there are files in both locations, the external directory override the one inside the image, but those files are not lost. To recover them you can commit a container to an image (preferably after stopping it), then launch a new container from the new image, without mapping that directory.

Belief answered 25/8, 2014 at 21:14 Comment(1)
I am... hesitant about this answer. Of course, the file index.php is not an application by itself, but the whole thing that can be launched from an index.php can indeed be an application. I mean, browser based CRM's are full fledged applications, as much as their desktops equivalents.Beatify
G
15

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.

Guenna answered 11/4, 2017 at 11:42 Comment(0)
B
9

Your index.php is not really an application. The application is your Apache or nginx or even PHP's own server.

Because Docker uses features not available in the Windows core, you are running it inside an actual virtual machine. The only purpose for that would be training or preparing images for your real server environment.

There are two main concepts you need to understand for Docker: Images and Containers.

An image is a template composed of layers. Each layer contains only the differences between the previous layer and some offline system information. Each layer is fact an image. You should always make your image from an existing base, using the FROM directive in the Dockerfile (Reference docs at time of edit. Jan Vladimir Mostert's link is now a 404).

A container is an instance of an image, that has run or is currently running. When creating a container (a.k.a. running an image), you can map an internal directory from it to the outside. If there are files in both locations, the external directory override the one inside the image, but those files are not lost. To recover them you can commit a container to an image (preferably after stopping it), then launch a new container from the new image, without mapping that directory.

Belief answered 25/8, 2014 at 21:14 Comment(1)
I am... hesitant about this answer. Of course, the file index.php is not an application by itself, but the whole thing that can be launched from an index.php can indeed be an application. I mean, browser based CRM's are full fledged applications, as much as their desktops equivalents.Beatify
C
2

You'll need to build a docker image first, using a dockerFile, you'd probably setup apache on it, tell the dockerFile to copy your index.php file into your apache and expose a port.

See http://docs.docker.com/reference/builder/

See my other question for an example of a docker file: Switching users inside Docker image to a non-root user (this is for copying over a .war file into tomcat, similar to copying a .php file into apache)

Cutcheon answered 25/8, 2014 at 20:15 Comment(0)
P
0

First off, you need to choose a platform to run your application (for instance, Ubuntu). Then install all the system tools/libraries necessary to run your application. This can be achieved by Dockerfile. Then, push Dockerfile and app to git or Bitbucket. Later, you can auto-build in the docker hub from github or Bitbucket. The later part of this tutorial here has more on that. If you know the basics just fast forward it to 50:00.

Petulance answered 17/6, 2017 at 0:42 Comment(0)
P
0

With the latest version of Docker you can just cd to your project directory and execute:

docker init

see docs

Once you get the files generated you can run build to create your first image.

docker build .
Prickle answered 22/4 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.