How to run Postgres in Docker container?

docker run -d \
  --name book-service \
  -e POSTGRES_PASSWORD=root \
  -p 5432:5432 \
  postgres:18.1

You can connect to Postgres using the following command:

psql -h 127.0.0.1 -p 5432 -U postgres -d postgres

How to connect to a running Docker container?

docker exec -it container_name bash

How to run Ubuntu in Docker?

  1. Run Ubuntu container
    docker run -d --name ubuntu -it ubuntu
  2. Connect to ubuntu container
    docker exec -it ubuntu bash
  3. Install sudo
    apt update && apt install -y sudo