Let's look at all the necessary Docker commands

Let's look at all the necessary Docker commands

Hey guys, it's been a while since my last blog about Linux commands. And now this blog is all about the necessary Docker commands.

Note:- This blog doesn't include all the theory parts but only the main definitions and this is exclusively for commands used in Docker.

Let's get started

What is Docker?

Docker is a container platform that allows you to build, test and deploy applications quickly. A developer defines all the applications and their dependencies in a Dockerfile which is then used to build Docker images that define a Docker container. Doing this ensures that your application will run in any environment.

Basic Docker commands:

docker --version  - displays the version of the docker we are using
docker version --format '{{json .}}'  - displays all the information of our   
                                        docker in json format
docker info  - info about the docker version
docker --help  - lists all the docker commands
docker (command) --help  - gives detailed information about a particular                                     
                           command

What is an Image?

A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template. A Docker image contains application code, libraries, tools, dependencies and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container.

docker run hello-world  - if hello-world is already locally present in the  
                          system then it won't pull. But if it isn't locally  
                          then it pulls from docker hub registery and 
                          downloads the image

docker images  - lists all the docker images which are locally present

docker image ls  - lists all the docker images which are locally present

docker run -it ubuntu  - runs the ubuntu image which is already present   
                         locally and then the terminal in the container opens 
                         which is an interactive shell

docker pull ubuntu:20.10  - downloads the image directy instead of running 
                            it. [20.10 is one of the previous version of the 
                            ubuntu]

docker ps  - lists all the containers that are currently running

docker run (image)  - it is not an interactive shell

docker run -d (image) - it runs in the background but not an interactive 
                        shell

docker run -it -d (image) - it runs in the background as an interactive shell 
                            but gives random names

docker run -it --name=chrisredis -d (image)  - this command would solve the 
                                          above problem with a specified name

docker search (image)  - displays all the locations where the specified image 
                         is present

docker images -q  - lists all the image Ids

docker rmi $(docker images -q) -f  - will remove all the stopped images at a 
                                     time

docker rmi (container Id) -f  - will remove specific images

What is a Container?

A container is a runnable instance of an image. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine.

docker container ls  - lists all the containers that are currently running

docker container exec -it (container Id) bash  - attaches bash with the 
                                                 running container

docker stop (container Id)  - stops the running containers

docker kill (container Id)  - stops a running container 

docker ps -a  - lists all the stopped containers

docker rm (container Id)  - removes the specified stopped container from the 
                            list

docker inspect (container Id)  - displays all the information about the 
                                 running container

docker logs (stopped container ID)  - displays all the commands used in that 
                                      container session with their outputs

docker container prune -f  - deletes all the stopped containers 

docker run -d -p 8060:80 nginx  - accessing a container locally(local  
                                  host).Forwards all the requests of 
                                  localhost to standard port 80 of nginx 
                                  which is called port forwarding

docker stats  - displays all the memory consumption details of containers

How to create our own Docker Image?

First, create a Dockerfile with some instructions in it.

Dockerfile is a simple text file that consists of instructions to build Docker images.

touch Dockerfile
vi Dockerfile

and then in the vim editor give some instructions like:

FROM ubuntu - (ubuntu is a base image)
RUN apt-get update
CMD ["echo", "Hello, Iam Chris"]  - executable which has to be run

Now, for building an image out of Dockerfile use the following command:

docker build -t demoimage .  - demoimage is the image name and '.' is the 
                               current directory

Note: Avoid keeping the current directory as Desktop as it has many files in it which takes a lot of time to transfer into the daemon server.


What are Docker Volumes?

Docker volumes are a widely used and useful tool for ensuring data persistence while working in containers. Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.

docker volume create  - creates a volume

docker volume inspect  - Displays detailed information of the volume

docker volume ls  - lists all the volumes

docker volume prune  - Remove all unused local volumes

docker volume rm  - Remove one or more volumes

As I've mentioned at the start of the blog that this blog will only cover the necessary commands in Docker but doesn't include theory parts except for some important definitions.

For detailed theory information kindly go through Kunal Kushwaha's blog on Docker.

Thank you guys for reading. I hope this blog helped you in some way. I'll be back with many more blogs on MyDevOpsJourney.

You can contact me:

Twitter - https://twitter.com/Chris__Jonathan

LinkedIn - https://www.linkedin.com/in/chris-jonathan/