Dabbling in Dockers
How frequently have we encountered a situation where one has an entire suite of tests ready but it is never executed as a part of a CI/CD pipeline OR just even executed by analysts, business or even developers in their local box??
The answer is ALWAYS!!!
There is no single reason for the above but a lot of factors prevent smooth test execution:
- Lack of admin rights to install required libraries.
- Different and Incompatible versions.
- Not enough resources are available to execute a test.
- Test Execution is not fast enough, etc.
If you work in an Agile organization, the above factors will only slow you down.
I often get requests for test automation from other Agile teams and most recently, it dealt with testing of a microservice. The easy part was developing the test code, but the challenge was to ensure that the requesting team was able to run the tests on-demand.
Here arose an opportunity to learn “implementation of Containers using Dockers”. I highly recommend this blog for any beginners.
In this article, I will quickly share how to write a “hello world” in python without installing Python on our local machine and execute the program in a Container
-: First things first :-
- Go to https://hub.docker.com/ and create a free account.
- Download Docker Desktop for Mac or Windows. I used Mac.
- Install the Docker Desktop. Upon successful installation, Double-Click on the Docker Whale to start Docker. Once Docker starts, you will see a Whale Icon and a notification that ‘Docker Desktop is running’
4. Install PyCharm IDE or just use your notepad to type a couple of python lines of code.
That’s All. Armed with the above 4 steps,
Python Code: Let us create a Python Code and save it as test.py
#!/usr/bin/env python
print(“Hello, World!”)
Dockers and Containers:
Before we run our code in a Python Container using Dockers, let us understand a few Docker commands.
Open the Terminal on your Mac and type the following:
To see the Docker version — docker version
To see the Docker Images — docker images
To pull a specific image from Docker registry— docker pull python
To run a container — docker run python
To see all running containers — docker ps -a
To build your own image — docker build -t imagename
Steps to Run our code in a Container
- ’python’ is an official image maintained by Docker. Let us pull this image to our local machine using the Terminal.
docker pull python
2. Now, let’s create our own image. This can be done with the help of a ‘Dockerfile’.
Create a Dockerfile in the same directory as our ‘test.py’.
- We need to specify a launching point, which will be our existing image (python).
- The next line is to add the text.py file to the Dockerfile.
- Followed by the execution command.
FROM python:3ADD test.py /CMD [ “python”, “-u”, “./test.py” ]
3. Let’s use the above Dockerfile to create an Image. User-generated builds are usually named as <dockerHub username>/<imagename>. Under the same directory where the Dockerfile resides, let’s run the following command
docker build -t ramansivasankar/pythontest .
4. Now run the Image to see if the code gets executed.
docker run ramansivasankar/pythontest
As you can see, although we developed our code in python using an IDE, we did not install python or pip in our local system. We built our own Image using the standard python image and successfully ran our code in a container. It was super easy and super fast. Wasn’t it?
Now, time for some Housekeeping
To check all running containers — docker ps -a
To end all containers — docker container prune
To launch a Container and end it — docker run — rm ramansivasankar/pythontest
We can leverage our existing code, microservice or even an entire automation framework and create an Image using Dockerfile.
That’s one small step for me, one giant leap for my confidence!!