Docker Chapter: 1 Basic Commands

ABHISHEK KUMAR
5 min readJul 16, 2020

Please go to this link to install docker or

sudo apt install docker.io
sudo groupadd docker ##For all user
sudo usermod -aG docker $USER
sudo reboot

Docker Commands

  1. The command to check the version of Docker installed.
docker --version

2. To look/search for available docker images from the Docker registry.

docker search nginx

nginx is the image name we are searching for, from the official docker registry. The image will have a basic nginx configuration.

3. To pull docker images from the Docker registry.

After searching for an image, If it is available you can pull it from the docker registry.

docker pull nginx

The above command will pull the nginx — docker image from the docker registry to the local system/server.

4. Listing all the docker images

docker images

The above command will list all the docker images stored locally on the system.

5. Creating / Running docker container from a Docker image.

After pulling the image from the docker registry, We can run a docker container from that image.

docker run -it -d nginx

-d (detached mode)- It will run the docker container in the background.

-it (interactive mode) — means You can execute commands inside the docker container while it is running.

6. To list the actively running docker containers.

docker ps

The above command will list all the containers which are currently running.

7. To list all the docker containers

docker ps -a

The above command will list all the actively running as well as the containers in the stopped state.

8. To stop a Container

If you want to stop a running docker container,

docker stop containerID

9. To start a Container

To start a container that is in a stopped state.

docker start containerID

10. To restart a Docker container

The command to restart the running docker container.

docker restart containerID

11. To login to running Docker container

If you want to execute some bash commands or make configuration changes within the docker container, You can access it using the below command.

docker exec -it containerID /bin/bash

12. To delete the stopped Docker containers

docker rm containerID

The above command will delete the docker container which is in the stopped state from the system.

13. To delete Docker images from the Local system

docker rmi imageID

If you’ are deleting an image, But If there are containers running with those images, You will get the below error.

Error response from daemon: conflict: unable to delete 0901fa9da894 (cannot be forced) - image is being used by running container 3e98722264b4

If you’re not running containers from the docker image on the local system, You should be able to delete it.

14. To check logs of a running Docker container

docker logs -f containerID

-f (–follow) — To follow docker output logs or tail continuously

15. Killing docker containers

The docker stop command will give some time for the container to shut down gracefully, But sometimes It takes so much of time for the container to go to stopped state, In this case, we will run docker kill to stop it immediately.

docker kill containerID

16. Log in to Docker Hub registry (hub.docker.com)

docker login is a command which is used to login to the docker hub registry from the local system.

Once logged in you can push or pull images from local systems to private docker repository.

Before running the docker login command, Make sure you have the credentials in place.

docker login

It will ask for username and password, Once validated, You should get a message as Login Succeeded

17. Removing the Docker hub registry login from the system.

In the previous command, We used a docker login to login to the docker hub registry on the local system.

Run the below command, If you want to remove the docker login from the system.

docker logout

And If you check /root/.docker/config.json file, You can see that the auth is removed from the system.

18. Check active resource usage by each container

docker stats

The above command displays the active Memory and CPU usage of all the containers on the system.

19. Rename a Docker container

When you pull an image from the Docker registry and run a container out of it, It will automatically set a name for a container.

If you want to change the name of the container at any time, Run the below command.

From the below image, You can see the name of the container is abhi_demo

To change the name of the container,

docker rename abhi_demo newname

With the help of above command, We have changed the name of the container from abhi_demo to nodejs

20. To display system-wide information of Docker

docker info

The above command will display information such as the number of total containers, Container running, containers stopped the number of images available on the system, kernel version, plugins, volumes, etc.

21. Inspecting a Docker container

The docker inspects command will list the complete information of the docker container in JSON format.

docker inspect containerID

You can also truncate the output and check based on what needs to be verified. For example, You can get the information only about the networking part (IP address) of the container as shown below.

docker inspect --format='{{ .NetworkSettings.IPAddress }}' containerID

22. Building docker images from Docker file

The docker build command builds docker images from a Dockerfile.

docker build -t imagename .

. represents the directory where Dockerfile is present.

23. Creating new docker images from a Container

If you have made any changes to the containers, You can create a new docker image from that docker using the below command.

docker commit containerID username/imagename:tag

username — The username of the hub.docker.com

24. Pushing Docker images from Local to Docker registry.

docker push username/imagename

--

--

ABHISHEK KUMAR

DevOps/Cloud | 2x AWS Certified | 1x Terraform Certified | 1x CKAD Certified | Gitlab