Is it possible to enable networking between coder docker containers?

I am working on a mongod image. My ideal use case would be to have a coder template that creates and manages multiple mongodb databases that I can use in my other workspaces. I started creating a template with docker: https://github.com/T0mThomson/coder-templates/tree/main/mongodb It succesfully runs mongodb and a web ui to manage the databses (mongo-express). However withouth manualy messing with the docker CLI in my terminal I can't automaticly get it to enable connection between other docker containers / workspaces in coder. I would like to expose the port 27017 in the default docker network OR enable networking between two coder workspaces, however I don't think that's possible yet?
GitHub
coder-templates/mongodb at main · T0mThomson/coder-templates
Contribute to T0mThomson/coder-templates development by creating an account on GitHub.
No description
19 Replies
Scott
Scott•2y ago
I'm interested in doing something similar, but my basis is k8s. And with k8s, I believe you can set your app's (the one in the coder container) to the URL for the MongoDB service. I haven't gotten that far yet, but it should be relatively straight forward. Again, I'm definitely interested in any other solutions that there might be out there. 🙂
Tom Thomson
Tom ThomsonOP•2y ago
That was a really good hint. I edited my template and added a new app:
resource "coder_app" "mongodb" {
agent_id = coder_agent.main.id
slug = "mongo-db"
display_name = "MongoDB Server"
url = "http://localhost:27017"
icon = "https://cdn.iconscout.com/icon/free/png-256/mongodb-3629020-3030245.png"
subdomain = true
share = "public"

healthcheck {
url = "http://localhost:27017"
interval = 5
threshold = 6
}
}
resource "coder_app" "mongodb" {
agent_id = coder_agent.main.id
slug = "mongo-db"
display_name = "MongoDB Server"
url = "http://localhost:27017"
icon = "https://cdn.iconscout.com/icon/free/png-256/mongodb-3629020-3030245.png"
subdomain = true
share = "public"

healthcheck {
url = "http://localhost:27017"
interval = 5
threshold = 6
}
}
I set share = "public" so no authentification is required via coder. When I open the app via http it looks like the mongodb server is responding:
No description
Tom Thomson
Tom ThomsonOP•2y ago
And I set subdomain = true so mongo connecotrs wouldn't be confused by the url.
Tom Thomson
Tom ThomsonOP•2y ago
However I still couldn't manage to get it to connect to mongodb:
No description
Tom Thomson
Tom ThomsonOP•2y ago
I tried no port (results in 27017) 443 and 80.
Scott
Scott•2y ago
No clue. What you are trying to do seems strange to me. Like, why would the coder URL offer a message from Mongo? And, why would you have an external URL to get to your MongoDB server? That is a basic no-no AFAIK. Unless you are inside a personal network.
Tom Thomson
Tom ThomsonOP•2y ago
Looks like when you access your mongodb server via http protocol it sends you a response. I wouldn't want to expose it to the public, but with port forward you would need to authentificate with your coder account. I don't think that would work. I am testing solutions to make it work in my current coder setup.
Scott
Scott•2y ago
Ok. What are you running it all on?
Tom Thomson
Tom ThomsonOP•2y ago
I am currently running coder on a single ubuntu server with docker containers as workspaces.
Scott
Scott•2y ago
I'm not too fluent with Docker, but I believe when you start a pod, it should be automatically assigned an IP Address. This address is what your app in the coder workspace should be using to access MongodB. You can't use http to access mongo.
Tom Thomson
Tom ThomsonOP•2y ago
Yes thats possible, however you first need to configure the docker container to open the ports in the docker network.
Tom Thomson
Tom ThomsonOP•2y ago
I know that it's not possible to connect to mongodb via http. However I wanted to show that the server should be accessible. Maybe its not working because coder port forward only allows http(s) protocol and not others like https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/
Scott
Scott•2y ago
So, you are trying to start a mongodb database inside a workspace? I'm confused. LOL! 😄 I would have imagined you'd start the Mongo server in its own container, before you create workspaces. My thinking is, a workspace should only hold the app that needs to be worked on. Nothing else.
Tom Thomson
Tom ThomsonOP•2y ago
Correct, I am trying to create a template to run mongodb databases inside a workspace.
Scott
Scott•2y ago
@Tom Thomson - Hmm... ok. I'm assuming your only using the workspace to access Terraform to build the container, right? Your intention isn't to actually develop in that container, right?
Tom Thomson
Tom ThomsonOP•2y ago
@Scott yes, correct
Scott
Scott•2y ago
@Tom Thomson - I went looking yesterday, but closed the browser tab. At any rate, in the example templates there was one where Nginx was being added to the workspace. I'd say that is similar to what you need? I'd not try to add it in as the coder_app. All just a guess on my part though. 🙂
Tom Thomson
Tom ThomsonOP•2y ago
I found a solution where I created a new docker network using docker network create coder and then modified the template main.tf and added the following config to the container resource:
resource "docker_container" "workspace" {
...
networks_advanced {
name = "coder"
aliases = [ data.coder_workspace.me.name ]
}
}
resource "docker_container" "workspace" {
...
networks_advanced {
name = "coder"
aliases = [ data.coder_workspace.me.name ]
}
}
This makes the docker container available in the docker network "coder" and sets a network alias to the workspace name so you can reach it easly withoud needing to retrieve the containers ip address (which can potentialy change on restart)
Codercord
Codercord•2y ago
Marked the thread as resolved.

Did you find this page helpful?