What is Docker? How to create a Docker image and execute an application within a container ?
Varun Lobo
Data Scientist | Automotive Engineering | Analytics | Agile | Python | SQL | Data Science
What is Docker?
Docker is a platform as a service product that uses an OS level virtualization of your application to create an image and to use this image to deliver software in packages called containers. It is a platform designed to help developers build, share and run modern applications with ease without the tedious system level complications, so that a team can focus more on the code rather than server setup and installations.
In this article, I will create a docker file and show you how to create an image which will be a virtual screenshot of your application and then run it within a container.
Prerequisites
One needs to have Docker desktop installed in your system and an editor to create the Dockerfile. Here is the link to download Docker Desktop: Link
Creating a Dockerfile.
Dockerfile is a file with a set of instructions telling Docker desktop steps to execute. Basically, A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
It does not have an extension and is always named 'Dockerfile'. Note: using 'dockerfile' or 'DockerFile' may give errors.
Some of the lines above is self explanatory, however, I will explain what these do.
A?Dockerfile?must begin with a?FROM?instruction. This may be after?parser directives,?comments, and globally scoped?ARGs. The?FROM?instruction specifies the?Parent Image?from which you are building. In our case, we're running a #python application using #streamlit.
Since we would like to run this application locally, we specific a local port for our browser. Our default port is 8501. This will make our application appear on https://172.17.0.2:8501
WORKDIR specifies where all files are stored. The?WORKDIR?instruction sets the working directory for any RUN, CMD, ENTRYPOINT etc. instructions that follow it in the Dockerfile.
There might be a possibility that the requirement.txt file changes over time, for this we will COPY the requirement.txt file into the working dir before creating an image.
领英推荐
RUN command will execute the line 'pip3 install -r requirements.txt' to make sure all dependent libraries are installed before we build our image.
Last is CMD, we run the command that executes our application. In our case, I am running a streamlit application in python with the name 'Sentiment_Analysis_NLTK.py'
Once the Dockerfile is created, go to the command prompt and navigate to the working directory. Type the following:
docker build -t str:latest .
This command line will build an image called 'str' with a tag called 'latest' and this will show up in your Docker desktop.
You might notice this image is 1.36GB. However, my working dir is less than 1 MB. The reason is because a docker image will contain all the supporting OS dependencies that will be used to run the application. Literally an 'image' of your application on your system.
If you want to see a list of all images created, run the following command on the command line.
docker images
The last step is to execute this image within a container using the following command:
docker run -p 8501:8501 str?
You know you have a container up and running, is by checking your Docker desktop.
Although knowing Docker is essential from a deployment stand-point, Data Scientists who know how to deliver applications that can be easily handed over to other teams for cross-functional collaboration is an indispensable skill in todays fast paced world of data.