Docker Basic Operations & Installation

Yatin GambhirYatin Gambhir
3 min read

Let's continue our journey of DevOps and learn how to install Docker on the Ubuntu machine. You can dual-boot your existing system, create a virtual machine using different hypervisors, or run a virtual machine on the cloud platform (DO REFER MY PREVIOUS BLOGS).

In this blog, we learn how to start working on Docker Engine and creating containers.

In the previous blog, we learned the basics of Docker, You can read it here - About Docker

Installing Docker on Ubuntu Machine

Step 1 - Open the terminal by pressing "Ctrl + Alt + T".

Step 2 - Enter the below command on the terminal screen to install Docker. After entering the command press "Y" on the prompt screen, to begin the installation.

sudo apt install docker.io

Step 3 - Next, enter the below command to install all the dependencies & packages of Docker.

sudo snap install docker

Step 4 - You can verify the installation by typing the below command in your terminal.

docker --version

Now we have successfully installed Docker into our machine. ๐Ÿฅณ๏ธ๐Ÿš€

Note - After installing docker on your Ubuntu Machine use "sudo" before typing any command in the terminal to play with docker to use the root privileges.

Example - sudo docker ps

Installing Docker on Windows

You can install Docker Desktop on a Windows machine by going through the official website of Docker - Installing Docker on Windows.

It's an easy process to install the executable file of Docker on a Windows Machine.

Commands in Docker

  • To get the list of docker images.

      docker images
    

  • To get the list of containers created in Docker.

      docker ps -a // Gives the list of all the containers created
      docker ps // Gives the list of only active conatiners
    

  • To remove a docker container

      docker rm "<conatiner_id>"
    
  • To remove a docker image

      docker rmi "<image_id>"
    
  • To remove a docker container and to stop a container

      docker stop "<container_id>"
      docker rm "<container_id>"
      ## First stop the conatiner then remove it
    

Deploying a basic program using Docker

Step 1 - Write a simple "Hello World" HTML code using any editor.

Step 2 - Now write the DockerFile and deploy the HTML page on the nginx server. Create a new file in the same directory as "Dockerfile" without any extension.

Step 3 - After creating the Dockerfile open the terminal in the same directory only and build the Docker Image by typing the below command.

docker build -t "image_name" .

Now the image has been created from the Dockerfile. You can check using the "docker images" command.

Step 4 - Now we have to create a container in which our image will be deployed.

docker run -itd --name "<name_of_container>" -p 8080:80 "image_name"

Now our container is also deployed on the Docker engine.

Step 5 - Redirect to the URL of localhost with the port you have written in the above command with the file name in the Dockerfile (http://localhost:8080/index.html)

So this is how we can deploy a basic container and deploy a simple website using the nginx server. Now stop the container and remove it using the above commands.

That's a wrap-up to this blog. Explore more on this topic. The next blog will soon be published, Stay Tuned.

Happy Learning ๐Ÿš€

1
Subscribe to my newsletter

Read articles from Yatin Gambhir directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yatin Gambhir
Yatin Gambhir