Installing Docker, and Creating, Running First Python Script Using it on Ubuntu 20.04 LTS

Image Source: Google

I always get in trouble when creating a new project with different dependencies installed on my machine. I had to reinstall new libraries, which is doing a new thing repeatedly, and it’s time-consuming. Then I learned about docker. Docker will allow you to create a virtual machine to install all your dependencies for your specific project without tempering your primary system. Still, it is faster than the virtual machine. In this tutorial, we are going to see:

  1. Installing docker on ubuntu 20.04 LTS
  2. Creating and running a python script using the docker image

Installing Docker

For installing docker from the website for the different OS, you can visit here. First of all, we are going to uninstall the previously installed docker (if have any). We will run the following command in the terminal to do that:

Then we need to update the apt packages and install the following dependencies by running the commands:

Now we need to add Docker’s official GPG key to the system.

We will use the following command to set up a stable repository.

To install the Docker engine first we have to update the apt package again and run the following commands:

Now, the docker should be installed in our system, but before we test it, first we need to add the docker into the user group, otherwise, in some systems, it will give an error. We can do this by using the following commands:

After running this command, you have to logout or restart the pc. Then run the following command to test the docker system.

This will download a test container and print some messages and then it will exit.

Creating First Docker Script

Now, it’s time to create our first program that will run on docker. Here we will create a simple script that will measure the square root of a number using python’s numpy library. So, in this program, we have a dependency, and that is NumPy. We have to install the dependency inside our docker container. First, we will create a folder containing all the necessary things such as a python file, docker image, etc.

Then we will create the python script which we will run, let’s name it and write the following code into that.

Now, let’s create a file name Dockerfile. This file is the blueprint of the docker image. "A docker image is a combination of a file system and parameters." In other words, this file will contain the information on the working environment, dependencies, etc. of our docker app. And the docker container (it's an instance of docker image) will run the docker system.

Let’s put the following lines in the Dockerfile.

Now, let’s understand what these lines represent. The docker needs an environment where the docker will run, and this environment can be an operating system, a software package, a python environment, etc. Here we are using Python version 3.8 as our environment. By executing FROM python:3.8 line, the docker container pulls python version 3.8 from the docker hub. To add the python script in the docker current directory, we will use ADD . line. The . specifies the current directory. Now, we will be going to install the dependencies, in the python script we have only one dependency, and that is NumPy. So to install the NumPy dependency, we are using RUN pip install numpy. If your program has other dependencies, you can add them in this line. And finally, to run the python script, we will use CMD command. Usually, we run a python script in the terminal by using python command. Just like this, we will pass this command into a tokenized list with the CMD command. So, CMD ["python", ""] will run the python script inside the docker container.

We have the Dockerfile ready, and we now know what it contains and what the lines inside the Dockerfile does, it is time to build the docker image! To build the docker image, we need to give the following command:

Here first-docker is our docker image name. We can name it whatever we want. And the -t argument is used for tagging the resulting image. After running this command, it will look like the following screenshot.

So, our docker image is now ready. To run our image we need to give the following command in the terminal:

This command will run the docker image and give the output of the python script we have written. In our case, we have calculated the square root of 16 using NumPy’s sqrt function. So we will see the result like the following screenshot.

Congratulations! You have just run your first docker app. We can do more complex things with docker. Keep digging. The code for this tutorial can be found here.

Have fun and Stay Safe!



Originally published at on December 26, 2020.

Ph.D. Student at Linköping University, Sweden.

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