Most of the time, I forget the Docker commands and their usage. Every time, I have to search for my notes to see them or any online resource, maybe a blog to remember. So, here is the blog in which I have defined the most useful commands in Docker used in day-to-day applications. I have tried to use real-life examples in the blog to make it more understandable.
First, understand what the Docker is and what it does and helps.
What is Docker?
In simple terms, Docker lets you create a mini machine inside your main computer. These mini-machines are called containers and you can run anything on these containers.
Suppose you want to test anything on your computers without affecting them. In these cases, Docker is the most useful choice. Using a simple command, you can start your container and run anything.
When setting up an open-source codebase or any project on GitHub, you first need to install all the necessary dependencies. After that, if the project uses third-party services, you should also need to configure those. Next, you have to run your local database and start the project on your computer. Keep in mind that after completing the setup, you may never use it again, and it will remain on your drive, occupying some space. Additionally, if you mismatch any required library versions for the project, you will fail to run it locally.
Docker solves this problem, and with one command you can start your container with all the required dependencies, databases etc. required for the project. And, after you don’t use them you can simply delete that container.
Now, that you have understood what Docker is and what problems it solves. Let’s understand
What is an image in Docker?
Let us take an example to understand, Suppose you have a coffee shop (or any shop you like) and you want to open its franchises in other cities. To ensure consistency in all the locations, you will have to create a blueprint of the shop, containing all the recipes, and everything needed to replicate that shop.
A Docker image is like a Blueprint that contains everything you need to set up an open-source codebase or any project on a container. To set up anything on your container in Docker you a list need commands to run it, The Docker image contains all these commands to set up and run it in the container.
These Docker images are already created by the company and you can run them on your computer using a single command.
Docker Hub is the place where you will find the images required for the project. Later on, we will see how we can set our own image in Docker and deploy it on Docker Hub.
Now, you have to understand what is Docker and Docker images. Moving on let’s understand how to set up a Docker container and run images on it.
Commands are used in docker to create, run, use etc.
for running these commands make sure you have docker installed on your machine
and it is running on your machine. You can download it from their official website.
You can also use Docker GUI for running these commands. But I like to use a terminal for all these commands to feel more like a hacker 😃.
1)For running an image inside a container
- docker run <image_name>
: Docker command to run an image in the container.
This command will start a container on your computer and run the image you have provided.
Example:-
this command will start your MongoDB database inside a container on your machine.
After that, you will see some logs. Don’t worry; they are just the Docker logs for the running container.
- docker run -d -p <pc_port>:<container_port> <image_name>
:- Docker command start an container in detach mode (-d) and with port mapping (-p). So, you can easily connect to the MongoDB database.
- d:- detach mode will let you continue to use the terminal while docker will run in the background, no logs will be shown only an ID of the container is returned.
-p:- port mapping, to hit your computer port to the container port, we specify our port to hit that container port.
Note: Your container is a mini-machine running on your computer. It has its own file system, networks, etc. To access that port from your computer, you need to specify port mapping.
Here is an image of mine running a MongoDB container using detach mode and port mapping. The left side of the port is your computer port, and the right side of the port is the container port. You can choose any port you like.
Now, you can easily connect to the MongoDB database, your link will be mongodb://localhost:27017.
- For viewing all your currently running containers on your computer
- docker ps
:- Docker command that will show all your currently running containers.
Example:-
- For stopping a currently running container
- docker kill <container id>
:- Docker command that will stop your running container on your machine.
Note: you can copy the container ID using the docker ps command
Example:
It is a good idea, to stop an unused container on your machine for memory optimization. If you shut down your computer it will automatically stop, and you can start it again.
- To get the list of all the images in the Docker
- docker images:- Docker command that will show all the images downloaded from Docker Hub or created by own.
Example:- An image of mine showing the list of the images that are stored on my PC.
If you are new to docker, you will only have some images, maybe
- To remove a container or an image from a PC.
- docker rmi <image_id>
:- Docker command for removing an image from a PC.
You can specify multiple IDs to remove several images at once.
Example:-
To create your docker image
-
docker build -t <specify_image_name> .
: Docker command to build your own imageNote:- For building your own image a folder must have a .dockerfile in it.
A step-by-step guide to creating your own Docker image.
a) Creating a .dockerfile:- Create a new project directory or in the existing project directory (if you don’t have one just clone this project from Github:- github.com/Piyush5784/express-app.git )
- create a new “.dockerfile” in your root directory
- add this content to your .dockerfile
FROM node:20-alpine # select any node version, i have choosed an lighter version of node-apline
WORKDIR /app # select root dir /app or /any
COPY . . # copy all the files to the container
RUN npm install # run install command for the container
RUN npm run build # covert typescript to javascript file
EXPOSE 3000 # expose port, declares that the application running in the container will listen on port 3000 using TCP
CMD ["node","dist/index.js"] # for running the project inside container
Additionally, add a “.dockerignore” file in the root directory to exclude unnecessary files. List the files you don’t want to include in the container.
Example:-
b) Run the Docker command to build an image
- docker build -t myapp .
:- This command will create your image with the name of myapp
c) Run the myapp image on your PC
- docker run -p 3000:3000 myapp
: This command will run the myapp image into a container.
Now, you have successfully created your own image.
In the next blog will see,
How we can optimize Docker images using caching?
Learn about layers in Docker.
How to communicate two containers using networks?
How to make data consistent in Docker using Volumns?