Skip to main content

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_NAME

If 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_NAME

Run a container in the background (detached mode)

bash
docker run -d IMAGE_NAME

Run with port mapping (for web applications)

bash
docker run -d -p 8080:80 IMAGE_NAME

where 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_NAME

What's important to remember

  • run creates 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.