Google:

adverisment

Docker Interview Question & Answer

details img

 

How do you check running Docker containers?

docker ps       # Only running containers
docker ps -a    # All containers

How to build a Docker image from a Dockerfile?

docker build -t my-image:tag .

How to run a container with a specific port mapping?

docker run -d -p 8080:80 nginx
  • Maps host port 8080 → container port 80.


How to link containers together?

  • Use Docker networks:

 
docker network create my-net docker run -d --name db --network my-net mysql docker run -d --name app --network my-net my-app

How to attach a shell to a running container?

docker exec -it <container_name> /bin/bash

Difference between EXPOSE and -p/--publish?

  • EXPOSE: Metadata, tells which port the container listens on.

  • -p: Maps host port to container port (actual access).

How to inspect container details?

docker inspect <container_name>
  • Gives JSON with IP, volumes, networks, etc.

How to remove all stopped containers?

docker container prune

How to commit a running container into an image?

 
docker commit <container_id> my-new-image:tag

How to mount a volume to a container?

docker run -d -v /host/path:/container/path my-app

How to copy files from host → container or container → host?

docker cp host_file.txt <container_name>:/container_path/ docker cp <container_name>:/container_path/file.txt ./host_path/

How to check image history?

docker history <image_name>
  • Shows image layers and size.

How to reduce Docker image size?

  • Use Alpine base images.

  • Multi-stage builds.

  • Minimize RUN commands and clean caches (apt-get clean, etc.).

How to run a container in detached mode?

docker run -d my-image

How to pass environment variables to containers?

docker run -e VAR_NAME=value my-image

How to rollback to previous image version?

  • Use tagged images:

 
docker run my-image:previous_tag

How to handle Docker logs?

docker logs <container_name> docker logs -f <container_name> # Follow logs

How to restart containers automatically?

docker run --restart unless-stopped my-image

How to create a custom Docker network and connect containers?

docker network create my-net docker run -d --name app --network my-net my-app docker run -d --name db --network my-net mysql

How to check Docker version and system info?

docker version docker info

Leave Comment