Setting up a database in a template
First of all: Coder is running on my server at the office, and I want to host the workspaces on it too with Docker.
I've got a PHP project I'm trying to create a template for.
It requires a MySQL/MariaDB database to run, so I'm trying to set that up now, but I'm not sure how to go about it.
Do I create another terraform resource for it, like:
Or is there another preferred way to do it?
6 Replies
Hi @Skerit most database Docker images have some way of initialising various things through environment variables and such. I see you've already done that with the mariadb image. I don't think there's any real need to create the database as a resource if you're doing it with a Docker image. In fact, I can see that the official MySQL terraform provider is archived https://github.com/hashicorp/terraform-provider-mysql
On cloud platforms, yes you'd probably have to create the database instances -- for example, there's an
aws_db_instance
resource type here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instanceOh, so my mariadb
docker_container
is enough, there's no need to setup the provider "mysql"
and resource "mysql_database"
too?
That's good.
In my current example the database will always listen to port 3306
, so I will only be able to have 1 running workspace at a time.
Is there a way to generate a random port number, or should it be set up in another way?Oh, so my mariadb docker_container is enough, there's no need to setup the provider "mysql" and resource "mysql_database" too?indeed
so I will only be able to have 1 running workspace at a timeNo, because each workspace has it's own port space like with regular docker containers
So each workspace has its own port space, but when I define a docker container like this:
I get this error:
Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use
Which makes sense, because there is another, non-coder MongoDB container exposing that port to the host machine.
So how do I make that work? 🤔
Or am I going about this all wrong, and should I create a new Docker image (based on codercom/code-server:latest
) that contains all the services I need?this is what I do
you'd just be running both your code-server and the mongodb container
I don't know if there's an effective way to connect both
that would be an interesting thing
but yeah, just do this
@Skerit is this good to close ?
Sure!