Tips and tricks for docker

3 minute read

Most used docker commands for everyday

  • Docker help
$ docker -h
  • To check docker version
$ docker --version
  • Login to docker
$ docker login 
  • To see list of images
$ docker image ls
  • List of Containers
$ docker container ls
  • See list of running containers
docker ps
  • Restart a container
docker restart <container name>
  • To stop docker container
$ docker stop <container id>
  • Remove all docker images
    docker rmi $(docker images -q)
  • Stop all docker containers
docker stop $(docker ps -a -q)

Remove all docker containers

docker rm $(docker ps -a -q)

Deleting no longer needed containers (stopped)

docker container prune

Deleting no longer needed images

docker image prune

Delete all volumes, which are not used by any existing container

docker volume prune

Same for unused networks

docker network prune
  • To run docker composer
$ docker-compose up
  • Build a docker image
$ docker build -t <any tag name> <directory of the docker file>
$ docker build -t aspnetcore/generator .   
Here aspnetcore/generator is tag name and '.' means current directory
  • To run a docker image
docker run --rm -it -p 8000:80

The sample core application will run on http://localhost:8000. Here, 8000 is host machine port and 80 is container port.

  • SSH into container
docker exec -it <container-id> sh
>>ls (Type ls to see all files and directory)
>>exit (type exit to return from the container)
  • View information about your container using command
    PS C:\Users\mahedee> docker exec -it b73cb9467f5a sh
    # cd ..
    # ls
    app  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  remote_debugger  root  run  sbin  src  srv  sys  tmp  usr  var
    # cd app
    # ls
    Controllers  Dockerfile  HRM.API.csproj  HRM.API.csproj.user  Program.cs  Properties  WeatherForecast.cs  appsettings.Development.json  appsettings.json  bin  obj
    # cd ..
    # ls
    app  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  remote_debugger  root  run  sbin  src  srv  sys  tmp  usr  var
    # cd src
    # ls
    HRM.API  HRM.API.sln  bin  docker-compose.dcproj  docker-compose.override.yml  docker-compose.yml  obj
    # cd bin
    # ls
    # cd debug
    # ls
    HRM.API.deps.json  HRM.API.exe  HRM.API.runtimeconfig.json  Swashbuckle.AspNetCore.Swagger.dll     Swashbuckle.AspNetCore.SwaggerUI.dll  appsettings.json
    HRM.API.dll        HRM.API.pdb  Microsoft.OpenApi.dll       Swashbuckle.AspNetCore.SwaggerGen.dll  appsettings.Development.json
  • Run a command in a running container

$ docker exec -ti 34717 sh -c "echo a && echo b"
$ docker exec -ti my_container sh -c "echo a && echo b"
$ docker exec -ti my_container sh -c "dir"
  • To run bash in a container


$ docker exec -it <container id> bas
$ docker exec -it 2f7 bash

View information about your container user Visual Studio

  • Use Ctrl+Q to activate Visual Studio Search Box, and type containers.
  • Or, Go to View > Other Windows > Containers.

For more information see Use the Containers window

  • Mounting a folder name ‘api’ into the container
$ docker run --rm -it -v ${PWD}:C:\api

Here ${PWD} means current directory of host machine map to the directory C:\api into the container

  • To inspect network and other information of a container
$ docker inspect <container id>
  • Run a docker image
$ docker run --rm -it -p <host port>:<image port> <any tag name>
$ docker run --rm -it -p 8777:80 test2  
Here test2 is tag name of the image which already running
  • Login to docker and push an image
$ docker login 
$ docker push mahedee/simpleapi  
Here mahedee/simpleapi is the tag name of the image
  • Setting the API Port with an Environment Variable inside a container
$ export ASPNETCORE_URLS=http://+:80
  • To see docker event in json format
$ docker events --format ''

Run this command in another windows
  • To detach docker compose file
$ docker-compose up -d
  • To see the log while running docker compose file
$ docker-compose logs -f
  • Stop and remove containers, networks, images, and volumes
$ docker-compose down
  • Remove one or more images
$ docker image rm <repository>:<tag>
$ docker image rm mahedee/webapi:build