Skip to content

Running the Container

We have a built image named node-aboard. To create and run a live container from that image, we run:

Terminal window
docker run -p 3000:3000 node-aboard
  • docker run: creates and starts a new container based on an image.
  • -p 3000:3000: maps host port 3000 to container port 3000.
  • node-aboard: the name (tag) of the image we want to run.

If the Node app is listening on the expected port and starts cleanly, it should now be fully reachable.

Navigate to http://localhost:3000 in a browser and break a bottle of champagne over the server rack bow (No, don’t actually do that. It’s just a metaphor).

A Successful Voyage

That is the big moment: our application is now running cleanly inside an isolated container, totally divorced from whatever weird global tools might be installed on the host machine.

Port mapping is one of the most common early confusion points with Docker.

The format is always:

hostPort:containerPort

So, if we ran:

Terminal window
docker run -p 8080:3000 node-aboard

That means:

  • The host machine (the laptop) listens on port 8080.
  • Traffic hitting 8080 is forwarded to port 3000 inside the container, which is where our Node app is listening.

We would visit the app at http://localhost:8080.

Port Collisions

If another application on your laptop is already using port 3000, docker run -p 3000:3000 will fail. You’ll need to map it to an open host port, like -p 3001:3000. Note that the container port usually stays whatever the app was programmed to listen on.

There are a few ways to stop a running container.

  • Press Ctrl + C in the terminal where the container is running in the foreground.
  • From a new terminal with the Docker CLI stop command:
    • run docker ps to list all running containers,
    • then docker stop <container_id>
  • Find and stop the container in Docker Desktop.
Docker Desktop Container Running

Figure 1: The Docker Desktop Container Running

Often we want to run a container in the background, without tying up our terminal.

We can do this by adding the -d flag to the docker run command.

Terminal window
docker run -d -p 3000:3000 node-aboard

In this case we can’t use Ctrl + C to stop the container.

Instead, we use the docker stop command:

Terminal window
docker stop <container_id>

Docker Networking Overview

What could possibly go wrong?