docker run -d --name web-api -p 3000:3000 myapp:1.0docker run -it ubuntu:22.04 bash
output
e4f5a6b7c8d9...
Note docker run = docker create + docker start. The long hex string printed is the container ID. Use --name so you can refer to it by a human-readable name instead.
run containerstart container from imagelaunch containercreate and start
Start a Stopped Container
syntax
docker start <container>
example
docker start web-api
docker start -ai my-cli-tool
output
web-api
Note Unlike docker run, this does not create a new container — it restarts an existing one that was previously stopped. Use -ai to reattach stdin and see output.
Note Sends SIGTERM first and waits 10 seconds (default) before sending SIGKILL. Use -t to change the grace period. If your app does not handle SIGTERM, it will always take the full timeout before dying.
Note You cannot remove a running container without -f. The second example force-removes ALL containers — obviously destructive. Data in unnamed volumes is lost unless you use -v to also remove them intentionally.
remove containerdelete containerclean up containersdestroy container
List Containers
syntax
docker ps [options]
example
docker ps
docker ps -a
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
output
CONTAINER ID IMAGE STATUS PORTS NAMES
e4f5a6b7c8d9 myapp:1.0 Up 2 minutes 0.0.0.0:3000->3000/tcp web-api
Note Without -a you only see running containers. Stopped containers still exist and consume disk space until you docker rm them.
list containersshow running containerssee all containerscontainer status
Server listening on port 3000
GET /api/users 200 12ms
Note Use -f to follow in real time (like tail -f). Combine --tail and --since to avoid dumping thousands of lines. Logs persist until the container is removed.
Note The container must be running. Use -it for an interactive shell. The -u flag lets you override the user (e.g., run as root even if the image uses a non-root user).
exec into containershell into containerrun command in containerenter containerbash into container
Attach to a Running Container
syntax
docker attach <container>
example
docker attach web-api
Note This connects your terminal to the container's main process (PID 1). Pressing Ctrl+C sends SIGINT to that process and may stop the container. Use Ctrl+P, Ctrl+Q to detach without stopping. Prefer docker exec for most debugging scenarios.
attach containerconnect to containerview main process
Rename a Container
syntax
docker rename <old_name> <new_name>
example
docker rename web-api web-api-legacy
Note Works on running or stopped containers. Other containers referencing the old name via DNS in a user-defined network will no longer resolve it.
Note Blocks until the container stops and then prints the exit code. Exit code 0 means success. Useful in scripts that need to wait for a task container to finish before proceeding.
wait containerblock until doneexit codewait for finish
Show Port Mappings
syntax
docker port <container>
example
docker port web-api
output
3000/tcp -> 0.0.0.0:3000
8443/tcp -> 0.0.0.0:8443
Note Only shows ports explicitly published with -p at run time. Ports declared with EXPOSE in the Dockerfile are not listed here unless they were also published.