0

I have a Java application that needs a MySQL/MariaDB database in order to work. My goal is to build a standalone Docker image for it. I have looked up a number of tutorials but they seem to contradict what I want to achieve. This makes me doubt whether my goal is appropriate in the first place, but I still can't see why it wouldn't be a reasonable one.

I do not care for sharing the MySQL server to have multiple databases on it. It's only important that the DB of the application be there, nothing else.

What is the right way to proceed to build such a Docker image?

Do I make two containers with docker-compose, one for the DB and one for the Java application? That's what most seem to suggest, but I want an image that can just be pulled from a registry and will work out of the box.

Do I make a single image FROM a MySQL/MariaDB image and add Java and my application to it?

Do I make a single image FROM an OpenJDK image and add MySQL/MariaDB and my application to it?

is there another way to proceed?

Many thanks

1 Answer 1

1

A docker image is designed to run a single command, so although you could add your application files to a MySQL image (or vice-versa), you would struggle to get both running.

You were heading down the right track with docker-compose - that would let you define the two containers and how they’re run. This may actually give you a simpler deployment than standalone containers.

You may also wish to consider not shipping a MySQL container at all. Why not give your application the ability to create the required database, and add a config option for the user to tell it where to find the database server? If the user chooses to use a dockerized MySQL instance, they can just pull the vanilla mysql:latest image and configure the ports themselves.

Or once you’ve added that functionality to your application, you could then ship a Compose solution with the vanilla MySQL instance, requiring only one custom Dockerfile.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I'm afraid I still don't quite understand: If I make it a combination of containers, doesn't it mean that the image of my application will never be able to run by itself? Doesn't that negate the magic of "you pull it from the registry and it just works because it's got everything it needs inside it"?
That’s not the Docker magic. It’s an effective portable packaging around a single process and its dependencies that isolates the storage and network setups (I can configure both MySQL and PostgreSQL to listen on alternate ports in the same way without learning either database’s local configuration, for example). Pretty much every real useful Docker-based application requires multiple containers, and that’s normal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.