top of page

Docker: Containerize your first app

Rebecca Boardman

In previous blogs we've gone through how to deploy an existing image as a container using docker. In this weeks blog post we'll look at how to create our very own docker images to containerize an application!


Prerequisite:

  • https://github.com/beckyboardy/WebAppFlask --This is a simple example application that we will be using in this demo

  • Docker needs to be installed on your machine

  • I also use WSL so feel free if you want to do the same. You can find out more info here

  • and how you can make it work with docker here


Manual app deployment

First off, lets deploy the application manually so you get a feel for how the application works! I'll be using my WSL as advised in the prereqs!


1. Launch your WSL and run the following command. Please note if you get any permission denied errors then append to run as admin by adding "sudo" at the start of the commands

apt-get update

2. Once the above has completed now run the following command to install python

apt-get install -y python

3. Then run the following command to install pip

*Please note if python-pip doesn't work you can try to install pip3 by substituting it to python3-pip

apt install -y python-pip

4. We are now ready to install flask.

pip install flask

5.) Create your own new directory called myapp and switch to the directory

mkdir myapp
cd \myapp

6. Create your application file within a "myapp" folder

cat > myapp.py

6. Copy the contents from my github application here and paste into the terminal. You may get a warning but select paste anyway. Press CTL C once your ready to quit out.



7. Now we are ready to run the application. To run type the following:


FLASK_APP=myapp.py flask run --host=0.0.0.0

8. Now browse to http://127.0.0.1:5000/ and you should see your nice little web application pop up like so.. and there you have a simple webapp!





Containerizing the application


To do this we'll need to create a docker file

Think of a docker file like a wedding cake- it's built up in a series of layers. Each layer represents a set of instructions. As per the above we went through all the steps needed in order to deploy the application. Therefore we now know the re-requites needed in order to containerize the application. Docker builds images automatically by reading instructions from a docker file.

  1. Make a directory where your going to run the file from and then change to that directory

mkdir mywebapp
cd \mywebapp

2. You'll need to create the app.py file to store the application code as we'll be using this to copy it over to the docker container

cat > app.py


3. Copy the contents from my github application here and paste into the terminal. You may get a warning but select paste anyway. Hit enter and then press CTL C once your ready to quit out.




Lets pause here to go through the structure of a docker file

The docker file will follow a lot of the manual steps we ran in the manual app deployment scenario. You can see my example docker file here.

Before we deploy this I want to go into explaining the contents of the file

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y python python-pip
RUN pip install flask
COPY app.py /opt/
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0 --port=8080
FROM: A Docker file must always begin with a FROM instruction. This is the base image for which your container will run off. This can be FROM Scratch or you can build images from other images. In our case ours will run Ubuntu 16.04.
RUN: Run commands will execute commands (adding on another layer to your image) eg in this case as part of a prerequisite we require pyton, pip3 and flask to be installed.
COPY: Copy can create new files or directors from source. In our case it takes app.py that we created in our source folder and copies it to the container within the opt folder.
ENTRYPOINT: Allows you to configure a container to run as an executable. In order to run the application we need to execute the above command.

4. Lets create our docker file.

cat > dockerfile

5. Paste in the dockerfile contents. You can see my example docker file here Hit enter and then press CTL C once your ready to quit out.



6. Now we are ready to build our docker image! Run the following to build. Specify -t to give it a tag

docker build . -t mywebapp

you'll get an output something like the below which will run through all your commands listed within the docker file


7. If you run the below command to view your images you'll see mywebapp appear within the list of images

docker images


8. Now we are ready to run our container! If your wanting to access it through windows it's really important to remember to map local ports to the docker container.

I'm giving the container a name of BeckysWebApp and mapping ports 8081 (local to my windows machine) to 8080 (the container).

 docker run -d --name BeckysWebApp -p 8081:8080 mywebapp

9. This should start your container and will tell you it's running off port 8080 (see screenshot below)





If you want to test this out locally on your windows by opening up a browser remember that the port 8080 is local to the container. In Step 8 we actually mapped port 8081 from our windows machine to the container port 8080. So if you want to access this through our windows machine we will need to use port 8081. Open a browser and type in http://localhost:8081/ . Your result should look like this:




Congrats you've just containerised your first application


But the fun doesn't stop there

So you've seen how to create your own custom container. But how do we push this to the "docker hub" so that everyone could download this image?


1. Well first things first you'll need a docker account. You can sign up for a free one here

2. Make a note of your username. In order to push your docker container image you'll need to add a tag to the image with your username.

3. To do this go to the directory of your webapp. If your not already still in there

cd \mywebapp\

4. Run the build command again but this time specify your username in the tag as well as the app name. It should look something like this.

docker build . -t Username/mywebapp

5. This then builds the image again for you to push with the correct tags.


6. Login to docker by typing the following into your terminal and specifying your username and password (hit enter)

docker login

7. Then you'll be ready to push your application by entering the following command

 docker push beckyboardy/mywebapp

this will output something like the below to show it's been successful:



8. If you browse on https://hub.docker.com/ to your profile under repos you'll see an entry for your web application.


And that's it for this weeks blog post. I hope you enjoyed it. I really enjoy looking at how to do tasks like this. I find it takes a bit of fumbling around but once I figure it out- wow the sense of achievement is just incredible! I'd like to go more in-depth into containers in the future but at the moment doing tasks like the above really allows me to gain a strong foundation that can only be built on. As always if you have any questions please feel free to get in touch. Thank you for reading!

Recent Posts

See All

Comments


bottom of page