search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
chevron_leftDocker
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment
auto_stories Bi-column layout
settings

Getting Started with Docker Containerization

schedule Jun 6, 2023
Last updated
local_offer
Docker
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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 app.py file:

if __name__ == '__main__':
print('Hello world!')

Running this python file will output the following famous words:

Hello world!

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 python
COPY app.py .
CMD ["python", "app.py"]

Here, note the following:

  • Every Dockerfile begins with the FROM clause 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 python that 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 python image a base image.

  • The COPY command copies the file app.py from 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 COPY command to copy the app.py file into the docker container.

  • Finally, the CMD clause 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.py to actually run our small python script.

WARNING

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
...

Here:

  • the -t flag, which stands for tag, specifies the name of the image.

  • the . 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 images
REPOSITORY TAG IMAGE ID CREATED SIZE
my_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:

  1. build a Docker container.

  2. execute our program.

We can accomplish both steps at once using the run command:

docker run my_first_app
Hello 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!

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!