You can effortlessly enhance your docker skills by mastering some common docker commands.
Whether you are a beginner or looking to expand your knowledge, understanding these commands will significantly improve your docker experience.
In this article, we will provide a comprehensive guide on important docker commands that will streamline your workflow and enable you to efficiently manage your containers.
By following our step-by-step instructions and examples, you will be well-equipped to navigate the world of docker with ease.
Docker Image Management Commands
If you want to manage docker images efficiently, you need to familiarize yourself with the following commonly used docker image management commands:
The images will be downloaded from a repository. Default repository is docker hub but you can configure it to use your own private repository.
1. docker images
This command displays a list of all the images currently stored or downloaded on your local system. Example,
docker images
Example output will be
REPOSITORY TAG IMAGE ID CREATED SIZE
java latest 406bsa29d5ab a moment ago 1.089 GB
<none> <none> g456274db6ce 24 hours ago 1.089 GB
2. docker rmi
Use this command to remove one or more images from your local machine. Its syntax is
docker rmi image_id
3. docker tag
This command is used to add a new tag to an existing image. Example,
docker tag 406bsa29d5ab codippa
4. docker save
This command saves an image to a tar archive so that it can be shared or transferred from one device to another.
Example,
docker save -o image.tar 406bsa29d5ab
5. docker load
This command is used to load a docker image from a tar archive file.
This command allows you to import an image that has been saved using the docker save
command. Example,
docker load /path/to/image.tar
This command will import the docker image contained in my_image.tar
into your local docker environment, making it available for use.
By mastering these docker image management commands, you will be able to efficiently handle docker images on your system.
Docker Container Lifecycle Commands
Now, let’s probe the important docker commands that manage the lifecycle of containers.
1. docker run
This command creates and starts a container from a specified image. Example,
docker run 406bsa29d5ab
You can also set environment variables while running an image and the value of those variables may be used by your application to make it more dynamic.
Environment variables may be set using -e
flag with docker run
as shown below.
docker run -e DB_HOST=127.0.0.1 406bsa29d5ab
2. docker ps
Lists all running containers. Example,
CONTAINER ID IMAGE COMMAND CREATED STATUS
s91c8831a4c9 406bsa29d5ab “/entryPoint.sh” 5 days ago Up 3 days
ard323a3c8ns g456274db6ce “/start.sh” 24 hours ago Up 2 hours
docker ps
will list only running containers.
To list both stopped and running containers, use -a
flag as docker ps -a
.
3. docker stop
Stops one or more running containers. Example,
docker stop s91c8831a4c9
where the value after stop is container id to be stopped.
4. docker rm
Removes one or more stopped containers. Example,
docker rm s91c8831a4c9
5. docker restart
Restarts one or more containers. Example,
docker restart s91c8831a4c9
6. docker cp
docker cp
command can be used to copy a file from your local system into a docker container and vice-versa.
Its syntax is
docker cp <path to local file> container_name:<destination path in container>
Example,
docker cp /home/user/example.txt s91c8831a4c9:/home/app/example.txt
Similarly, you can also copy a file from docker container to your local system by interchanging second argument with first.
Mastering these docker container lifecycle commands will empower you to efficiently manage your containers throughout their lifecycle.
Docker Registry and Repository Commands
Unlike the local docker daemon, which stores images and containers on the host machine, docker registry is a service for hosting and distributing docker images.
docker Repository is a collection of related docker images tagged with a specific version.
Below are some commonly used docker commands related to interacting with docker Registry and Repository.
1. docker pull
This command is used to download images from docker Hub or any other registry. Example,
docker pull ubuntu
2. docker push
Push an image or a repository to a registry. Example,
docker push myrepo/myimage:latest
3. docker login
Command to log in to a docker registry. Example,
docker login docker.io
As stated earlier, if you do not issue this command, default registry is docker hub.
4. docker logout
Command to log out from a docker registry. Example,
docker logout docker.io
These commands are necessary for working with remote docker registries, managing images, and interacting with repositories to deploy and share docker applications effectively.
Conclusion
So, with these commonly used docker commands and their syntax, explanations, and examples in mind, you are well-equipped to efficiently manage your docker containers and images.
Whether you need to pull a new image, stop a running container, or rename a container, these commands provide you with the tools to streamline your docker workflow.
By mastering these commands, you can effectively navigate the world of containerization and harness the power of docker for your development and deployment needs.