Connect to MySQL / PostgreSQL Running in Docker Container

Running the dockerized instances of databases such as MySQL or Postgres saves us from the hassles of installing the databases on local machines, thus keeping the OS lean and clean. MySQL and Postgres are among the most popular database distributions. In this tutorial, we will learn to install the databases in docker containers and connect to them from other applications running on your local machine.

As a prerequisite, we assume that Docker is installed on your machine and running. If not, please refer to for Docker installation and refer to for Docker Compose installation.

1. Dockerized MySQL with Adminer

Adminer (formerly phpMinAdmin) is a full-featured database management tool written in PHP. It provides an excellent admin UI interface for several databases, such as MySQLMariaDBPostgreSQLSQLiteMS SQLOracleMongoDB etc.

1.1. Install the Services

The following docker-compose.yaml file will install the MySQL database and Adminer UI in a docker container.

Note that the compose file:

  • exposes the MySQL database to a 3306 port number. We can access the database at URL localhost:3306 or URLs. If we want to connect to this DB instance from a remote machine, then we must use the machine IP address in the URL.
  • creates a volume mysql_db_data_container used for persisting the data stored between container restarts. Note that docker containers are stateless, so the data will not be persisted if we are not using the volume to store the MySQL data.
  • installs and exposes the Adminer UI to URL localhost:8080.

Make sure that port numbers 3306 and 8080 are not already in use when we run the script.

version: '3.7'
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
      MYSQL_ROOT_PASSWORD: password
      - 3306:3306
      - mysql_db_data_container:/var/lib/mysql
    image: adminer:latest
      ADMINER_DEFAULT_SERVER: mysql_db_container
      - 8080:8080


To run the script, run the ‘docker-compose up‘ command in the same folder where the file is stored. The -d option runs the command in detach mode (in the background).

docker-compose up -d

1.2. Verify the Services

To verify that both services are installed and running properly, run the ‘docker ps‘ command:

C:\Users\lokesh>docker ps

CONTAINER ID   IMAGE            COMMAND                  CREATED      STATUS          PORTS                               NAMES
22e7865b1241   adminer:latest   " php -…"   3 days ago   Up 26 minutes>8080/tcp              mysql-adminer-adminer_container-1
a3614162267a   mysql:latest     "docker-entrypoint.s…"   3 days ago   Up 26 minutes>3306/tcp, 33060/tcp   mysql-adminer-mysql_db_container-1

We can connect to Adminer at http://localhost:8080/ .

Adminer Login screen

Fill in the correct username and password, and log in to the database to start working on it.

Adminer UI

2. Dockerized PostgreSQL with Adminer

Most configurations will remain the same as in the above example. Only we will replace the MySQL service with the Postgres service.

version: '3.7'
    image: postgres:latest
      POSTGRES_USER: root
      POSTGRES_PASSWORD: password
      POSTGRES_DB: inventory
      - 5432:5432
      - postgres_db_data_container:/var/lib/postgresql/data
    image: adminer:latest
      ADMINER_DEFAULT_SERVER: mysql_db_container
      - 8080:8080


To run the script, run the ‘docker-compose up‘ command in the same folder where the file is stored.

docker-compose up -d

Now we can connect to Adminer instance in localhost:8080 URL and then log into postgres database using the specified credentials.

Adminer with PostgreSQL

After filling in the credentials, we can see the database has been created and we can work on it.

3. Connecting from Spring Boot Application

If we want to connect with this database from a Spring boot application, the most straightforward way is to configure the DataSource properties.

In the following properties file, we can override the datasource URL, username and password with supplied properties or environment variables i.e. DB_HOST, DB_PORT, DB_USER, DB_PASSWORD. If no properties are supplied in runtime, the default values will be selected, and a database connection will be established.


4. Conclusion

In this Docker tutorial, we learned to install the database services such as MySQL and PostgreSQL and connect to these database installations from a local machine. We learned to connect to the database with Adminer UI, and with a Spring boot application as well.

Happy Learning !!

Sourcecode on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.