Skip to content

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:

Terminal window
docker run -it ubuntu bash

Once 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, -it gives 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:

Terminal window
docker run -d -p 8080:80 nginx

Flag 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 at localhost:8080 on 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.

To see what is currently running:

Terminal window
docker ps

Expected output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp elegant_morse

The 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.

Pass the container ID (or name) to docker stop:

Terminal window
docker stop a1b2c3d4e5f6

Docker 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:

Terminal window
docker ps -a

This 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
What does the `-d` flag do in `docker run -d`?
What does `-p 8080:80` mean in `docker run -d -p 8080:80 nginx`?
Which command lists only the currently running containers?
What signal does `docker stop` send to the container process first?