Adding Adminer to Laravel Sail
Adminer is a single PHP file that allows you to connect to your database quickly and easily.
If you’re interested in using PHPMyAdmin instead, we have tutorials on adding it to Sail and Homestead
With Adminer, you can connect to any MySQL, MariaDB, PostgreSQL, SQLite, MS SQL, Oracle, Firebird, SimpleDB, Elasticsearch, or MongoDB database.
In this article, we will assume that you are using either a MySQL, MariaDB, or PostgreSQL database. However, we will be showing how to do this with MySQL for the most part. We will also assume you have docker, docker-compose, and a Laravel Sail project already creating, and that you ran sail down or that your sail containers aren’t running. We will also assume that you already have your docker-compose.yml file already created.
First, let’s head to your docker-compose.yml. From here, let’s find the section that say depends_on and we’ll add adminer to the list:
depends_on:
- mysql
- adminer
- redis
- minio
Addind this to our depends on means that if Adminer doesn’t load properly, sail will cancel loading, as we’ll want to be able to use it when developing. This is similar to how MySQL or Redis is in this list, as we need it when developing our application.
Next, we’ll add a new service in our services section. This is where we’ll add adminer after MySQL, for simplicity, but it will not be MySQL if you’re using a different database.
......
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
adminer:
image: 'adminer:latest'
ports:
- 8080:8080
networks:
- sail
......
Let me take a minute to explain what we added underneath the adminer: line. First, we told Docker Compose to pull the ‘adminer:latest’ image from Docker Hub. Then, we told it to take the container's 8080 port and make it our computer's 8080 port. Now, in order for Adminer to access our database container, we had to make sure they were on the same network, which by default is sail. I’d recommend leaving it as sail unless you know what you’re doing.
After adding Adminer to your services list, you can then run sail up or sail up -d which will pull Adminer’s latest version from Docker Hub.
Now you’ll be able to access Adminer from localhost:8080 in your browser. You’ll have to select your database type, if it’s MariaDB, keep it as MySQL. The server you select is the name of your database container, which you can find out in your docker-compose.yml file. As you can see from the example, above, the MySQL service says “mysql:” as the name, which means the mysql will be the server in Adminer. Then the username and password are the ones you have set in your .env file.
And there you have it, Adminer is successfully working in Laravel Sail. What should we write about next?