All About Docker and Containers!

All About Docker and Containers!

ยท

3 min read

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.

Topics covered in this section are listed below ->

Containers

Containers can be defined as packages of software or code with their libraries and dependencies, Containers can be easily shipped from one environment to another.

Example to understand Containers - > Two developers are working on a java project, they both are working on different versions of JDK, one with JDK 8 and the other with JDK 11, so the first developer shares the source code with another developer, unfortunately, second Developer can't compile and run the source code, because of missing java libraries and files, So in this case, Containers comes into the role package up the Source code with all the libraries and dependencies which are required to compile and run the code in any environment and ship it to a different environment.

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 .

Did you find this article valuable?

Support Sayam by becoming a sponsor. Any amount is appreciated!

ย