Windows or macOS: Install Docker Desktop; Linux: Install Docker and then Docker Compose; Containerize a Python service. We show how to do this with a simple Flask service such that we can run it standalone without needing to set up other components. From flask import Flask server = Flask(name) @server.route('/') def hello. Next, follow the instruction below to install the Docker engine on your system. Go to Docker CLI and run the Docker version to verify the version of Docker installation on the system. Congratulations, Docker Installation on Windows is now done, and now, you are ready to build and run Docker images and containers on the Docker ecosystem. Jun 08, 2016 A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc.
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:
- Installing docker on ubuntu 20.04 LTS
- Creating and running a python script using docker image
For installing docker from the website for the different OS, you can visit here. First of all, we are going to uninstall previously installed docker (if have any). We will run the following command in the terminal to do that:
Setting up the Repository
Then we need to update the
apt packages and install the following dependencies by running the commands:
Now we need to add the Docker’s official GPG key to the system.
We will use the following command to set up the stable repository.
Installing Docker Engine
To install the docker engine first we have to update the
apt package again and run the following commands:
Docker Install Python Ubuntu
Now, the docker should be installed in our system, but before we test it, first we need to add the docker into usergroup, otherwise in some system it will give 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 the 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 python file, docker image etc.
Then we will create the python script which we will run, let’s name it
main.py 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
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 main.py . 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 main.py command. Just like this, we will pass this command into a tokenized list with the
CMD command. So,
CMD ['python', 'main.py'] will run the python script inside the docker container.
Building the Docker Image
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:
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.
Running the Docker Image
So, our docker image is now ready. To run our image we need to give the following command in the terminal:
Python Install Docker Using
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! We have just run our first docker app. We can do more complex things with docker, keep digging. The code for this tutorial can be found in here.
Have fun and Stay Safe!