Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created September 9, 2016 21:37
Show Gist options
  • Save wesleybliss/29d4cce863f5964a3eb73c42501d99e4 to your computer and use it in GitHub Desktop.
Save wesleybliss/29d4cce863f5964a3eb73c42501d99e4 to your computer and use it in GitHub Desktop.
Docker Compose with example App & Mongo
version: '2'
services:
myapp:
build: .
container_name: "myapp"
image: debian/latest
environment:
- NODE_ENV=development
- FOO=bar
volumes:
- .:/var/www:rw
- /tmp/.X11-unix:/tmp/.X11-unix:rw
ports:
- 3000:3000
- 9000:9000
links:
- mongodb
depends_on:
- mongodb
mongodb:
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
command: mongod --smallfiles --logpath=/dev/null # --quiet
@ZabdiAG
Copy link

ZabdiAG commented Oct 24, 2017

Hi, great example!!
what about create an user with password auth in the mongo container?

@vicmars5
Copy link

vicmars5 commented Nov 4, 2017

Nice really helpful!

@anguelov
Copy link

environment:
    MONGODB_USER="user" 
    MONGODB_PASS="pass"

@grcmc
Copy link

grcmc commented Dec 13, 2017

Hi,
I want to prepare a MongoDB and unittests cointanters. How to configure mongo section to terminate it, when python app finish whit exit code 0?

@omar331
Copy link

omar331 commented Jan 19, 2018

Huge thanks, @wesleybliss!

@Nithya25Mari
Copy link

Nithya25Mari commented Feb 6, 2018

Hi,
I am using docker-compose up file it shows application is running but unable to login to the system

docker-compose.yml

version : '3'
services :
sampledocker:
build: .
ports:
- "4200:4200"
depends_on:
- mongo
mongo:
image: mongo:3.4.2
ports:
- "27017:27017"
volumes:
- data:/data/db
volumes:
data:

package.json:

{
"name": "sampleproject",
"version": "1.0.0",
"main": "server.js",
"author": "Nithya nitikishu@gmail.com",

"scripts": {
"start": "node server.js"
},
"dependencies": {
"async": "^2.1.5",
"bcrypt-nodejs": "0.0.3",
"body-parser": "~1.4.2",
"bootstrap-ui-datetime-picker": "^2.5.4",
"chalk": "^1.1.3",
"connect-mongo": "^1.3.2",
"dotenv": "^4.0.0",
"ejs": "^2.5.6",
"errorhandler": "^1.5.0",
"express": "~4.5.1",
"express-session": "^1.15.1",
"express-validator": "^3.1.2",
"handlebars": "^4.0.6",
"method-override": "~2.0.2",
"mongoose": "~3.8.0",
"morgan": "^1.8.1",
"multer": "^1.3.0",
"nodemailer": "^3.1.7",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"request": "^2.81.0"
}
}

server.js:

var dbpath = "mongodb://mongo:27017/SampleDB";
var SESSION_SECRET = "sample";
var fs = require('fs')
var App_PORT = 4200;
mongoose.connect(dbpath, function(err) {
if (err) {
console.log('MongoDB connection error: ' + err);
// return reject(err);
process.exit(1);
}
});

app.use(session({
resave: true,
saveUninitialized: true,
secret: SESSION_SECRET,
store: new MongoStore({
url: dbpath,
autoReconnect: true
})
}));

docker-quickstart-terminal

$ docker-compose up
Starting sample_mongo_1 ...
Starting sample_mongo_1 ... done
Recreating sample_sampledocker_1 ...
Recreating sample_sampledocker_1 ... done
Attaching to sample_mongo_1, sample_sampledocker_1
mongo_1 | 2018-02-06T08:09:59.924+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=b2d80c378f8b
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] db version v3.4.2
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] modules: none
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] build environment:
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] distmod: debian81
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] distarch: x86_64
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] target_arch: x86_64
mongo_1 | 2018-02-06T08:09:59.927+0000 I CONTROL [initandlisten] options: {}
mongo_1 | 2018-02-06T08:09:59.933+0000 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active
storage engine to 'wiredTiger'.
mongo_1 | 2018-02-06T08:09:59.933+0000 I STORAGE [initandlisten]
mongo_1 | 2018-02-06T08:09:59.933+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo_1 | 2018-02-06T08:09:59.933+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo_1 | 2018-02-06T08:09:59.933+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=489M,session_max=20000,eviction=(threads_max=4),config_
base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statist
ics_log=(wait=0),
mongo_1 | 2018-02-06T08:10:00.551+0000 I CONTROL [initandlisten]
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten]
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten]
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten]
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
mongo_1 | 2018-02-06T08:10:00.552+0000 I CONTROL [initandlisten]
sampledocker_1 |
sampledocker_1 | > sampleproject@1.0.0 start /C:Usersuser2Sample
sampledocker_1 | > node server.js
sampledocker_1 |
sampledocker_1 | Γ£ô server running on port 4200
mongo_1 | 2018-02-06T08:10:00.554+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1 | 2018-02-06T08:10:00.555+0000 I NETWORK [thread1] waiting for connections on port 27017
mongo_1 | 2018-02-06T08:10:01.875+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42210 #1 (1 connection now open)
mongo_1 | 2018-02-06T08:10:01.876+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42212 #2 (2 connections now open)
mongo_1 | 2018-02-06T08:10:01.883+0000 I NETWORK [conn2] received client metadata from 172.18.0.3:42212 conn2: { driver: { name: "nodejs", version: "2.2.34" }, os
: { type: "Linux", name: "linux", architecture: "x64", version: "4.4.74-boot2docker" }, platform: "Node.js v9.4.0, LE, mongodb-core: 2.1.18" }
mongo_1 | 2018-02-06T08:10:01.894+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42214 #3 (3 connections now open)
mongo_1 | 2018-02-06T08:10:01.902+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42216 #4 (4 connections now open)
mongo_1 | 2018-02-06T08:10:01.905+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42218 #5 (5 connections now open)
mongo_1 | 2018-02-06T08:10:01.908+0000 I NETWORK [thread1] connection accepted from 172.18.0.3:42220 #6 (6 connections now open)
mongo_1 | 2018-02-06T08:10:01.913+0000 I INDEX [conn2] build index on: SampleDB.sessions properties: { v: 2, key: { expires: 1 }, name: "expires_1", ns: "SampleDB.ses
sions", expireAfterSeconds: 0 }
mongo_1 | 2018-02-06T08:10:01.913+0000 I INDEX [conn2] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongo_1 | 2018-02-06T08:10:01.915+0000 I INDEX [conn2] build index done. scanned 0 total records. 0 secs
sampledocker_1 | Press CTRL-C to stop
sampledocker_1 |

192.168.99.100:4200 In this host application is running
but unable to login after clicking the login button and also nothing is happening after entering username and password... What is wrong ???

@dongmai
Copy link

dongmai commented Jul 17, 2018

Thanks!!!

@yami12376
Copy link

ERROR: Cannot locate specified Dockerfile: Dockerfile

@danielecr
Copy link

Hi,
I want to prepare a MongoDB and unittests cointanters. How to configure mongo section to terminate it, when python app finish whit exit code 0?

Maybe too late, but ...

--abort-on-container-exit \
    --exit-code-from [containername]

--exit-code-from is usefull as well for testing (i.e. I use this as jenkins test step)

@SarasaGunawardhana
Copy link

helpful!

@aleksb86
Copy link

same than yami12376:
ERROR: Cannot locate specified Dockerfile: Dockerfile
WIDW?

@ralyodio
Copy link

for some reason my data directory on the host is owned by influxdb:root....anyone know why?

@LittleBuster
Copy link

Thank you!

@vaibhav-nunna
Copy link

/data/db:/data/db
data:/data/db:rw" is used in service "db" but no declaration was found in the volumes section.

May I know what might be the issue,
I'm using docker for windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment