docker run -d--name web-api -p3000: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
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.
dockerexec-it web-api sh
dockerexec web-api cat/app/config.jsondockerexec-u root web-api apt-get update
Output
/ #
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.