Your First Container
Interactive container: getting a shell inside Ubuntu
Section titled “Interactive container: getting a shell inside Ubuntu”The -it flag combination is how you open an interactive terminal session inside a container. Run:
docker run -it ubuntu bashOnce Docker pulls the image and starts the container, your prompt changes to something like:
root@3f2e1a9b7c4d:/#You are now inside the container. The hostname shown (3f2e1a9b7c4d) is the container ID. You have a fully isolated Ubuntu environment — its own filesystem, its own process tree. When you are done, type exit to return to your host shell. The container stops the moment the bash process exits.
What the flags mean:
-i— keep stdin open so you can type commands into the container.-t— allocate a pseudo-TTY so the shell prompt renders correctly and handles terminal control sequences.- Together,
-itgives you an interactive shell session inside the container.
Detached web server: running Nginx in the background
Section titled “Detached web server: running Nginx in the background”Not every container needs an interactive session. Long-running services such as web servers and databases are started in detached mode with -d, so they run in the background without holding your terminal:
docker run -d -p 8080:80 nginxFlag breakdown:
-d— detached mode: the container runs in the background and Docker prints only the container ID.-p 8080:80— publish ports: map host port 8080 to container port 80. Traffic arriving atlocalhost:8080on your machine is forwarded to port 80 inside the container, where Nginx is listening.
After running this command, open http://localhost:8080 in your browser and you will see the Nginx welcome page.
Listing running containers with docker ps
Section titled “Listing running containers with docker ps”To see what is currently running:
docker psExpected output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1b2c3d4e5f6 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp elegant_morseThe columns tell you everything at a glance: the container ID, which image it was started from, the command running inside it, how long it has been up, its status, the port mapping, and an auto-generated name.
Stopping a container
Section titled “Stopping a container”Pass the container ID (or name) to docker stop:
docker stop a1b2c3d4e5f6Docker sends SIGTERM to the main process inside the container, giving it up to 10 seconds to shut down gracefully. If the process has not exited by then, Docker sends SIGKILL to force-terminate it.
Listing all containers (including stopped ones)
Section titled “Listing all containers (including stopped ones)”Once stopped, a container no longer appears in docker ps. Add the -a flag to see everything, including exited containers:
docker ps -aThis is useful for inspecting exit codes, recovering logs, or cleaning up old containers with docker rm.
docker run -d -p 8080:80 nginx
docker ps
docker stop $(docker ps -q --filter ancestor=nginx)
docker ps -a