IntelliJ IDEA 2017.3 Help

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

  1. Create a Dockerfile and open it in the editor.
  2. Type FROM <DBMS_image_name>:<tag> e.g. FROM postgres:latest. If necessary, add other instructions.
  3. Click runTwoGreenArrows and select Run on 'Docker'.
  4. 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) for ExposedPorts. For a postgres container you are likely to find something like 5432/tcp.
    75 DockerDBMSExposedPorts
  5. 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 add and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify localhost or 127.0.0.1. To make the port accessible from other computers on your network as well, specify 0.0.0.0. To apply the changes, click Save.
    • Select the Publish all ports checkbox 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.
    76 DockerDBMSNetworkSPorts
  6. Connect to your database.

1b. Pull and then run an image from the Docker tool window

  1. In the Docker tool window (View | Tool Windows | Docker), right-click the Docker node and select Pull image.
  2. 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.

  3. When the image is downloaded, select it, and then click iconCreateContainerDocker or select Create container from the context menu.
  4. In the Create container popup, click Create.
  5. In the dialog that opens, if necessary, specify the name for the container that will be created.
  6. To make the database port in the container available from the host, either select All for Publish exposed ports to the host interfaces or specify necessary port bindings.
  7. Click Run.
  8. When the container starts, run the Inspect command for the container and search the inspection results (Ctrl+F) for NetworkSettings. In the "Ports" subsection, note the values for the host IP and port number for connecting to the database.
    76 DockerDBMSNetworkSPorts
  9. Connect to your database.

1c. Run an image using a Compose file

  1. Create a 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 and latest.

  2. Create a Docker run configuration (Run | Edit Configurations | add | Docker | Docker-compose) and select your docker-compose.yml file.
  3. To execute the run configuration, right-click the docker-compose.yml file in the Project view and click Run (run).
  4. 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 results (Ctrl+F) for ExposedPorts. For a postgres container you are likely to find something like 5432/tcp.
    75 DockerDBMSExposedPorts
  5. 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 add and specify the mapping. To make the port accessible only from your localhost, for Host IP, specify localhost or 127.0.0.1. To make the port accessible from other computers on your network as well, specify 0.0.0.0. To apply the changes, click Save.
    • Select the Publish all ports checkbox 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.
    76 DockerDBMSNetworkSPorts
  6. Connect to your database.

2. Connect to your database

  1. Open the Database tool window, e.g. View | Tool Windows | Database.
  2. Click add, point to Data Source, and select your DBMS, e.g. PostgreSQL.
    77 DockerDBCreateDataSource
  3. 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.
  4. 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 both postgres, and the password is empty. For other images, refer to corresponding documentation to find out what your database settings should be.
  5. To make sure that the settings are all correct and IntelliJ IDEA can properly interact with your database, click Test Connection.
    78 DockerDBDataSouceS

For more info on working with databases and SQL, see Databases and SQL.

Last modified: 30 November 2017

See Also

Languages, Frameworks and Technologies: