Today I was searching for virtualization techniques and I learned about Docker. As I was digging into, it seemed more interesting. After a little research, I ended up installing Docker in my windows machine and a little POC. So, let’s start quickly.
What is Docker?
Docker is a software platform which can be used to create containers and a container is very much similar to a virtual machine but a container does not create whole virtual operating system rather uses same kernel (and may be other components) of operating system to run the application on which Docker container is running on. The best part, you can create as many containers as you want based on your need and capacity of host machine.
Why Docker is gaining popularity and who can benefit from it?
- Docker is popular in cloud based applications where large applications can run on containers and can be scaled on runtime.
- Useful for developers and operations team (DevOps) where a programmer can be more worried about coding instead of system setup and operations team to simplify deployment process.
- With Docker one can create custom images of system and code files and the same image can be deployed in different environments like dev, test and production which solves problem of inconsistent behavior of application in different environments.
- Some cloud based applications like Kubernetes can use Docker images to scale system based on the need.
- There are many prebuild Docker images available publicly, based on need developer can use one and deploy his application on it.
In this POC I will create a python container and run python script on it. Let’s do this:
Step 1: Install Docker on your windows machine
link is given below for windows:
Step 2: Create a folder in local directory
Like I have created a folder “docker” in C drive.
Step 3: Create python script
Create a file "helloworld.py" and paste code below:
Step 4: Create a Docker file
Create a file named “DockerFile” without any extension and paste lines below:
#publicly available docker image "python" on docker hub will be pulled
#creating directory helloworld in container (linux machine)
RUN mkdir c:\home\helloworld
#copying helloworld.py from local directory to container's helloworld folder
COPY helloworld.py /home/helloworld/helloworld.py
#running helloworld.py in container
CMD python /home/helloworld/helloworld.py
Above description of each line is given in comments starting with #. You can search any images publicly available in Docker hub using command: docker search *SearchText*. In my case I ran command in CMD: docker search *python*
I got search results like below and used python image:
Step 5: Building the Image using DockerFile
First target the docker directory we created in Setp 2 using command:
Once we are inside directory run the command:
docker build -t helloworldapp .
docker build is command to build the docker file available in directory, -t is to tag the name "helloworldapp" to the container.
Now it will perform operation as written in dockerfile, like pulling python image, copying the script file and running the script. And operation will look like:
If Image of python is available in local system, it will not pull it again and use the same.
Step 6: Run the container
Use the command:
docker run helloworldapp
And that’s it. It is that much simple. Don’t forget to write your thoughts in comment section.