Skip to content

Docker Day 2: Running Your First Nginx Container

After setting up the Docker environment on Day 1, my next task was to actually run a containerized application. The goal was to deploy a lightweight Nginx web server. This is the "Hello, World!" of the container world and the first real taste of Docker's power.

This task taught me the fundamental workflow of pulling a pre-built image from a registry and running it as a live, isolated container on my server.

Table of Contents


The Task

On App Server 1, I needed to deploy a container based on the official Nginx image. The specific requirements were: 1. Use the nginx image with the alpine tag. 2. Name the container nginx_1. 3. Ensure the container was left in a running state.


My Solution & Command Breakdown

I was able to accomplish the entire task with a single docker run command, followed by a verification step.

1. The Container Run Command

This command tells Docker to find the image, download it if necessary, and then create and start a container from it.

sudo docker run -d --name nginx_1 nginx:alpine

Command Breakdown: * sudo docker run: The primary command used to run a container from an image. * -d: This is the "detached" flag. It runs the container in the background so I get my terminal prompt back. Without this, the terminal would be attached to the container's output. * --name nginx_1: This flag assigns a custom, easy-to-remember name to my container. * nginx:alpine: This specifies the image to use. nginx is the name of the image, and alpine is the tag, which points to a specific, lightweight version of that image.

2. The Verification Command

To make sure the container was running, I used the docker ps command.

sudo docker ps
This listed all the currently running containers, and I saw my nginx_1 container in the output, confirming success.
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
a1b2c3d4e5f6   nginx:alpine   "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   80/tcp    nginx_1


Common Pitfalls & Solutions

While my execution was smooth, I thought about a few common errors that could happen with this task.

  • Failure 1: Forgetting the -d flag.

    • Symptom: The terminal seems to "hang" and starts showing Nginx log output. You don't get your command prompt back.
    • Solution: Press Ctrl+C to stop the container. The container is now stopped. You'll need to remove it (sudo docker rm nginx_1) and then run the docker run command again with the -d flag included.
  • Failure 2: "Container name is already in use".

    • Symptom: You run the command, but Docker responds with an error saying the name nginx_1 is already taken. This happens if a previous attempt created a container (even a stopped one) with the same name.
    • Solution: Container names must be unique. You must first remove the old container using sudo docker rm nginx_1 before you can create a new one with that name.
  • Failure 3: Permission Denied.

    • Symptom: Running docker run ... gives an error about not being able to connect to the Docker daemon socket.
    • Solution: This means you forgot to use sudo. By default, only the root user can interact with the Docker engine. The fix is to run the command again with sudo at the beginning.

Why Did I Do This? (The "What & Why")

This task demonstrates the core value of Docker: effortless application deployment.

Instead of a long, manual process (installing Nginx, managing dependencies, configuring system files), I used a single command to run a fully functional, pre-configured web server. The nginx:alpine image was created by experts and shared on a public registry (Docker Hub). I simply pulled that blueprint and ran it.

This workflow is fast, repeatable, and consistent. The Nginx server running in my container is identical to the one anyone else in the world would get by running the same command. This eliminates the classic "it works on my machine" problem.


Deep Dive: Image vs. Container

Understanding the difference between an image and a container is the most important concept in Docker.

  • An Image is a blueprint. It's a read-only, inert template that contains the application, its libraries, and all its dependencies. The nginx:alpine file that Docker downloaded is an image. It doesn't do anything on its own, just like a house blueprint sitting on a table.
  • A Container is a running instance of an image. It's the live, executing environment created from the blueprint. My nginx_1 container is a real, running Nginx process, isolated from the rest of my server. It's the house that was built using the blueprint.

From a single image, I can create many containers, just like I can build many identical houses from one blueprint. Each container is its own isolated environment.


Exploring the Commands Used

- docker run: The workhorse command of Docker. It's incredibly versatile, with dozens of flags to control networking, storage, resources, and more. I used the basic flags -d (detached) and --name to get started. - docker ps: This is my primary tool for seeing what's currently running. The ps stands for "Process Status," which is borrowed from the classic Linux ps command. It gives me a quick overview of all active containers, their IDs, the image they're based on, and their names.