Run Adminer with Docker Compose for MySQL and Postgres Container

Posted October 10, 2023
Run Adminer with Docker Compose for MySQL and Postgres Container

Learn how to create Docker Compose to run an Adminer container for MySQL and Postgres DB Containers. This way, Docker will Run Adminer UI so you can manage your DB.

What You Will Learn

Are you looking to manage your databases within Docker using Adminer? In this guide, you will learn the following:

  • How to run Adminer with Docker compose
  • How to connect PostgreSQL to Adminer. This will allow you to manage Postgres with Adminer and Docker compose
  • Connect MySQL to Adminer. You will create a MySQL Adminer Docker compose to do so

Related: Docker Slim With Minimal Reduce Docker Image Size Strategies

Why Run Adminer with Docker Compose

Adminer is powered using a single PHP file. This makes it light to manage databases such as MySQL and PostgreSQL.

Instead of using local servers such as XAMPP and WAMP, why not run Adminer and use it along with Docker Compose?

Getting Docker Compose with Adminer

You will run Adminer with Docker Compose alongside MySQL or a PostgreSQL container here. You will use Docker Compose to instruct Docker how to spin your Adminer.

In this case, create a docker-compose.yml file and define the Adminer service and configurations. But first, you need to have ready MySQL or a PostgreSQL container.

Let’s Dive in and get Postgres and MySQL with Adminer and Docker compose

Related: Guide to TypeORM with NestJS, Postgres, MySQL, Multer, Docker and Docker Compose

Running Docker Compose Adminer with MySQL

Adminer supports MySQL database. So, if you are running Docker Compose with MySQL, Adminer will be your ideal database management tool.

In your docker-compose.yml file, you should have a MySQL server running as follows:

version: '3.8'
services:
  mysql:
    image: mysql:8.1.0
    environment:
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      MYSQL_DATABASE: your_mysql_database
      MYSQL_USER: your_mysql_user
      MYSQL_PASSWORD: your_mysql_password
    ports:
      - 3306:3306
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - adminer-network

In this example:

  • You are running a MySQL server with Docker Compose
  • Ensure you have your environment variable set. You will need them to access your database on Adminer.
  • Add volumes to persist your database changes.

Now, this MySQL server should be accessed with Adminer as follows:

version: '3.8'
services:
  mysql_server:
    image: mysql:8.1.0
    environment:
      MYSQL_ROOT_PASSWORD: your_mysql_root_password
      MYSQL_DATABASE: your_mysql_database
      MYSQL_USER: your_mysql_user
      MYSQL_PASSWORD: your_mysql_password
    ports:
      - 3306:3306
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - adminer-network

  adminer:
    image: adminer
    ports:
      - 8080:8080
    environment:
      ADMINER_DEFAULT_SERVER: mysql_server
    depends_on:
      - mysql_server
    networks:
      - adminer-network
volumes:
  mysql-data:

networks:
  adminer-network:

Note that:

  • Your Adminer depends on your MySQL database. And you must indicate that using depends_on, then add your mysql_server service.
  • Adminer supports many databases. Therefore, you must add ADMINER_DEFAULT_SERVER, which MySQL runs as a mysql_server service.

To test your container, execute the following command:

docker-compose up -d

Run Adminer with Docker Compose for MySQL and Postgres Container

Everything is ready, and you can now open http://localhost:8080/ on your browser to access Adminer and MySQL running on Docker compose as follows:

Run Adminer with Docker Compose for MySQL and Postgres Container

Note that Adminer has already preselected your mysql_server service. And now, you can log in to your MySQL server. In this case:

  • Username should be root
  • The password should be the MySQL root password you added on the MYSQL_ROOT_PASSWORD environment.
  • You can add your database name specified in MYSQL_DATABASE.

Go ahead and log in to your service:

Run Adminer with Docker Compose for MySQL and Postgres Container

Now everything should be ready as you can access your database as follows:

Run Adminer with Docker Compose for MySQL and Postgres Container

You can use Adminer to manage your MySQL database as such:

Run Adminer with Docker Compose for MySQL and Postgres Container

Creating a Docker Compose Container for PostgreSQL and Adminer

What if your ideal database is PostgreSQL? PostgreSQL can run on Adminer using docker compose. So, your docker-compose.yml file should be updated with the PostgreSQL Adminer connection as follows:

version: '3.8'
services:
  postgres_server:
    image: postgres:16.0
    environment:
      POSTGRES_DB: your_postgres_database
      POSTGRES_USER: your_postgres_user
      POSTGRES_PASSWORD: your_postgres_password
    ports:
      - 5432:5432
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - adminer-network

  adminer:
    image: adminer
    ports:
      - 8080:8080
    environment:
      ADMINER_DEFAULT_SERVER: postgres_server
    depends_on:
      - postgres_server
    networks:
      - adminer-network

volumes:
  postgres-data:

networks:
  adminer-network:

To test your container, execute the following command:

docker-compose up -d

You can now open http://localhost:8080/ on your browser to access Adminer and PostgreSQL running on Docker compose as follows:

Run Adminer with Docker Compose for MySQL and Postgres Container

Adminer has already preselected your postgres_server service:

Run Adminer with Docker Compose for MySQL and Postgres Container

Also, make sure System is selected as PostgreSQL.

To login to your docker compose Postgres Adminer, you will use the following credentials:

  • Username that you added as POSTGRES_USER.
  • Password should be what you specified on POSTGRES_PASSWORD.

Run Adminer with Docker Compose for MySQL and Postgres Container

Conclusion

In this guide, you have learned the following:

  • How to Adminer docker compose.
  • How to connect PostgreSQL to Adminer. This will allow you to manage Postgres with Adminer and Docker Compose.
  • Connect MySQL to Adminer. You will create a MySQL Adminer docker compose to do so.
Run Adminer with Docker Compose for MySQL and Postgres Container

Written By:

Joseph Chege