Many web applications whether they are built in Python or any other technologies, use docker containers to deploy and run the application. The container creates the independent environment required for running the application. Cloud hosting services like AWS and Microsoft Azure use the docker container to run applications.
Docker implication is in high demand. Being a developer, learning docker, and adding new skills to your development stack can open many doors and opportunities.
In this tutorial, we will see all the essentials required to learn docker and basic docker commands for beginners.
Docker is the most popular and widely used container for deploying and running projects.
There can be multiple services running in the container. Each of the services is associated with a 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 the project demands.
I learned the docker for deploying and running my web application which is built using a Python Django project.
These commands will be very useful while working on a docker container. After learning these commands you will never feel like a beginner. 😀
Table of Contents
Run the following command in your Linux terminal.
sudo docker ps
It will list all the docker services running on your system.
Note: If your current user is not a super-user, use sudo the 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 for one from the listed services and get the container ID.
If you are searching for 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 details about specific services running in the container along with the container ID.
To run any service in the docker container, you need to load the 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. Then load the image of your choice into the docker container.
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 the docker service.
sudo docker service update --image <docker_image_name> --force <docker_service_name>
You can get the docker service name from the docker ps
command.
Sometimes you need force
the 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.
Over time, while working on a project, you might end up creating a lot of images. Run the 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.
After updating the docker image into the container, you might be interested if the service is running properly.
You can get the logs for any of the services in the docker container.
Use docker logs
command with the -f
option and container ID.
sudo docker logs -f <container_id>
These logs are very useful for debugging purposes.
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.
Using above exec
command we can open sh
, 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 the 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.
Keep learning!