2. Run a 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.
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.