Skip to main content

Basic commands for docker

Build image

docker build -t my-app .

Build using a custom Dockerfile (e.g., Dockerfile.production).

docker build  -f Dockerfile.production -t my-app .

List images

docker images
note

Note: The -a or --all flag in the docker images command displays all the Docker images, including intermediate ones that are not referenced by any tags. By default, docker images shows only the images with at least one tag. However, there may be some images without any tags that are still taking up disk space on the system. The -a flag can be helpful in identifying images that can be pruned to save disk space. When used with the docker rmi command, the -f or --force flag can also be used to remove images with no tags.

Start Container

docker run -d --rm -p 3000:80 --name container-name image-nage

Example: 3000:80 means access app on localhost:3000, which maps to container port 80

Flag used in the command

  • -d - To run the container in the background (Detach Mode )
  • --rm - To delete the container, when you stop the container
  • -p - Port Mapping between container and outside world.
  • 3000:80 - [Port access from Browser]: [Port exposed from the container]

List containers

docker ps

Delete image

docker rmi image

Stop containers

docker stop container_name_or_id
note

Use -f to force removal (e.g., if image is in use by stopped containers)

Delete containers

docker rm container_name_or_id

Enter a Running Container

docker exec -it container_name_or_id sh