docker-crear-iniciar-contenedores

Create and Start Containers

  • 7 min

We’ve already seen the docker run command in action, a combined instruction that sequentially orchestrates the entire instantiation process in Docker.

It’s the most famous command in the tool, the one that features in almost every tutorial, and the one you’ll use 90% of the time to start a service.

But it’s not the only command involved. In this article, we’ll look at the lifecycle of our applications, and the difference between creating (create), starting (start), and running (run) 👇.

docker run is a “shortcut”

Internally, when you execute this command, Docker performs several sequential operations for you.

docker run = docker pull (if needed) + docker create + docker start

When we launch a container, three distinct phases occur:

  1. Acquisition: The image is downloaded (if you don’t have it).
  2. Creation: The container is prepared on disk (ID, network, volumes are assigned), but it does not consume CPU or RAM. It is “asleep”.
  3. Startup: The main process runs, and the container comes to life.

Docker Create

The docker create command creates the writable layer of the container on top of the image and prepares the configuration, but does not start it.

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Copied!
  • OPTIONS: Parameters to configure the container.
  • IMAGE: The Docker image from which the container will be created.
  • COMMAND: Optional command to run inside the container.
  • ARG…: Additional arguments for the command.

For example,

docker create --name my-web-server -p 8080:80 nginx
Copied!

If you run this, you’ll see Docker returns a long ID and… nothing else. Nothing happens. If you go to your browser at localhost:8080, nothing will load.

If we check the status with docker ps -a (the -a flag is to also see stopped containers), we’ll see:

CONTAINER ID   IMAGE     STATUS    NAMES
a1b2c3d4e5     nginx     Created   my-web-server
Copied!

Notice the status: Created. The container exists, has a name, has ports assigned, but it’s “powered off”.

What is this for? It’s very useful in orchestration or advanced scripts where you want to “pre-provision” containers so they are ready, and start them all together later in milliseconds.

Docker Start

Now that we have the container created (but stopped), let’s start it.

docker start my-web-server
Copied!

Now it works. Docker finds the container that already existed and simply starts the process. If we check the status again:

CONTAINER ID   IMAGE     STATUS          NAMES
a1b2c3d4e5     nginx     Up 4 seconds    my-web-server
Copied!

Status: Up. Now it consumes RAM and CPU.

There is also, logically, docker stop. This command sends a stop signal (SIGTERM) to the container. It shuts it down, but does NOT destroy it.

The container returns to the “Exited” state, keeping its data and configuration ready for a new docker start.

Docker Run

As we’ve said, docker run is simply the convenience of doing everything above in a single line.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copied!
  • OPTIONS: Parameters to configure the container.
  • IMAGE: The Docker image from which the container will be created.
  • COMMAND: Optional command to run inside the container.
  • ARG…: Additional arguments for the command.

For example,

docker run --name my-other-web -p 8081:80 -d nginx
Copied!

Docker checks if it has the image, creates the container, and starts it immediately.

When to use each?

Don’t use docker run every time or you’ll create duplicate containers.

  • docker run: 95% of the time. When you want to start a new service from scratch.
  • docker start: When you have an existing container you created previously (perhaps a database with persistent data) and you simply want to “resume” work where you left off.

Practical examples