Day to day used Docker commands

Tags : Docker / Containers


Frequently used docker commands for local developement, like build, create, start and restart

Day to day docker commands

  1. Building an image using Docker.

    • docker compose build <service_name>
      Eg: docker compose build django_cvarma Note: One may use docker-compose instead of docker compose for legacy versions of docker

    • docker compose build <service_name> --pull --no-cache
      Eg: docker compose build django_cvarma --pull --no-cache

    • --pull: tells to pull the latest version of base images
    • --no-cache: forces to run all steps without using cache layer

    One may just skip service_name to build all services in docker-compose.yaml

  2. List Docker images / containers.

    • docker images # List all docker images
    • docker ps / docker compose ps # List all containers
  3. Create and start containers.

    • docker compose up <service_name> -d
      Eg:docker compose up django_cvarma -d
    • -d: helps to run in background.

    Pro tip to create, start all containers in the background and to follow the logs
    - docker compose up -d; docker-compose logs -f

  4. Commands to start, stop and restart the stopped containers.

    • docker compose start
    • docker compose stop
    • docker compose restart

    One may choose to start or restart the container with specific service also
    Eg: docker compose restart django_cvarma

  5. Down the containers and remove all services, networks defined in docker-compose.yaml

    • docker compose down
    • docker compose down -v # removes the volumes as well, so it will delete all the data stored, especially like the data stored in the DB wil be lost and files that are uploaded.

    Alternatively one can choose to stop and remove specific service
    docker compose stop django_cvarma
    docker compose rm -f django_cvarma # -f force removes without a confirmation prompt