dockerbeginner
Docker Basics
Images, containers and Dockerfile
7 questions
By EZ4Code Team
1. What is the relationship between a Docker image and a container?
An image is a read-only template; a container is a running instance of an image
They are exactly the same
A container is the template; an image is the instance
An image can only be created from a container
Explanation: An image is a static read-only template (containing the app and dependencies); a container is a writable running instance of an image; one image can start multiple containers.
2. What is the command to start a container from an image?
docker run
docker start image
docker create only
docker exec
Explanation: docker run [options] image [command] creates and starts a container; docker start starts a stopped container; exec runs a command in a running container.
3. What does the FROM instruction do in a Dockerfile?
Specifies the base image; must be the first non-comment instruction
Runs a command
Exposes a port
Copies files
Explanation: FROM <image>:<tag> specifies the base image; a Dockerfile must start with FROM (except for ARG), and subsequent instructions build on top of it.
4. What is the difference between COPY and ADD in a Dockerfile?
COPY only copies local files; ADD can also auto-extract tar archives and pull from URLs (COPY is recommended)
They are exactly the same
ADD can only copy local files
COPY can extract tar archives
Explanation: COPY simply copies local files into the image; ADD additionally supports auto-extracting tar archives and pulling from URLs, but its behavior is less transparent; COPY is officially recommended.
5. What is the command to build an image?
docker build -t name:tag .
docker make
docker create image
docker compile
Explanation: docker build -t <name>:<tag> <context> builds an image from the Dockerfile in the context directory; . means the current directory is the build context.
6. What do -p and -v do in docker run?
-p maps ports; -v mounts volumes (directories)
-p mounts volumes; -v maps ports
Both are port mappings
Both are volume mounts
Explanation: -p hostPort:containerPort maps ports; -v hostPath:containerPath mounts volumes for data persistence or file sharing.
7. What is the difference between CMD and ENTRYPOINT?
CMD can be overridden by docker run arguments; ENTRYPOINT is more stable, treating the container as an executable
They are exactly the same
ENTRYPOINT can be overridden
CMD cannot be overridden
Explanation: CMD provides the default command and is easily overridden by arguments after docker run; ENTRYPOINT defines a fixed entry point, with arguments passed to it, suitable for fixed executables.