docker run — create and start a container
docker runcreates and starts a container from an image.- If the requested image isn’t present locally, Docker pulls it from Docker Hub the first time and reuses the downloaded image for subsequent runs.
- By default
docker runruns the container in the foreground (attached). Use-dto run in detached (background) mode.
nginx for the first time triggers a pull and then starts the container:
docker login to increase limits on the free Personal tier, or use a paid plan for unlimited pulls.

If you hit “too many requests” or rate limit errors while following this lesson, run
docker login or sign up for a paid Docker Hub plan to avoid interruptions.docker ps and docker ps -a — list containers
docker pslists currently running containers and shows: CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, and NAMES.- Each container receives an automatic name (e.g.,
silly_sammet) unless you supply one with--name. docker ps -alists all containers, including exited ones.
Stop and remove containers
- Stop a running container:
docker stop <container>where<container>is a container name or ID (short prefix allowed if unambiguous).
- Remove a stopped container:
docker rm <container>
Image management: docker images, docker rmi, docker pull
docker imageslists images stored locally with REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.docker rmi <image>removes an image. You must stop and remove any containers using that image first.docker pull <image>downloads an image to the host without running it — useful to avoid the initial pull delay when you laterdocker run.
Example output:
Why some containers exit immediately
Containers run the process given as PID 1. When that process exits, the container stops. Many base images (likeubuntu) do not include a long-running foreground process, so starting them without a command will simply exit.
Example: default ubuntu run exits immediately:
sleep 5 and exit after five seconds.
Executing commands in a running container
Usedocker exec to run commands inside an already-running container. Common uses: inspect files, check processes, or open an interactive shell.
- Run a single command inside a container:
docker exec <container> <command>
- Start an interactive shell:
docker exec -it <container> /bin/bash(or/bin/shif bash isn’t available)
Foreground (attached) vs detached mode
- By default
docker runattaches the container’s stdout/stderr to your terminal (foreground). Ctrl-C sends SIGINT to that process and usually stops the container. - Use
-dto run detached (background):
- Follow logs:
docker logs -f my-web-app - Open a shell:
docker exec -it my-web-app /bin/bash - Attach to the main process:
docker attach my-web-app(see warning below)
docker attach connects directly to the container’s primary process stream and can interfere with signal handling (for example, Ctrl-C may stop the container). Prefer docker logs or docker exec -it for most debugging and interactive tasks.That covers the basic Docker CLI commands you’ll use to run, list, stop, remove, and inspect containers and images. Practice these commands in the hands-on labs to solidify your understanding.
Links and references
- Docker documentation: https://docs.docker.com/
- Docker Hub rate limits: https://docs.docker.com/docker-hub/download-rate-limit/