Docker 🐋 quick guide

Posted on 18 Oct, 2020

  1. Remove docker image.

    docker rmi <img-id>
  2. Remove docker container.

    docker rm <container-id>
  3. Build a docker image with a name.

    docker build -f <dockerfile-path> -t name .
  4. Run a container.

    docker run -p 3000:3000 <container-id>
  5. Stop a container.

    docker stop <container-id>
  6. Run a container in detach mode (run in background).

    docker run -d <container-id>

    For the love of God always add a -d while running a container. Speaking this from experience.

    If you don't run in detach mode, you won't be able to Ctrl+C (or exit), instead use Ctrl+PC (yes the P key and C key).

  7. List docker volumes.

    docker volume ls
  8. Remove docker volume.

    docker volume rm <volume-name>
  9. Check port mapping.

    docker port <name>
  10. Starting a docker container

    docker start <container>

    The first two letters of CONTAINER_ID can be provided as an argument too.

  11. Run a command inside container.

    docker container exec <CONTAINER> ls -la
  12. Check history of an image.

    docker history <IMAGE>
  13. Connect to local port inside container.

    docker run -d --network=host --add-host=host.docker.internal:127.0.0.1

Docker Compose

Tip: Alias docker-compose to an alias like dc to make your life better

  1. Build and run containers.

    docker-compose up --build
  2. Start compose in detached mode

    docker-compose up -d
  3. Stop containers.

    docker-compose stop
  4. Check logs/console messages.

    docker-compose logs <image name>
    # or better use docker
    docker logs -f <image name>

    To clear log output, use cmd + k, or ctrl + k

  5. List all containers.

    docker-compose ps

Docker Quick Steals

  1. Run a postgres container.

    docker run --name postgres --rm -p 5438:5432 -e POSTGRES_PASSWORD=pass -e POSTGRES_DB=test -d postgres

    Get psql using

    docker container exec -it postgres psql -U postgres -d test

Last updated