Hello Docker 2: build and run your container

We have discussed how to run an existing Docker image from command line in Hello Docker 1. What about creating your own Docker image and run it?

This article illustrates how to create a simple Docker app running a python program.

1. Prepare a project folder and the simple python file

2. Create a Dockerfile

A Dockerfile provide the configuration to create a Docker image. Here is one we are going to create.

“FROM python”

defines the base image our new image is going to based on.

You may wonder why we chose this specific image as your base. Because your app is a Python app, you need your image to contain the Python binary executable to run the app. You could have used any image that contains that binary, or you could have even used a Linux distro base image such as fedora or ubuntu and installed Python into the container at image build time. But because the node image is made specifically for running Python apps, and includes everything you need to run your app, you’ll use that as the base image.

“ADD hi.py /hi.py”

adds the simple python file on top of the base image.

Docker image is built on layers. hi.py is the last layer on top of the Python base image we just installed.

ENTRYPOINT [“python3”, “hi.py”]

specifies the command to run when running a Docker container based on the new image we are going to create.

3. Build our first Docker image

We see 3 steps in the console corresponding to the 3 commands we specified in the Dockerfile.

One intricacy of Docker build is that the process doesn’t happen on the Docker client, but the Daemon. The whole layer of content needs to be uploaded to the Daemon machine or VM to build.

If the client and the daemon happen to be in two different physical machines, the file uploading can be the bottleneck of the whole build process.

So don’t include any unnecessary files in the build directory, especially when the daemon is in a remote machine.

“docker images” shows the docker image we have just built and the python image that has been downloaded during the process.

4. Run the container

A container is created and then run successfully!

What’s next?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store