Run a Docker image as a container
Create a container with the intended command, ports, mounts, and environment, then distinguish a new run from restarting an existing instance.
docker run IMAGE creates and starts a new container from an image. Arguments before the image configure the container; arguments after it provide the command or arguments passed through the image's entrypoint.
Select the image and command
docker image ls
docker run --rm -it ubuntu:24.04 /bin/bash
You can also use an actual local image ID from the listing. A tag is a convenient name, while a digest identifies particular image content. A copied example ID from someone else's machine does not identify an image on yours.
The default pull policy normally pulls an image when it is missing locally. It does not guarantee that an existing local tag is refreshed. Use a deliberate --pull policy, and check registry permissions, network access, and architecture compatibility if image retrieval or startup fails.
Combine the settings an application needs
For an application image that listens on container port 8000 and writes data under /data:
docker run -d --name myapp \
--restart unless-stopped \
-p 127.0.0.1:8080:8000 \
--mount type=volume,source=app-data,target=/data \
-e MODE=production \
myapp:1.2
This is a configuration example, not a claim that an image named myapp:1.2 is publicly available. The port mapping is host-local; choose another binding only when remote access is intended. Publishing a port does not make the application listen, configure TLS, or verify readiness.
The named volume can outlive the container, but ownership, application consistency, and backups still need attention. Environment values are strings and may be visible through inspection; use an appropriate secret-delivery mechanism for sensitive settings.
Keep a shell continuation backslash at the end of the line. Adding an inline comment after it breaks a copied multiline command.
Choose a lifecycle
| Pattern | Example | What ends it |
|---|---|---|
| Interactive experiment | docker run --rm -it ubuntu:24.04 bash |
The shell exits |
| Background service | docker run -d --name web -p 127.0.0.1:8081:80 nginx:stable |
The main process exits or is stopped |
| One-off task | docker run --rm myapp:1.2 python etl.py |
The task completes |
The task example assumes the image supports that command. Overriding a command does not necessarily replace an entrypoint. --rm removes the container after exit, including its writable layer; preserve needed results and diagnostic evidence elsewhere.
Inspect and manage the instance
docker ps -a
docker logs --tail 100 myapp
docker exec -it myapp sh
docker stop myapp
docker start myapp
Exec requires a running container and an installed executable. Stop retains the container's writable layer; start launches its existing configured process again. It does not create a fresh image-based instance or apply newly edited environment and port settings.
Every successful run creates a new container. Automatically generated names differ; explicitly reusing an occupied --name produces a conflict, including when the old container is stopped. Inspect and decide whether to restart, rename, or replace the old instance rather than reflexively deleting it.
If the container exits immediately, inspect its state and logs. A command that completed successfully is different from a missing executable, an entrypoint failure, or a service incorrectly launched into the background. Detached mode alone does not keep it alive.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan