Introduction
In modern software development, containers have become a common way to package and deploy applications. A container is not a full virtual machine. It shares the host kernel, but runs the application in an isolated process environment with its own filesystem view and network configuration.
In this lab, you will take your first steps into Docker by running a real-world application: the Nginx web server. This is useful because many containers are not one-time commands. They run as long-lived services, so you need to understand their lifecycle.
This is an important shift from the hello-world example. Instead of a container that starts, prints, and exits, you are now working with a service container meant to stay alive while its main process is running.
You will learn how to:
- Start a container in the background (detached mode).
- View the status of your running containers.
- Stop a container when you are done with it.
- Remove a container to free up system resources.
By the end of this lab, you will understand the full lifecycle of a Docker container: create, run, stop, and delete.
Run a Web Server in the Background
In this step, you will launch an Nginx web server.
When you run a command in the terminal, it usually occupies the screen until it finishes. However, for a service like a web server, you want it to run continuously in the background so you can keep using your terminal for other tasks. In Docker, this is called "detached mode".
We use the -d flag to run the container in detached mode. We will also use the --name flag to give our container a specific, easy-to-remember name.
Detached mode does not change what the container is doing. It changes how your terminal is attached to it. The service keeps running, but your shell prompt returns immediately.
Run the following command to start an Nginx container named my-nginx:
docker run --name my-nginx -d nginx
Breaking down the command:
docker run: The command to create and start a new container.--name my-nginx: Assigns the name "my-nginx" to the container. If you skip this, Docker assigns a random name likehappy_hopper.-d: Runs the container in detached mode (background).nginx: The image to use. Docker looks for it locally; if missing, it downloads it from Docker Hub.
The Output:
You will see a long string of characters printed to the terminal. This is the Container ID. The fact that your command prompt returned immediately confirms the container is running in the background.
The container ID is Docker's internal identifier, while the container name is the human-friendly label. In everyday work, names are often easier to use than long IDs.
Check Container Status
Now that you've launched the container, you need to verify it is running as expected. The docker ps command lists all active containers.
Execute the following command:
docker ps
You should see output similar to this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 10 seconds ago Up 10 seconds 80/tcp my-nginx
Understanding the Output:
- CONTAINER ID: The unique ID for this container (truncated version of the long string you saw earlier).
- IMAGE: The image running (
nginx). - STATUS:
Upindicates the container is running correctly. - NAMES: The name we assigned (
my-nginx).
If you see this entry, your web server is successfully running in the background!
The docker ps output is one of your main operational views. It tells you which containers are active now, which image they came from, and how long they have been running.
Stop the Service
When you are finished with a service or need to reconfigure it, you must stop the running container.
The docker stop command sends a signal to the container to shut down gracefully. You can target the container using either its Container ID or its Name. Since we named it my-nginx, we will use the name.
Graceful stop matters because many services need time to close files, release ports, or finish in-flight work. Stopping a container is not just "turning it off"; it is asking its main process to exit cleanly.
Run the following command:
docker stop my-nginx
The terminal will print my-nginx to confirm the command was received.
Verify it stopped:
Run docker ps again. The list should be empty (or my-nginx should be missing).
To confirm it still exists (but is stopped), use the -a (all) flag:
docker ps -a
You will now see my-nginx listed with a status of Exited.
Clean Up Resources
Stopping a container does not delete it. It remains on your disk, preserving its logs and configuration. This is useful if you want to restart it later with docker start.
This is another lifecycle distinction worth remembering: create/run, stop, and remove are separate operations. Docker keeps these states separate so you can inspect or restart containers before deleting them.
However, if you are completely finished with the container, you should remove it to keep your system clean. The docker rm command removes stopped containers.
Execute the following command to remove my-nginx:
docker rm my-nginx
The terminal will print my-nginx to confirm removal.
Final Check:
Run docker ps -a one last time. You will see that the container is completely gone.
Note: You generally cannot remove a running container with docker rm. You must stop it first (as we did), or use the -f (force) flag, though stopping it gracefully is the preferred method.
Summary
In this lab, you successfully managed the full lifecycle of a Docker container. You learned how to:
- Run a background service using
docker run -d. - Check the status of active containers with
docker ps. - Stop a service gracefully using
docker stop. - Remove a stopped container to free up resources with
docker rm.
These four commands are the daily drivers for any Docker user. You are now ready to explore how to interact with these running containers more deeply.



