关于使用docker-compose


准备使用compose yml 创建两个容器,按照官网的栗子https://docs.docker.com/compos ... setup 很愉快的执行成功,然后我把redis换成mongodb,确一直不成功,不知道是哪里出了问题,请大家看看
app.py
from flask import Flask
from pymongo import MongoClient
import random
app = Flask(__name__)
client = MongoClient('mongodb')
db=client

@app.route('/')
def hello():
db.col.insert({"hits":random.random()})
return 'Hello World! I have been seen %s times.' % (db.col.count())

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)

Dockerfile

FROM python:2.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD 

docker-compose.yml
version: '2'
services:
web:
    build: .
    ports:
        - "5000:5000"
mongodb:
    image: mongo
    ports:
        - "27017:27017"


然后下面是执行的日志
mongo_1  | 2018-04-13T05:46:10.107+0000 I STORAGE   shutdown: removing fs lock...
mongo_1  | 2018-04-13T05:46:10.107+0000 I CONTROL   now exiting
mongo_1  | 2018-04-13T05:46:10.107+0000 I CONTROL   shutting down with code:0


如果有不充足的,提醒我,我会立马补上!!!

其实我主要想问的是,使用compose之后,里面的A服务如果想使用里面的B数据库,host可以直接写B的名字吗?
已邀请:

vector4wang - 90后IT男、公司扫地僧

赞同来自:


这个是compose 下的网络信息

<pre>
<span>
</pre>

vector4wang - 90后IT男、公司扫地僧

赞同来自:


这个是docker-compose up 的日期信息

<pre>
Recreating python_web_1
Starting python_mongodb_1
Attaching to python_mongodb_1, python_web_1
mongodb_1 | 2018-04-13T06:45:11.988+0000 I CONTROL MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=27887cb46207
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL db version v3.6.3
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL OpenSSL version: OpenSSL 1.0.1t 3 May 2016
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL allocator: tcmalloc
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL modules: none
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL build environment:
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL distmod: debian81
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL distarch: x86_64
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL target_arch: x86_64
mongodb_1 | 2018-04-13T06:45:11.994+0000 I CONTROL options: { net: { bindIpAll: true } }
mongodb_1 | 2018-04-13T06:45:11.994+0000 I - Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb_1 | 2018-04-13T06:45:11.994+0000 I STORAGE

mongodb_1 | 2018-04-13T06:45:11.994+0000 I STORAGE ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongodb_1 | 2018-04-13T06:45:11.994+0000 I STORAGE ** See http://dochub.mongodb.org/core ... ystem
mongodb_1 | 2018-04-13T06:45:11.994+0000 I STORAGE wiredtiger_open config: create,cache_size=407M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongodb_1 | 2018-04-13T06:45:15.917+0000 I STORAGE WiredTiger message , txn-recover: Main recovery loop: starting at 6/6016
mongodb_1 | 2018-04-13T06:45:16.127+0000 I STORAGE WiredTiger message , txn-recover: Recovering log 6 through 7
mongodb_1 | 2018-04-13T06:45:16.235+0000 I STORAGE WiredTiger message , txn-recover: Recovering log 7 through 7
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL

mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** WARNING: Access control is not enabled for the database.
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** Read and write access to data and configuration is unrestricted.
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL

mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL

mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** We suggest setting it to 'never'
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL

mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL ** We suggest setting it to 'never'
mongodb_1 | 2018-04-13T06:45:20.339+0000 I CONTROL

mongodb_1 | 2018-04-13T06:45:20.660+0000 I FTDC Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongodb_1 | 2018-04-13T06:45:20.660+0000 I NETWORK waiting for connections on port 27017
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1 | * Restarting with stat
web_1 | * Debugger is active!
web_1 | * Debugger PIN: 300-576-521
</pre>

我快崩溃了

vector4wang - 90后IT男、公司扫地僧

赞同来自:


我改成了这个ip 172.19.0.2 就行了,真的是很奇怪

<pre>
"27887cb46207cb3202bb5863169b81177f30165a9f7a8d120842ae2735ac0b4a": {
"Name": "python_mongodb_1",
"EndpointID": "ca3fc6f3d4782e14ae21cb8001ac482ce5677d19802be6df54738ca04ec7f090",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
}
</pre>

vector4wang - 90后IT男、公司扫地僧

赞同来自:

vector4wang - 90后IT男、公司扫地僧

赞同来自:


今天在我自己的虚拟机上试了一下,竟然可以!
感觉是我用的阿里云的ecs可能在网络这块有点问题,看来需要研究下network了

要回复问题请先登录注册