Goglides Dev 🌱

Cover image for Process to containerize simple python web application.
Rahul Gautam
Rahul Gautam

Posted on • Updated on

Process to containerize simple python web application.

Today, We are going to discuss on containerizing a simple web application.

What is Container?

Image description

Simply, Docker containers are like boxes that you can put things in. You can put anything you want in a container, and it will keep it safe and sound. You can put multiple things in one container, or you can have just one thing in a container. It's up to you!

Docker File

Commands that you use on your command line to build an image can be put together in a file. These text files are called Dockerfile. They read the instructions given by users and build images automatically.

Let's try making a Dockerfile that runs ubuntu and echos our string. To create a Dockerfile, you will require to begin your instruction using FROM where you assign a parent image, which is your main build. We will build an ubuntu image and display "This is a test" on our screen.

Creating a Docker File:

1) Let's Create a Dockerfile (You can use any text editor. We will be using vi).

Image description

2) Now, let's add our command to pull the ubuntu image and echo the message.

FROM ubuntu
CMD echo "This is a test" 
Enter fullscreen mode Exit fullscreen mode

3) Now, let's execute our command automatically using the docker build command.

docker build . -t ubuntutest
Enter fullscreen mode Exit fullscreen mode

Image description

You can see the Dockerfile has been built.

4) Now, let's run our build.

docker run ubuntutest
Enter fullscreen mode Exit fullscreen mode

Image description

Dockerfile to containerize an application

We are going to use Dockerfile to containerize our simple application.

Also, A simple Django application(API) has been made, which shows json data that I randomly generated from mockaroo. If you want to use the code I am using here, You can find it on https://github.com/Gamerited/JsonDjango .

1) First, let's check if the application works on a local server.

python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Image description

It seems to be running fine. Our task is to get it to run in a docker container.

2)Now, Let's create a Dockerfile to put up our command. Our Dockerfile will look like this,

FROM python:3.11-rc-slim

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN pip install --upgrade pip
COPY requirements.txt /code/

RUN pip install -r requirements.txt
COPY . /code/

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Enter fullscreen mode Exit fullscreen mode

Here,

  • We are using python (3.11-rc-slim).

  • We are also using two environment variables (PYTHONDONTWRITEBYTECODE and PYTHONUNBUFFERED). PYTHONDONTWRITEBYTECODE removes .pyc files from the container, and PYTHONUNBUFFERED buffers our output to make it look normal.

  • We now create directory name code and change our directory to code.

  • We now upgrade our pip to the latest version and copy requirements.txt files to our code directory so we can install all the requirements to run this application.

  • We now copy all the source code to the directory.

  • We now inform Docker that the container listens on port 8000 using EXPORT.

  • We now command our container to run the python application on port 8000.

Now let's run our application. For this process, first, we will use the docker build command.

docker build . -t pythonapi
Enter fullscreen mode Exit fullscreen mode

We are naming our container with the image as pythonapi using the tag (-t) argument.

Image description

The build is completed. Now, let's run our docker application using the command,

docker run pythonapi
Enter fullscreen mode Exit fullscreen mode

You can see it is running.

Image description

Let's visit http://0.0.0.0:8000/,

Image description

Uh oh, We were Unable to connect . We could not view our application because we had not forwarded the port from the container to our local device. We have to add the -p argument to our command. It will look like this,

docker run -p 8000:8000 pythonapi
Enter fullscreen mode Exit fullscreen mode

Where the initial port is our local device's port and the port number following it is what port we want to forward from the container. Now, let's visit the application,

Image description

Voila! Now you can see your simple application is currently running inside a container.

Thank you for reading this blog, next time we will be publishing the image to Docker Hub.

Top comments (0)