What is Docker

Docker is a platform to build a development running application package and run the same behavior in any of the system or environment, without manually installing or configuring the dependencies.

And that package of all files, directory, source codes, environment variables, binaries dependency, run time environment are called image, a Docker Image

 

What is Container

Container is a running instance of the Docker Image.

Once have a Docker Image which consist of all necessary resources together, and when we run that image on any system or machine, that running instances are called Docker Container.

 

 

docker version

This command used to check the docker version installed.

 

docker build -t <tag an image name> <directory where Dockerfile exist>

This command is used to create a Docker image, by referring the instruction given in the Dockerfile. If the docker build command trigger from the same directory where Dockerfile exist, then replace the <directory where Dockerfile exist> with period/ dot symbol


docker images / docker image ls

These both commands are used to list the available Docker images in the system

 

 

docker run <image name>

This command used to run the docker image, this generate a running instance called container

This by default run in the attached mode, means when the terminal is close, the running instance or container will auto terminate.

This run command also checks if the image available in local repository or it will pull from the remote repository like Docker Hub, and start the image.

 

docker run -d <image name>

This command used to run the Docker image to create a container in the detached mode, that means even if the terminal gets closed, the container will be in running state.

 

docker run -d -p5000:3000 <image name>

This command used to run the Docker image in detached mode with the port flag attached. The 5000 define the PORT of the container, and the 3000 define the PORT of the host machine, where the container are running.

 

docker run -d -p5000:3000 -name "tag name" <image name>

This command run the Docker image and explicitly also define the name of the container, rather than assigning it randomly.

 

docker ps

This command used to list the available running container

 

docker ps -a

This command used to list all running and stopped container

 

 

docker pull <image name>

This command pull the Docker image from Docker Hub or any other repository, which holds the Docker image.

 

docker push <image name>

This command push the Docker image to remote repository

 

docker stop <container id | container name>

This command used to stop the running container.

 

docker start <container id | container name>

This command used to start the stopped container


docker rmi <image name>

This command remove the image from the Docker

 

docker rm <container id | container name>

This command remove the container, removing container by default removes entire configurations or data associated with the container.

 

 

docker logs <container id | container name>

This command used to view the logs of the container.

 

docker exec -it <container id | container name> /bin/bash

This command allow to login to the bash inside the container, this allow to execute certain command directly on the container instance.

-it refer to the interactive mode

/bin/bash define the provision to get into the container

 

docker login

This command allow to login to the Docker in the local development, using Docker credentials

 

 

 

What is Docker network

Docker network is an environment, where multiple Docker container interact or communicate with each other internally.

Since Docker creates an isolated environment, so the container which resides on the same network can interact without need of any explicitly hosted on PORT

docker network ls

This command list out the available network in the Docker

 

docker network create <network name>

This command will create a new network in the Docker environment.

 

Docker compose

Docker compose allow to execute more Container in very easy and optimal way.

Lets assume we built an application, which runs over 10 or 15 containers. Ex one container for Mongo DB, One container for Node JS etc.

Now managing all those container one by one using the docker run command and mapping all if required with the same Docker network and exposing them to certain PORT, or assigning them certain environment variables, are all possible with the various flags available with the Docker run command. But this will be very hectic to do or manage for all containers.

So to make it simplify and easy to manage, Docker compose comes in the picture. Where it writes all certain command in the yaml file. And then using Docker compose command will create or terminate the instance in one go. Also it will be easy to change any configuration, just edit on the yaml file and rerun the compose.

 

Docker-compose command used to perform the compose operation, this command also comes up with the Docker installation.

docker-compose -f <yaml file path> up

This command reads all the container specification given in the yaml file, and up the container or run the container.

 

docker-compose -f <yaml file path> down

This command reads all the specification define in the yaml file and terminate the running container

 

Docker volumes : for data persistent in container to delete

Whenever we remove the container or restart it, all the data and configurations are lost. The data are not persistent by default. And to keep data persistent even after stop or terminate the container, can use of Docker volume

 

What is Dockerfile

Dockerfile is a special file which has instruction to Docker, while create an image. It will be consist of all the instruction that a container to follow once we run the Docker image.