Start a Docker Container

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Updated: July 16, 2024Published: July 11, 2024

The short answer

To start a Docker container in the foreground of the terminal based on an existing Docker image, you can use the docker run command as follows:

Bash
$ docker run -it <image>

Where:

  • The -i flag is used to launch the container in interactive mode, allowing it to accept input from the command line.
  • The -t flag is used to allocate a terminal within the container, making it behave like a regular terminal interface.

Note that this command will automatically download the image on your local machine from the configured Docker registry if not present.

Also note that you can find the list of images present on your system using the docker images command.

For example:

Bash
$ docker run -it ubuntu
root@2ce91ff7d776:/#

You can learn more about restarting containers by reading our other article on how to restart stopped containers in Docker.

Starting a container in the background

To start a container in the background (i.e. "detached mode"), you can use the docker run command with the -d flag as follows:

Bash
$ docker run -d < image>

Easily retrieve these commands using Warp's AI Command Suggestions feature

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering docker launch container background in the AI Command Suggestions will prompt a docker command that can then quickly be inserted into your shell by doing CMD+ENTER.

Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Learning Docker (The Easy Way) Using LazyDocker & Warp

A concise guide to learning Docker using Lazydocker. Highlights Docker’s benefits and takes advantage of Warp's AI features for a quick setup.

Run SSH In Docker

Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.

Remove a Docker Image

Learn how to remove a Docker image locally, on a Docker registry, and on Artifactory.

Override the Container Entrypoint With docker run

Learn how to override and customize the entrypoint of a Docker container using the docker run command.

The Dockerfile ARG Instruction

Learn how to define and set build-time variables for Docker images using the ARG instruction and the --build-arg flag.

Stop All Docker Containers

How to gracefully shutdown running containers and forcefully kill unresponsive containers with signals in Docker using the docker-stop and docker-kill commands.

Set Docker Container Hostname

Learn how to set, change and match a docker container hostname.

How To Use An .env File In Docker Compose

Learn how define and pass environment variables to Docker containers using an .env file in Docker Compose.

Use An .env File In Docker

Learn how to write and use .env files in Docker to populate the environment of containers on startup.

Restart Docker Containers

Learn how to restart Docker containers automatically with restart policies and manually using the docker restart, docker start, docker stop and docker kill commands.

Run Bash Shell In Docker

Start an interactive shell in Docker container

Launch MySQL Using Docker Compose

Learn how to launch a MySQL container in Docker Compose.