A Hands on Docker Tutorial

A Hands on Docker Tutorial

A python simple calculator container

ยท

2 min read

What is Docker?

Docker is a tool that makes it easier to create, deploy, and run applications using containers.

A docker container is a collection of dependencies and code organized as software that enables applications to run quickly and efficiently in a range of computing environments.

A docker image, on the other hand, is a blueprint that specifies how to run an application. In order for Docker to build images automatically, a set of instructions must be stored in a special file known as a Dockerfile.

image.png

Who is this for?

Do you find yourself in a position where you ask yourself ok I've understood what docker does, but how do I build a simple docker image and publish it on docker hub. Then you have come to the right place!!๐Ÿ˜€

Python Setup

Assuming you have already installed python create a python file and paste the below code in that file

ip1 = int(input("Enter the first number:")) 
ip2 = int(input("Enter the second number:"))
choice = int(input("Enter the operation needs to be performed: 1.Add 2.Sub 3.Mul 4.Div"))    

def add(num1, num2):
    return num1+num2
def sub(num1, num2):
    return num1-num2
def mul(num1, num2):
    return num1*num2
def div(num1, num2):
    return num1/num2

if choice == 1:
    print(add(ip1, ip2))
elif choice == 2:
    print(sub(ip1, ip2))
elif choice == 3:
    print(mul(ip1, ip2))
elif choice == 4:
    print(div(ip1, ip2))
else:
    print("Invalid choice")

Writing the dockerfile

FROM ubuntu:latest

RUN apt update && apt upgrade -y

RUN apt install python3 -y

COPY . /app

WORKDIR /app

CMD ["python3", "main.py"]

Building and Pushing Image to DockerHub

Now head over to the current working directory and open the terminal and type this command to build the image.

docker build -t <repo-name/app-name-demo:tagname>
# An Example would be
docker build -t user10/calculator-demo:v1

On issuing command, you should see your container

docker images

image.png

You can run the docker container by using

 docker run -it repo-name/app-name-demo:tagname

Now push this docker image to the dockerhub by typing this command

 docker push repo-name/app-name-demo:v1

Make sure you have given the same docker image, app and tag name while building and pushing. You might stumble across this error if give a wrong name.

image.png

If you have made it till here kudos to you๐ŸŽ‰๐ŸŽ‰๐Ÿ˜€

Thank You

ย