mgo Docker panic

36 visninger
Gå til det første ulæste opslag

Jean-Marcel Belmont

ulæst,
29. jun. 2017, 19.33.1429.06.2017
til mgo-users
db.go
```
package model

import (
 "log"
 "os"

)

// DBName is name of the database and CName is the name of document collection
const (
 DBName = "docker"
 CName  = "users"
)

var session *mgo.Session

// CreateDBSession creates mgo session
func CreateDBSession() {
 var err error
 session, err := mgo.Dial(os.Getenv("MONGO_URL"))
 defer session.Close()
 if err != nil {
   log.Fatal("Cannot Dial Mongo: ", err)
 }
 session.SetMode(mgo.Monotonic, true)
 collection := InitDB()
 CreateInitDocument(collection)
}

// GetSession will create a session if doesn't exist else will return existing session
func GetSession() *mgo.Session {
 if session == nil {
   CreateDBSession()
 }
 return session
}

// InitDB is to be used in main.go to initialize database
func InitDB() *mgo.Collection {
 c := NewContext()
 return c.DBCollection()
}

// CreateInitDocument initializes collection with 50 users
func CreateInitDocument(m *mgo.Collection) {
 models := GetModels()
 for _, model := range models {
   m.Insert(model)
 }
}

```

docker
-compose.yml
```
version: '3'
services:
 db:
   image: mongo:3.4.5
   ports:
     - "27017:27017"
   volumes:
     - /usr/local/var/mongodb:/data/db
 redis:
   image: redis:3.2.9-alpine
   volumes:
     - ./redis.conf:/usr/local/etc/redis/redis.conf
   ports:
     - "6379:6379"
 frontend:
   build:
     context: .
     dockerfile: Dockerfile
   environment:
     DOCKER: "true"
   volumes:
     - .:/var/www/app/:rw
     - /var/www/app/node_modules
   ports:
     - "3000:8081"
   restart: always
 test:
   image: mhart/alpine-node:8.0.0
   working_dir: /var/www/app
   volumes:
     - .:/var/www/app
   environment:
     NODE_ENV: testing
   command:
     /bin/sh -c "./node_modules/.bin/ava test/unit/*.js"
 backend:
   build:
     context: .
     dockerfile: Dockerfile-go
   command: go run main.go
   volumes:
     - .:/var/www/app/:rw
   ports:
     - "8080:8080"
   environment:
     MONGO_URL: "db:27017/docker"
     REDIS_URL: "redis"
   restart: always
   links:
     - db
     - redis

```

Dockerfile
```
FROM mhart/alpine-node:8.0.0

LABEL maintainer "marcel...@gmail.com"

# Set Environment variables
ENV appDir /var/www/app

RUN apk add --no-cache make gcc g++ python bash bzr git subversion openssh-client ca-certificates

# Set the work directory
RUN mkdir -p ${appDir}
WORKDIR ${appDir}

COPY . ${appDir}
COPY package.json ${appDir}/package.json

# Install npm dependencies and install ava globally
RUN npm install

# Add main node execution command
CMD ["npm", "run", "dev:docker"]

```

Dockerfile-go
```
FROM mhart/alpine-node:8.0.0

LABEL maintainer "marcel...@gmail.com"

# Set Environment variables
ENV appDir /var/www/app

RUN apk add --no-cache make gcc g++ python bash bzr git subversion openssh-client ca-certificates

# Set the work directory
RUN mkdir -p ${appDir}
WORKDIR ${appDir}

COPY . ${appDir}
COPY package.json ${appDir}/package.json

# Install npm dependencies and install ava globally
RUN npm install

# Add main node execution command
CMD ["npm", "run", "dev:docker"]

```

So at the moment I have 2 different containers. I can't seem to connect to mgo in docker. I run the following command `npm run build` and `npm run dev` but the connection to mgo doesn't connect
Svar alle
Svar til forfatter
Videresend
0 nye opslag