Docker for Test Automation

Mohamed Yaseen
8 min readNov 29, 2022

Docker is an open platform for app development, delivery, and operation. Docker allows you to decouple your apps from your infrastructure, allowing you to release software more quickly. Docker allows you to manage your infrastructure in the same manner that you control your apps.

Why Docker?

Docker is a program that makes it easier for developers to create, deploy, and execute containerized applications. Containers enable developers to bundle and deploy a program with all its requirements and customizations, such as libraries and other dependencies. By doing so, developers may be confident that their program will function properly on computers other than the one on which they wrote the code. Thus, in the world of technological progress, all of the services provided to customers over the internet necessitate something that may assist us in minimizing deployment time and utilizing the least amount of system resources. In this instance, a technology like Docker is required.

Docker makes it easy to install and run software without worrying about setup or dependencies.

What Docker?

Docker is a platform or ecosystem around creating and running containers.

Docker Installation

Ubuntu

Install Docker Engine on Ubuntu

CentOS

Install Docker Engine on CentOS

Windows

Install on Windows

MAC

Install on Mac

After the successful installation, you can use the following command to check the version.

$docker --version
Docker version 19.03.12, build 48a66213fe

Docker Architecture

Docker is built on a client-server model. The Docker client communicates with the Docker daemon, which is in charge of constructing, operating, and distributing your Docker containers. A Docker client and daemon can run on the same machine, or a Docker client can connect to a remote daemon. The Docker client and daemon communicate using a REST API, UNIX sockets, or a network interface. Docker Compose is another Docker client that allows you to deal with applications made up of a collection of containers.

Docker Daemon

Docker daemon (dockerd) handles Docker objects such as images, containers, networks, and volumes by listening to Docker API calls. To manage Docker services, a daemon can communicate with other daemons.

Docker Client

Many Docker users interact with Docker primarily through the Docker client (docker). When you use commands like docker run, the client transmits them to dockerd, which executes them. The docker command uses the Docker API. The Docker client can interact with many daemons.

Docker Desktop

Docker Desktop is a simple-to-install program for Mac, Windows, or Linux that allows you to create and distribute containerized apps and microservices. Docker Desktop comprises Dockerd, Docker Client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. See Docker Desktop for further details.

Docker Registries

Docker images are stored on a Docker registry. Docker Hub is a public registry that anybody may access, and Docker is set up by default to seek images on Docker Hub. You may even set up your register. The relevant images are retrieved from your specified registry when you use the docker pull or docker run commands. When you execute the docker push command, your image is pushed to your chosen registry.

Docker objects

Docker allows you to create and utilize images, containers, networks, volumes, plugins, and other items. This section provides a high-level overview of several of those things.

Images

An image is a read-only template containing Docker container creation instructions. A picture is frequently based on another image, with some added customization. For example, you might create an image based on the Ubuntu image that installs the Apache web server and your application and the configuration parameters required to operate your application. You may either generate your photos or utilize those made by others and published in a registry. To construct your image, develop a Dockerfile using a simple syntax for outlining the procedures required to generate and execute the image. Each Dockerfile instruction adds a layer to the image. Only the changed layers are rebuilt when you edit the Dockerfile and rebuild the image. When compared to other virtualization systems, this is part of what makes pictures so lightweight, tiny, and quick.

Containers

A container is a runnable image instance. The Docker API or CLI may create, start, stop, move, or destroy containers. You may attach storage to a container, link it to one or more networks, or even construct a new image based on its existing state. A container is separated from other containers and its host computer by default. You can specify how well a container’s network, storage, or other underlying subsystems are isolated from other containers or the host computer. A container’s image and any configuration parameters you provide when you build or start it describe it. When a container is removed, all state changes that were not saved in persistent storage are lost.

Docker Container Commands

Run

$docker run hello-world
$docker create hello-world
aec0f51C489ba65413c2ad961c33fe77a2978b427404c7365ac1b338a28afab4(container id)
$docker stärt -a aec0f51C489ba65413c2a961c33fe77a2978b427404c7365ac1b338a28afab4(start -a gives output)
Hello from Docker!
This message shows that your installation appears to be working correctly,
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(and64)
3. The Docker daemon created a new contatner from that mage which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker cltent, which sent tt
to your terminal.
To try something more ambittous, you can run an Ubuntu container with:
§ docker run -it ubuntu bash
Share mages, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
$docker start aecof51c489ba65413c2ad961c33fe77a2978b427404c7365ac1b338a28afab4(without -a gives the conatiner id only)
aec0f51c489ba65413c2ad961c33fe77a2978b427404c7365ac1b338a28afab4

Run with Overriding Command

$docker run busybox echo ls

List Running Containers Command

$docker ps

History of Containers Command

$docker ps -a // docker ps --all

Remove all Unused Containers Command

$docker system prune

Retrieving logs/outputs Containers Command

$docker run -it -d mongo
4152463137194130ec7aebc849230550867ed96c4290202cc89a9ebdcb302fe5
//-it - interaactive mode
//-d - deamon (backgroung)
$docker logs 4152463137194130ec7aebc849230550867ed96c4290202cc89a9ebdcb302fe5

Stop Containers Command

$docker ps
CONTAINER ID IMAGE
1865c53ee7a8 busybox
$docker stop 1865c53ee7a8 (take morethan 10 seconds to stop)
1865c53ee7a8

$docker kill 1865c53ee7a8 (immediately stop)
1865c53ee7a8

Executing Commands in Containers

$docker exec 019dd4498091 redis-cli
$docker exec -i 019d4498d91
$docker exec -it 019d4498d91 redis-cli (-it - gives the terminal of the container)

Getting CMD Prompt inside the Containers

$docker run -d redis-cli (redis-cli will run as deamon in the background)
2ff8df122a143f785dfc35a9435f3386eece408a87c3a4ce965aa6032be3180
$docker ps -a (to get the container id)
$docker exec -it 2ff8df122a14 sh (to access the terminal of the container 2ff8df122a14)
#1S
# pwd
/data
#cd ~/
#1S
#cd /
#Is
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin
# echo hi there
hi there
# export b=5
# echo $b
5
# exit (exit the docker terminal)
$docker exec -it 2ff8df122a14 bash
root@2ff8df122a14:/data# cd /
root@2ff8df122a14:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmo usr var
root@ff8df122a14:/# exit
exit
$docker exec -it 2ff8df122a14 bash
root@2ff8df122a14:/data# redis-cli
127.0.0.1:6379> exit
root@2ff8df122a14:/data#

Summary of Commands

  1. docker — version
  2. docker pull ubuntu
  3. docker images
  4. docker ps (running containers)
  5. docker ps -a (all containers)
  6. docker run -it -d ubuntu (-it=> interaction(gives container’s terminal), -d => background process(it will run in background even if close the container’s terminal))
  7. docker restart <<containerid>>
  8. docker restart <<containerid>>
  9. docker stop <<containerid>>
  10. docker exec -it <<containerid>> bash(to login to the container)
  11. docker rm <<containerid>>
  12. docker rmi <<imageid>>
  13. docker kill <<containerid>> (stop container forcefully)
  14. docker inspect <<containerid>> (get the information of the container)
  15. docker image ls
  16. docker image prune -a(remove dangling images(images doesn’t have any containers), -a => all images)
  17. docker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts (-p => port number/ 8080(local port):8080(container port))
  18. docker logs <<containerid>> (see the logs of the container)
  19. docker run -it — name MYUBUNTU -d ubuntu ( — name => giving a name for the container)
  20. docker run — rm -it — name MYUBUNTU -d ubuntu ( — rm => container will remove automatically once it stopped)
  21. docker cp file.txt MYUBUNTU:/tmp (copy a file from host to a container)

--

--

Mohamed Yaseen

Experienced QA Automation Lead with expertise in test automation, frameworks, and tools. Ensures quality software with attention to detail and analytical skills