Run a database in a Docker container
You can use Docker to run a database container and test how your application interacts with it. This tutorial describes how to run a Docker container with a PostgeSQL server and connect to it using IntelliJ IDEA.
Before you begin, make sure that Docker integration is properly configured.
Pull a PostgreSQL server image
- In the Docker tool window, right-click the Images node, and then click Pull image.
- In the Pull Image dialog, select the default Docker Hub registry and specify the PostgreSQL server image repository name and tag (
- Click OK and wait until the image is pulled.
Run a container from the PostgreSQL server image
- In the Docker tool window, right-click the PostgreSQL server image and then click Create container.
- In the Create container popup, click Create.
- In the Create Docker Configuration dialog, specify the name of the container, select All to publish all exposed container ports to the host interfaces, and click Run.
Connect to the PostgreSQL server
Docker automatically maps the default PostgreSQL server port 5432 in the container to a host port within the ephemeral port range (typically from 32768 to 61000). This tutorial assumes that port 32768 was used. To check what the actual mapping is, right-click the PostgreSQL container in the Docker tool window, click Inspect and then find the value of the
HostPort field in the output.
- Open the Database tool window ( ).
- Click New (), point to Data Source, and then click PostgreSQL.
- Set the host name to
localhost, port to
32768(or whatever host port it was mapped to), default database and user to
postgres. No password is required by default.
- If necessary, download the driver and test the connection.
- Click OK to add the PostgreSQL server as a data source
For more information about working with database sources in IntelliJ IDEA, see Databases and SQL.