Running a DBMS image and connecting to the database
You can run a DBMS image from a Dockerfile, from the Docker tool window or using a Compose file. When the DBMS image is running, you can connect to the database.
1a. Run an image from a Dockerfile
- Create a Dockerfile and open it in the editor.
- Type
FROM <DBMS_image_name>:<tag>
e.g.FROM postgres:latest
. If necessary, add other instructions. - Click and select Run on 'Docker'.
- When the container starts, find out which port the database server is listening on:
In the Docker tool window, right-click your database container and select Inspect. Search the inspection result (Ctrl+F) forExposedPorts
. For a postgres container you are likely to find something like5432/tcp
. - Make the container database port available on the host:
Select the Port Bindings tab. You can choose to specify the host port yourself or let Docker decide which port should be used. So, do one of the following:- Click and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify
localhost
or127.0.0.1
. To make the port accessible from other computers on your network as well, specify0.0.0.0
. To apply the changes, click Save. - Select the Publish all ports check box and click Save. Run the Inspect command for the container. Search the inspection result (Ctrl+F) for
NetworkSettings
. The"Ports"
subsection will include the info about the database host port you should be using.
- Click and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify
- Connect to your database.
1b. Pull and then run an image from the Docker tool window
- In the Docker tool window ( ), right-click the Docker node and select Pull image.
- To pull an image from Docker Hub (
registry.hub.docker.com
), specify the image name in the Repository field, e.g.postgres
, and the image tag, e.g.latest
.If pulling an image assumes user authentication, click New to create a Docker Registry configuration and specify your Docker image repository user account info.
- When the image is downloaded, select it, and then click or select Create container from the context menu.
- In the Create container popup, click Create.
- In the dialog that opens, on the Deployment tab, if necessary, specify the name for the container that will be created.
- On the Container tab, select the Publish all ports check box, or expand the Port bindings section and specify the database container port - host port mapping.
- Click Run.
- If you are using the Publish all ports option (check the Port Bindings tab for your container), find out which host port you should be using:
Run the Inspect command for the container and search the inspection result (Ctrl+F) forNetworkSettings
. You'll find the port of interest in the"Ports"
subsection next to"HostPort"
. - Connect to your database.
1c. Run an image using a Compose file
- Create a Compose
docker-compose.yml
file. A minimal Compose file for a DBMS image should contain something like this:version: '3' services: db: image: postgres:latest
Specify the name and tag of the image that you are going to use in place of
postgres
andlatest
. - Create a Docker Deployment run configuration for you
docker-compose.yml
file: . Select yourdocker-compose.yml
file from the Deployment list. - Execute the run configuration: .
- When the container starts, find out which port the database server is listening on:
In the Docker tool window, right-click your database container and select Inspect. Search the inspection result (Ctrl+F) forExposedPorts
. For a postgres container you are likely to find something like5432/tcp
. - Make the container database port available on the host:
Select the Port Bindings tab. You can choose to specify the host port yourself or let Docker decide which port should be used. So, do one of the following:- Click and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify
localhost
or127.0.0.1
. To make the port accessible from other computers on your network as well, specify0.0.0.0
. To apply the changes, click Save. - Select the Publish all ports check box and click Save. Run the Inspect command for the container. Search the inspection result (Ctrl+F) for
NetworkSettings
. The"Ports"
subsection will include the info about the database host port you should be using.
- Click and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify
- Connect to your database.
2. Connect to your database
- Open the Database tool window, e.g. View | Tool Windows | Database.
- Click , point to Data Source, and select your DBMS, e.g. PostgreSQL.
- If there is the message Download missing driver files in the lower part of the Data Sources and Drivers dialog that opens, click the Download link.
- Specify the database settings. In the Port field, specify the host port mapped to the container database port. For the
postgres:latest
image, the database and user, by default, are bothpostgres
, and the password is empty. For other images, refer to corresponding documentation to find out what your database settings should be. - To make sure that the settings are all correct and IntelliJ IDEA can properly interact with your database, click Test Connection.
For more info on working with databases and SQL, see Databases and SQL.
Last modified: 29 November 2017