Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #88: Switching MySQL to MariaDB for ARM 64 (M1) Support


Here's one way to support switching between MySQL and MariaDB with an environment variable.

If you prefer video, I recorded a demo video going over what’s written below.

Update as of late 2022: Good news! MySQL 8+ now supports amd64 and arm64 CPU architectures out of the box. Docker will pull the correct one for your system without having to do anything extra!

There’s no arm64 support for MySQL 5.7 but that’s coming up for end of life in October 2023. I’d suggest upgrading to 8+ before then.

I’ve left the original tip below unedited for historic purposes and to demo how you can set dynamic Docker images based on an environment variable.

As of mid-2021 the official MySQL image doesn’t support ARM 64 CPUs which includes Apple’s M1+ line of Macs. You can tell if it’s supported by looking in the list of tags on the Docker Hub under the OS/ARCH column.

Let’s say you work at an organization where most developers are using Windows with WSL 2, Intel MBPs and native Linux but you start to have a few folks using M1+ MBPs.

One way to continue using MySQL for everyone except the M1+ folks would be to make the image property populated by an environment variable like this:

# docker-compose.yml

    image: "${DOCKER_MYSQL_IMAGE:-mysql:8.0.17}"
      - "db:/var/lib/mysql"

  db: {}

Then in your .env file (ignored from version control) you can drop in something like:

# If you're using an Intel / AMD CPU (x86-64) you can leave this as the default value,
# but if you're using an M1+ CPU (ARM 64) you can use the MariaDB image instead of
# MySQL by uncommenting the variable below and setting it to mariadb:10.6.4-focal.
# DOCKER_MYSQL_IMAGE=mysql:8.0.17

Most folks can keep that value commented out since it defaults to MySQL but anyone using an M1+ Mac or any other ARM 64 device can uncomment that and replace MySQL with MariaDB by setting DOCKER_MYSQL_IMAGE=10.6.4-focal.

This pattern works nicely if you use an .env.example file that you commit to version control and everyone copies that to an .env file locally. This pattern and more was covered in my DockerCon 2021 talk on Docker best practices.

You can also read more about MariaDB vs MySQL compatibility. I was recently doing client work where they use MySQL and had to make this change. So far for their application it’s been a seamless drop-in replacement.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.