Basic Docker commands

Dhruv Saksena
3 min readApr 21, 2021

Docker is indeed a great platform. If you are dealing with high traffic and need a scalable solution, then Docker is a great tool to horizontally scale your services.

How many times we deal with a situation where something is running in the local environment but failed in production post-release. In majority of cases, we miss an environment config or folder creation or software version mismatch. Also, whenever we are scaling our services we build new machines and miss either a software installation, config, or library.

In a nutshell, Docker resolves all these problems with the help of containerization. Now, rather than shipping individual libraries, config and software, you ship the entire container which is less error-prone.

Below are the basic terminologies-

image — It’s a set of instructions to create a container on the Docker platform.

container — From the docker image, we create a container which is an environment where our application will execute.

DockerHub — A repository where have many developers have contributed their systems as docker images. One can download these images and easily construct containers to understand the application.

  1. Get an image from DockerHub
sudo docker pull centos

2. Execute a container from an image name

sudo docker run -it centos

3. Check the currently running containers

docker ps -a

4. List the current containers in the Docker platform

docker ps -a

5. Stop/Start a docker container

docker stop <containerId> 
docker start <containerId>

6. Execute a command on a running container

docker exec -it <<container-id>> bash
Ex: docker exec -it 95086b7e2d47 bash

7. Commit a docker image to a repository

docker commit <<image-id>> dhruv/centos-dhruv
Ex:docker commit dc2d3bedf1aa dhruv/centos-dhruv

8. Remove a container

docker rm <containerId>

9. Remove a Docker image

docker rmi <image-id>

10. List the Docker images

docker image ls

11. Run a container from an image-id

docker run -i -t <<image-id>> bash
Ex: docker run -i -t 344d9a1ec989 bash

12. Clean-up all the stopped containers, un-unsed networks, dangling images and all build caches

docker system prune

13. Build an image from a DockerFile.(File named “Dockerfile” is present in the folder where the command is being executed)

docker build .

14. Run the docker container in detached mode

Here -d is used to run it as a separate process altogether. As an output of this command is the containerId

docker run -d -i -t <container-id>

15. Port mapping in docker container

We can map container ports to host port using -p flag.

The syntax is host_ip:host_port:container_port

docker run -d -p 8080:8080 -i -t <container-id>

16. Listing docker networks

docker network ls

17. Inspect Docker network

docker inspect <network-id>

18. Execute an image with network mode as host

docker run --rm -d --network=host --name my_nginx nginx

19. Docker with MacOS

MacOS doesn’t support networking with hos using the above command.

If you want to access localhost from docker container, just replace localhost with host.docker.internal

Ex:mongodb://admin:123456@host.docker.internal:27017/db-name?ssl=false&authSource=admin

20. Give Docker image a name during build

docker build -t <<image-name>> . 

21. Remove all unused docker volumes

docker volume prune -f

22. Build docker image from a specific file-

docker build -f dockerfile.test .

23. Docker space details

docker system df
Output for docker system df

24. Space occupied by a docker process

docker ps --size

25. Getting memory and cpu usage of docker containers

docker stats

26. Setting ulimit in docker containers

In the example below, we are disabling core dumps

docker run --ulimit core=0 --rm -it bash

27. Tailing docker logs

Below command will tail last 10 lines in the docker container

docker logs -f --tail 10 <<container-name>>docker logs -f -n 100 <<container-name>>

28. Listing dangling docker volumes

docker volume ls -qf dangling=true

--

--

No responses yet