Getting Started with Docker Containerization
Start your free 7-days trial now!
In this tutorial, we will containerize a simple Python program that prints
"Hello world!". In latter guides, we will containerize more complex applications such as a web server and a machine learning model.
Suppose we have the following
if __name__ == '__main__':print('Hello world!')
Running this python file will output the following famous words:
We will now containerize our Python program using Docker. In the same directory as the
app.py, create a file called
Dockerfile (without any extensions), and copy and paste the following content:
FROM pythonCOPY app.py .CMD ["python", "app.py"]
Here, note the following:
Every Dockerfile begins with the
FROMclause which is used to specify the so-called base image. To run our simple python code, we actually need a lot of programs as well as a file system pre-installed within the Docker container. This not only includes the Python programming language but also some files and configurations of an operating system. Keep in mind that we're not installing a fully fledged operating system (as in a virtual machine) since the the container will always use the kernel of the host machine.
Instead of specifying all of these dependencies ourselves, the Docker community has released an image called
pythonthat includes the language as well as all other dependencies. This makes it much easier for us to get started. Since we are using the python image to build our image, we call the
pythonimage a base image.
COPYcommand copies the file
app.pyfrom the current directory into the root directory of the docker container. Remember, the docker container is isolated from your local machine, which means that docker container does not have access to resources outside the container. Therefore, we must use the
COPYcommand to copy the
app.pyfile into the docker container.
CMDclause specifies the commands you wish to run in the docker container. Since we already have Python installed at this point within the container, we can execute the command
python app.pyto actually run our small python script.
Unlike Python, the Dockerfile requires that you use double quotes
" instead of single quotes
' to specify the commands. For instance, suppose we wrote:
CMD ['python', 'app.py']
The build will still complete, but the image will not run properly.
Now, let's build our Docker image by running the following command:
docker build -t my_first_app .[+] Building 2.5s (7/7) FINISHED=> [internal] load build definition from Dockerfile...
-tflag, which stands for tag, specifies the name of the image.
.at the end means that the location of our Dockerfile is at the current directory.
Great, we've managed to build a Docker image using the Dockerfile! To confirm that our image has been built, run the following command:
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmy_first_app latest 5092209c8fd0 26 seconds ago 861MB
You may be wondering why the
SIZE of the container is so large (
861MB). This is because the base image
python has many dependencies, which includes a linux operating system.
Now that we built our image, the last two steps are to:
build a Docker container.
execute our program.
We can accomplish both steps at once using the
docker run my_first_appHello world!
Great, our exciting Python script has successfully run! Now that we know the basics of containerizing an application, we shall move on to more advanced and realistic examples in the upcoming guides!