IntelliJ IDEA 2017.2 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 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.
    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, on the Deployment tab, if necessary, specify the name for the container that will be created.
  6. 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.
  7. Click Run.
  8. 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) for NetworkSettings. You'll find the port of interest in the "Ports" subsection next to "HostPort".
    76 DockerDBMSNetworkSPorts
  9. Connect to your database.

1c. Run an image using a Compose file

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

  2. Create a Docker Deployment run configuration for you docker-compose.yml file: Run | Edit Configurations | add | Docker Deployment. Select your docker-compose.yml file from the Deployment list.
  3. Execute the run configuration: 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 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 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.
    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: 29 November 2017

See Also

Languages, Frameworks and Technologies: