7 Basic Docker Commands for Beginners | Tutorial
Many of the web applications built-in Python uses docker containers to deploy and run the application. The container creates the independent environment required for running the application. Cloud hosting services like AWS, Microsoft Azure use the docker container to run applications.
Docker implication is in high demand. Being a developer, learning docker, and adding new skills in your development stack can open many doors and opportunities.
In this tutorial, we will see all essential required to learn docker and basic docker commands for beginners.
What is Docker?
Docker is the most popular and widely used container for deploying and running projects.
There can be multiple services running in the container. And each of the services is associated with the unique container id.
Each of the services will be running independently.
If you are new to the docker, it’s okay not to be strong. But, at least learn the basics and keep reasonable knowledge so that you can use the docker whenever project demands.
I learned the docker for deploying and running my web application which is build using Python Django project.
Basic Docker Commands for Beginners
These commands will be very useful while working on a docker container. After learning these command you will never feel like beginners. 😀
1. How to get the list of all the running docker services?
Run the following command in your Linux terminal.
sudo docker ps
It will list out all the docker services running on your system.
Note: If your current user is not super user, use
sudo keyword while running docker commands.
Usually, this command is used to get the container ID for the particular service. This container ID is useful for running other docker commands. We will see these commands below.
If there are a lot of services running in your container, it’s not easy to search one from the listed services and get the container ID.
If you are searching the container ID of the specific service, use the Linux
grep command along with the
docker ps command.
sudo docker ps | grep <service_name>
It will give the detail about specific service running in the container along with the container ID.
2. What is a docker image?
To run any service in the docker container, you need to load docker image.
This docker image contains the actual program which is required to run the service.
You can build multiple images for any docker service. And then load the image of your choice into the docker container.
3. How to create and update docker image?
Get into the project code and run the following command.
sudo docker build -t <docker_image_name> .
You can give any name to the docker image.
Now you have to update this docker image into the container for docker service.
sudo docker service update --image <docker_image_name> --force <docker_service_name>
You can get docker service name from the
docker ps command.
Sometimes you need
force option to stop the running docker service and update the image.
You can also set the environment variables while updating the docker service image.
sudo docker service update --image <docker_image_name> --env-add <env_variable>=<env_variable_value> --force <docker_service_name>
This environment variable can be used by the docker services.
4. How to get the list of available docker images?
Over the time, while working on project, you might end up creating lot of images. Run following command to get the list of all the docker images.
sudo docker images
You can load any of the images and run them into the container.
Also make the habit of deleting unwanted images.
5. How to read docker logs?
After updating docker image into the container, you might be interested if the service is running properly.
You can get the logs for any of the service in the docker container.
docker logs command with the
-f option and container ID.
sudo docker logs -f <container_id>
This logs are very useful for debugging purpose.
6. How to run command inside the docker container?
Here is the basic syntax to run any command inside the docker container.
docker exec -it <container_id> <command_to_execute>
With this, you can run any Linux command inside the container.
7. How to open the Linux shell prompt (terminal) inside the docker?
exec command we can open
bash or any shell prompt.
Opening sh prompt
docker exec -it <container_id> sh
Opening bash prompt
docker exec -it <container_id> bash
After that, you can try running any basic Linux commands just like we do in our Linux system.
Most of the time I open the Linux shell inside the container to modify the docker image.
Modifying the project code, creating an image, and then updating docker service is a really cumbersome job.
If the change is small, I can open the container Linux shell and modify the code inside the docker using
vi editor command. There are multiple options for editing code in Linux. And then restart the docker service.
Hope you find these basic docker commands for beginners useful. If you have any doubts or questions related to the docker container, let me know in the comment.