Introduction to Docker
Vishal Ranaut
Full Stack Developer | Expert in JavaScript, TypeScript, Node.js, React & Angular | AWS & Docker Specialist | Passionate About Web 3.0 Innovation
Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine.
Difference between Docker Containers and Virtual?Machines
1. Docker Containers
2. Virtual?Machines
Important Terminologies in Docker?
1. Docker?Image
2. Docker Container
3. Docker?file
4. Docker?Engine
5. Docker Hub
Installing Docker on?Ubuntu
1. Remove old version of?Docker
$ sudo apt-get remove docker docker-engine docker.io containerd runc
2. Installing Docker?Engine
$ sudo apt-get update
$ sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
Check if docker is successfully installed in your system
$ sudo docker run hello-world
Create an application in?Docker
1. Create a folder with 2 files (Dockerfile and main.py file) in?it.
2. Edit main.py with the below?code.
领英推荐
print("Docker and GFG rock!")
3. Edit Dockerfile with the below commands.
FROM python:latest
COPY main.py /
CMD [ "python", "./main.py" ]
4. Create a Docker?image.
Once you have created and edited the main.py file and the Dockerfile, create your image to contain your application.
$ docker build -t python-test .
The ‘-t’ option allows to define the name of your image. ‘python-test’ is the name we have chosen for the image.
5. Run the Docker?image
Once the image is created, your code is ready to launch.
$ docker run python-test
Push an image to Docker?Hub
1.?Create an Account on Docker?Hub.
2.?Click on the “Create Repository” button, put the name of the file, and click on “Create”.
3.?Now will “tag our image” and “push it to the Docker Hub repository” which we just?created.
Now, run the below command to list docker images:
$ docker images
The above will give us this result
REPOSITORY TAG IMAGE_ID CREATED SIZE afrozchakure/python-test latest c7857f97ebbd 2 hours ago 933MB
Image ID is used to tag the image. The syntax to tag the image is:
docker tag <image-id> <your dockerhub username>/python-test:latest
$ docker tag c7857f97ebbd afrozchakure/python-test:latest
4.?Push image to Docker Hub repository
$ docker push afrozchakure/python-test
Fetch and run the image from Docker?Hub
1.?To remove all versions of a particular image from our local system, we use the Image ID for it.
$ docker rmi -f af939ee31fdc
2.?Now run the image, it will fetch the image from the docker hub if it doesn’t exist on your local machine.
$ docker run afrozchakure/python-test
Conclusion:
So you have learned about the basics of Docker, the difference between Virtual Machines and Docker Containers along some common terminologies in Docker. Also, we went through the installation of Docker on our systems. We created an application using Docker and pushed our image to Docker Hub. Lastly, we learned how we could remove a particular image from our local system and later pull the image from Docker Hub if it doesn’t exist locally.