C
Coder.com•3y ago
Skerit

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:
resource "docker_image" "mariadb" {
name = "docker.io/mariadb:latest"
keep_locally = false
}

resource "docker_container" "mariadb" {
image = docker_image.mariadb.latest
name = "${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-mariadb"
ports {
internal = 3306
external = 3306
}
env = ["MARIADB_ROOT_PASSWORD=elastic"]
}

provider "mysql" {
endpoint = "localhost:3306"
username = "root"
password = "elastic"
}

resource "mysql_database" "app" {
name = "project_db_name"
}
resource "docker_image" "mariadb" {
name = "docker.io/mariadb:latest"
keep_locally = false
}

resource "docker_container" "mariadb" {
image = docker_image.mariadb.latest
name = "${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-mariadb"
ports {
internal = 3306
external = 3306
}
env = ["MARIADB_ROOT_PASSWORD=elastic"]
}

provider "mysql" {
endpoint = "localhost:3306"
username = "root"
password = "elastic"
}

resource "mysql_database" "app" {
name = "project_db_name"
}
Or is there another preferred way to do it?
6 Replies
Cian
Cian•3y ago
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_instance
Skerit
Skerit•3y ago
Oh, 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?
Phorcys
Phorcys•3y ago
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 time
No, because each workspace has it's own port space like with regular docker containers
Skerit
Skerit•3y ago
So each workspace has its own port space, but when I define a docker container like this:
resource "docker_container" "mongodb" {
image = docker_image.mongodb.latest
name = "${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-mongodb"
ports {
internal = 27017
external = 27017
}
}
resource "docker_container" "mongodb" {
image = docker_image.mongodb.latest
name = "${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-mongodb"
ports {
internal = 27017
external = 27017
}
}
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?
Phorcys
Phorcys•2y ago
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 ?
Skerit
Skerit•2y ago
Sure!
Want results from more Discord servers?
Add your server