Docker is a containerization system which packages and runs the application with its dependencies inside a container. There are several docker commands you must know when working with Docker. This article is all about that. If you don’t know what Docker is, then you may take this Udemy beginner course.
Finding the version
One of the first things you want to know is how to find the installed docker version.
Downloading image
Let’s say you need to pull the docker image from dockerhub (docker repository). The following example of pulling the Apache HTTP server image.
Images
List all the docker images pulled on the system with image details such as TAG/IMAGE ID/SIZE etc.
Run
Run the docker image mentioned in the command. This command will create a docker container in which the Apache HTTP server will run.
What’s running?
ps lists all the docker containers are running with container details. As you can see, the Apache server is running in this docker container.
ps -a
List all the docker containers running/exited/stopped with container details.
exec
Access the docker container and run commands inside the container. I am accessing the apache server container in this example. Type exit and press enter to come out of the container.
Removing container
Remove the docker container with container id mentioned in the command. Run the below command to check if the container got removed or not.
Removing image
Remove the docker image with the docker image id mentioned in the command
Restart Docker
Restart the docker container with container id mentioned in the command. Run the command below and check the STATUS parameter to verify if the container started recently.
Stopping Docker
Stop a container with container id mentioned in the command. Run the below command to check if the container is still running or it has stopped.
Starting Docker
This command in docker starts the docker container with container id mentioned in the command. Run the command below to check if the container started or not.
Kill
Stop the docker container immediately. Docker stop command stops the container gracefully, that’s the difference between a kill and stop commands. Run the below command to see if the container got killed or not.
Commit
Save a new docker image with container id mentioned in the command on the local system. In the example below, geekflare is the username, and httpd_image is the image name.
Login
Login into docker hub. You will be asked your docker hub credentials to log in.
Push
Upload a docker image with the image name mentioned in the command on the dockerhub.
Docker network
The following command in docker lists the details of all the network in the cluster. There are several other docker network commands.
Docker info
Get detailed information about docker installed on the system including the kernel version, number of containers and images, etc.
Copying file
Copy a file from a docker container to the local system. In this example, I am copying httpd.pid file inside a docker container with id 09ca6feb6efc to /home/geekflare/ Run the command below to check if the file got copied or not.
Checking history
Shows the history of a docker image with the image name mentioned in the command.
Checking logs
Show the logs of the docker container with contained id mentioned in the command.
Searching image
Search for a docker image on dockerhub with the name mentioned in the command.
Updating configuration
Update container configurations. This shows all the update options. Run the below command to update the CPU configuration of docker container with container id mentioned in the command.
Creating volume
Create a volume which docker container will use to store data. Run the below command if the volume got created or not.
Installing plugin
Install a docker plugin vieux/sshfs with debug environment set to 1.
Logout
Logging out from dockerhub. Conclusion I hope you have got a fair understanding of docker commands by now. Try out those commands in your dev or lab environment to practice and learn. If you are interested in learning Docker and Kubernetes, then check out this online course.