title: Connecting Golang and Postgres docker containers
body: <p>I'm trying to run a golang server at localhost:8080 that uses a postgres database. I've tried to containerize both the db and the server but can't seem to get them connected.</p>
  <p><strong>main.go</strong></p>
  <pre><code>func (a *App) Initialize() {
      var db *gorm.DB
      var err error
      envErr := godotenv.Load(&quot;.env&quot;)
      if envErr != nil {
          log.Fatalf(&quot;Error loading .env file&quot;)
      }
      var dbString = fmt.Sprintf(&quot;port=5432 user=sample dbname=sampledb sslmode=disable password=password host=db&quot;)
      
      db, err = gorm.Open(&quot;postgres&quot;, dbString)
      if err != nil {
          fmt.Printf(&quot;failed to connect to databse\n&quot;,err)
      }
      a.DB=model.DBMigrate(db)
      a.Router = mux.NewRouter()
      a.setRoutes()
  }
  
  //Get : get wrapper
  func (a *App) Get(path string, f func(w http.ResponseWriter, r *http.Request)) {
      a.Router.HandleFunc(path, f).Methods(&quot;GET&quot;)
  }
  
  //Post : post wrapper
  func (a *App) Post(path string, f func(w http.ResponseWriter, r *http.Request)) {
      a.Router.HandleFunc(path, f).Methods(&quot;POST&quot;)
  }
  
  //Run : run on port
  func (a *App) Run(port string) {
      handler := cors.Default().Handler(a.Router)
      log.Fatal(http.ListenAndServe(port, handler))
  }
  
  func (a *App) setRoutes() {
      a.Get(&quot;/&quot;, a.handleRequest(controller.Welcome))
      a.Get(&quot;/users&quot;, a.handleRequest(controller.GetUsers))
      a.Get(&quot;/user/{id}&quot;, a.handleRequest(controller.GetUser))
      a.Post(&quot;/login&quot;, a.handleRequest(controller.HandleLogin))
      a.Post(&quot;/users/add&quot;, a.handleRequest(controller.CreateUser))
      a.Post(&quot;/validate&quot;, a.handleRequest(controller.HandleValidation))
  }
  
  func main() {
      app := &amp;App{}
      app.Initialize()
      app.Run(&quot;:8080&quot;)
  
  }
  
  
  </code></pre>
  <p><strong>server Dockerfile</strong></p>
  <pre><code>FROM golang:latest
  
  RUN mkdir /app
  WORKDIR /app/server
  
  COPY go.mod .
  COPY go.sum .
  RUN go mod download
  COPY . .
  </code></pre>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: '3.7'
  services:
      db:
          image: postgres
          container_name: ep-db
          environment:
              - POSTGRES_PORT=${DB_PORT}
              - POSTGRES_USER=${DB_USERNAME}  
              - POSTGRES_PASSWORD=${DB_PASSWORD}
              - POSTGRES_DB=${DB_NAME}
              
          ports:
              - '5432:5432'
          volumes:
              - ./db:/var/lib/postgresql/data&quot;
          networks:
              - internal
      server:
          container_name: ep-server
          build:
              context: ./server
              dockerfile: Dockerfile
          command: bash -c &quot;go build &amp;&amp; ./server -b 0.0.0.0:8080 --timeout 120&quot;
          volumes:
              - './server:/app/server'
          expose:
              - 8080
          depends_on: 
              - db
          networks:
              - internal
          stdin_open: true
  volumes:
      db:  
      server:
  networks:
      internal:
        driver: bridge
  
  </code></pre>
  <p>I have some get and post requests that return the right values when i run it locally on my computer (for ex. localhost:8080/users would return a JSON full of users from the database) but when I use curl inside the server container, I don't get any results. I am new to docker, Is there something wrong with what I'm doing so far?</p>