Skip to main content

Command Palette

Search for a command to run...

All About Docker and Containers!

Updated
3 min read
All About Docker and Containers!
S

Welcome to my blog 👋🏽

I'm a Tech Enthusiast with a passion of learning!

I write about Programming and Productivity Tips ✅

Prerequisite -

  1. Before jumping to the article, create a personal docker hub account because it will be needed in later sections.

  2. Install Docker in WSL (Windows Subsystem for Linux) Ubuntu.

  3. Docker Desktop.

Topics covered in this section are listed below ->

Containers

A Docker container is a lightweight, portable, and isolated environment used to run applications. It packages your app along with all its dependencies, so it runs the same everywhere — on your laptop, a server, or in the cloud.

Example to understand Containers - > Imagine two developers working on the same Java project. One is using JDK 8, and the other is on JDK 11. When the first developer shares the source code, the second developer runs into issues — the code fails to compile and run due to missing libraries and version mismatches. This is where containers come in. By packaging the source code along with all its required libraries, dependencies, and the correct JDK version, containers ensure that the application runs consistently across any environment — regardless of what’s installed on the host system.

What is Docker?

Docker is an open-source platform that is used to deploy and run applications using containers, Docker helps you isolate your applications which can be run in any environment.

Containers vs Virtual Machines

Docker Commands

verify the version of the docker installed

docker version

  1. list all the docker images

     docker images
    

  2. list all the running containers

     docker ps
    

  3. log in to the docker hub

     docker login
    

  4. pull image from Docker hub

     docker pull httpd  // syntax : docker pull image_name
    

  5. run containers in background

     sudo docker run -d httpd
    

  6. stop running container

     docker stop container_id
    

  7. list all exited Containers

     docker container ls -a
    

  8. tag a specific image

     sudo docker tag image_name docker_hub_username/image_name:tag 
    
     -> docker nginx sayam1007/nginx:10
    

  9. Push Image to docker hub

     Tag the image first 
     -> docker tag nginx sayam1007/nginx:10
     then push it
     -> docker push sayam1007/nginx:10
    

  10. Remove any image

    docker rmi -f image_name
    
    -> docker rmi -f httpd
    

Running containers to a specific port ->

to run a container at a specific port -p flag is used.

docker run -d -p 80:80 nginx

Building Docker image and Pushing to Docker hub

  • Create a source code that you want to containerize.

  • shell script code that prints hello world

      -> script.sh
    
      #!/bin/bash
      echo "Hello world"
    
      -> Dockerfile
    
      FROM bash
      COPY script.sh /
      RUN chmod +x script.sh
      CMD ["bash","/script.sh"]
    

After Creating Docker file and bash script , lets build the image

docker build -t image_name_you_want_to_give .
docker build -t bash .