Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Docker provides a useful CLI to interact with a container. In this tutorial, we'll see the run and start commands and highlight how they're different while going through some practical examples.

2. Run a Container

Docker's run command is a combination of its create and start commands. It creates a container over its specific image and then starts it. For example, let's run a Postgres container:

docker run --name postgres_example -p 5432:5432 -v /volume:/var/lib/postgresql/data -e POSTGRES_PASSWORD=my_password -d postgres

Let's have a look at our running containers with docker ps:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                                       NAMES
52b7c79bfaa8   postgres  "docker-entrypoint.s…"   22 seconds ago   Up 20 seconds  0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres_example

If we use docker logs, we can also check out more info about a started container, such as:

starting PostgreSQL 13.2
listening on IPv4 address "0.0.0.0", port 5432
listening on IPv6 address "::", port 5432
listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
database system is ready to accept connections

3. Start a Container

Docker's start command starts a stopped container. A container can stop for different reasons – for example, when it has consumed too much memory and gets killed by the host OS.

To demonstrate this, let's manually stop the container we created earlier:

docker stop 52b7c79bfaa8

In this case, our container's running list will show an exited container:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                    PORTS                                       NAMES
52b7c79bfaa8   postgres  "docker-entrypoint.s…"   2 minutes ago    Exited (0) 2 seconds ago  0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres_example

Let's have a look also at the logs:

received fast shutdown request
aborting any active transactions
shutting down
database system is shut down

In case a container is down, we may want to start it again using docker start:

docker start 52b7c79bfaa8

If no errors occur while starting the container, we'll be back to a running container status. Docker also provides the docker restart command, which combines stop and start into a single command.

4. Conclusion

In this tutorial, we briefly discussed the run and start commands in Docker.

We've seen an example of running a container using docker run. If a container stops, we can start it again with docker start.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!