Docker commands –
Are you new to Docker & wondering how to manage Docker container? Don’t worry, Docker commands can help you achieve this! Docker is a popular platform that allows developers to package their applications, dependencies, and configurations into containers that can run seamlessly in different environments.
Using Docker commands, you can easily create, run, stop, delete, and manage Docker containers. These commands can help you automate and streamline the process of deploying and managing your applications in a containerized environment.
What is Docker?
Docker is a free and open platform that provides a comprehensive experience to developers working on various aspects of software development. It is a containerization platform that allows developers to package code into different deployable units called containers. Docker Engine is the software that handles the hosting of containers. Docker, a subset of the Moby project, helps developers create, run, and deploy containers on servers and in the cloud. Containers contain a builder, engine, and orchestrator to deliver seamless applications that run in any environment.
Read Also – Jenkins for beginners
We usually start by finding the installed version of Docker we’re working on. Here’s how to find it –
$ docker --version
The “docker search” command searches for specific images through Docker Hub. This command returns specific information, including image name, description, automatic, official stars, etc. Here’s how to use it –
$ sudo docker search mysql
Display system-wide information about Docker.
$ docker info
As the name suggests, this command pulls a specific image from Docker Hub. All you need to do is use the ‘docker pull’ command with the name of the image. Here is an example of pulling an image without using tags.
$ sudo docker pull tomcat
Tags are used to identify images inside Docker Hub. If you don’t specify any tags, it will use the ‘:latest’ tag by default.
We can use ‘-all-tags’ command to pull all images from the repository for multiple images.
List all locally available Docker images.
$ docker images
Remove one or more Docker images.
$ docker rmi image:tag
This command is used to create a container from an image. Here is how to do it –
$docker run --env MYSQL_ROOT_PASSWORD=my-secret-pw --detach mysql
The ‘–detach’ option runs the container, and the ‘–env’ option is used to set the mandatory variable. If you do not use the ‘–name’ option, the docker will randomly assign a name to the container.
This command is used to list all the running containers in the background. Here is how to do it –
$ docker ps –all
The ‘docker stop’ command stops a container using the container name or its id. Here is how to do it –
$ docker stop [container-id]
This command is used to restart the stopped container. It is recommended to use this after rebooting the system. Here is how to do it –
$ docker restart [container_id]
This command is used to stop the container immediately by killing its execution. While the ‘docker stop’ command helps shut down the container in its own time, the ‘docker kill’ command stops it at once. Here is to use it –
$ docker kill 07b0b6f434fe
This command is used to access the container that is running. Here is how to use it –docker exec -it test_db bash mysql -uroot -pmy-secret-pw SHOW DATABASES;
You have to either provide the name or the id of the container, which is ‘test_db’ in this case. The ‘-i’ and ‘-t’ options are used to access the interactive mode.
View the logs of a running container.
$ docker logs container-id
This command helps you to log into your docker hub. As you try to log in, you will be asked to give your docker hub credentials.
$ docker login
This command is used to create or save an image of the edited container on the local system.
$ docker commit my_container my-new-image:tag
This command helps push or upload a docker image on the repository or the docker hub. Here is how to use it –
$ docker push your-registry.com/your-username/your-image:tag
Docker network ls:
List Docker networks.
$ docker network ls
Docker network create:
Create a new Docker network.
$ docker network create network-name
This command is used to free up some disk space. The image id is used to remove the image while using this command.
$docker rmi [container_id or name]
Docker ps -a
This command is used to know the details of all the running, stopped, or exited containers. Here is how to use it –
This command copies a file from docker to the local system. Here is how to use it –
$ sudo docker cp greatlearning@greatlearning:/home/greatlearning$ sudo docker cp 09ca4feb7tfc:/usr/local/apache2/logs/httpd.pid /home/greatlearning/ [sudo] password for greatlearning: In the above example, we used the docker container with id 09ca4feb7tfc to copy the file ‘http.pid.’ To check whether the file is copied or not, run this command – greatlearning@greatlearning:/home/greatlearning$ ls Desktop Documents example examples.desktop httpd.pid nginx_new.yml nginx.yml
This command is used to check the logs of all the docker containers with the corresponding contained id mentioned in the command. Here is how to use it –
$ docker logs [container_id or name]
This command creates a volume so that the docker container can use it to store data. Here is how to use it –
$ docker volume create
Docker system prune:
Remove all stopped containers, unused networks, and dangling images.
$ docker system prune
Docker container prune:
Remove all stopped containers.
$ docker container prune
Docker image prune:
Remove all dangling images.
$ docker image prune
Docker volume prune:
Remove all unused volumes.
$ docker volume prune
This command will log you out of the docker hub. Here is how to use it –
$ docker logout