How to run a docker container?
A container is started with the docker run command, specifying the image it should be created from.
Basic example
bash
docker run IMAGE_NAMEIf the image is not downloaded locally, Docker will download it from a registry (for example, from Docker Hub).
Run a container in interactive mode (with a terminal)
bash
docker run -it IMAGE_NAMERun a container in the background (detached mode)
bash
docker run -d IMAGE_NAMERun with port mapping (for web applications)
bash
docker run -d -p 8080:80 IMAGE_NAMEwhere 8080 is the port on the host, 80 is the port inside the container.
Run with a name
bash
docker run --name my_container IMAGE_NAMEWhat's important to remember
runcreates and starts a container.- When the main process inside the container finishes, the container stops.
If you want, I can show how to run with a volume, with environment variables, or how to view container logs and state.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.