...

/

Running a Server Container

Running a Server Container

In this lesson, you will be introduced to server containers and learn how to run long-lived containers.

We just saw how to run short-lived containers. They usually do some processing and display some output. However, there’s a very common use for long-lived containers: server containers. Whether you want to host a web application, an API, or a database, you want a container that listens for incoming network connections and is potentially long-lived.

A word of warning: it’s best not to think about containers as long-lived, even when they are. Don’t store information inside the containers. In other words, ensure your containers are stateless, not stateful. A stateless container never stores its state when it is run while stateful containers store some information about their state each time they are run. We’ll see later on how and where to store your container’s state. Docker containers are very stable, but the reason for having stateless containers is that this allows for easy scaling up and recovery. More about that later.

In short, a server container

  • is long-lived
  • listens for incoming network connections

How can we manage this? Read on.

Running a long-lived container

Up until now, we remained connected to the container from our command line using the docker run command, making it impractical for running long-lived containers.

To disconnect while allowing the long-lived container to continue running in the background, we use the -d or –detach switch on the docker run command.

Running a container as detached means that you immediately get your command-line back and the standard output from the container is not redirected to your command-line anymore.

Suppose you want to run a ping command. The Linux alpine container can be ...