Start a Docker Container
Razvan Ludosanu
Founder, learnbackend.dev
Updated: 7/16/2024
Published: 7/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:
$ docker run -it <image>
Run in Warp
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:
$ docker run -it ubuntu
root@2ce91ff7d776:/#
Run in Warp
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:
$ docker run -d < image>
Run in Warp
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
Founder, learnbackend.dev
Filed Under
Related Articles
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.
Use An .env File In Docker
Learn how to write and use .env files in Docker to populate the environment of containers on startup.
Run SSH In Docker
Learn how to launch and connect to a containerized SSH server in Docker using password-based authentication and SSH keys.
Launch MySQL Using Docker Compose
Learn how to launch a MySQL container in Docker Compose.
Execute in a Docker Container
Learn how to execute one or multiple commands in a Docker container using the docker exec command.
Expose Docker Container Ports
Learn how to publish and expose Docker container ports using the docker run command and Dockerfiles.
Restart Containers In Docker Compose
Learn how to restart and rebuild one or more containers in Docker Compose.
Output Logs in Docker Compose
Learn how to output, monitor, customize and filter the logs of the containers related to one or more services in Docker Compose
Rename A Docker Image
Learn how to rename Docker images locally and remotely using the docker tag command.
Understand healthcheck in Docker Compose
Learn how to check if a service is healthy in Docker Compose using the healthcheck property.