关于使用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['datas']

@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 ["python","app.py"]

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  [signalProcessingThread] shutdown: removing fs lock...
mongo_1  | 2018-04-13T05:46:10.107+0000 I CONTROL  [signalProcessingThread] now exiting
mongo_1  | 2018-04-13T05:46:10.107+0000 I CONTROL  [signalProcessingThread] shutting down with code:0


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

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

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

赞同来自:


这个是compose 下的网络信息
[
{
    "Name": "python_default",
    "Id": "25e8bb8f26abf268fbac3e187ac3ba7e55a2552427372878e16a1337cd3f65f3",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": null,
        "Config": [
            {
                "Subnet": "172.19.0.0/16",
                "Gateway": "172.19.0.1/16"
            }
        ]
    },
    "Internal": false,
    "Containers": {
        "27887cb46207cb3202bb5863169b81177f30165a9f7a8d120842ae2735ac0b4a": {
            "Name": "python_mongodb_1",
            "EndpointID": "763c9c3a5b26e1e0704f6c979680e855f888a3cc855cbb1f1e12f3388015594e",
            "MacAddress": "02:42:ac:13:00:02",
            "IPv4Address": "172.19.0.2/16",
            "IPv6Address": ""
        },
        "56278bce394de2365a233d89219be83657084fae428e7b67324021d7a49fc04f": {
            "Name": "python_web_1",
            "EndpointID": "d0694cbe2dcf6a4ada20c66e913d27a6ada2ad23e908f0dc2a8d2f9ee4817bef",
            "MacAddress": "02:42:ac:13:00:03",
            "IPv4Address": "172.19.0.3/16",
            "IPv6Address": ""
        }
    },
    "Options": {},
    "Labels": {}
}
]

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

赞同来自:


这个是docker-compose up 的日期信息
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  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=27887cb46207
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] db version v3.6.3
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] modules: none
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] build environment:
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten]     distmod: debian81
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb_1  | 2018-04-13T06:45:11.994+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongodb_1  | 2018-04-13T06:45:11.994+0000 I -        [initandlisten] 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  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:11.994+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongodb_1  | 2018-04-13T06:45:11.994+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongodb_1  | 2018-04-13T06:45:11.994+0000 I STORAGE  [initandlisten] 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  [initandlisten] WiredTiger message [1523601915:917708][1:0x7f1826abea00], txn-recover: Main recovery loop: starting at 6/6016
mongodb_1  | 2018-04-13T06:45:16.127+0000 I STORAGE  [initandlisten] WiredTiger message [1523601916:127137][1:0x7f1826abea00], txn-recover: Recovering log 6 through 7
mongodb_1  | 2018-04-13T06:45:16.235+0000 I STORAGE  [initandlisten] WiredTiger message [1523601916:235056][1:0x7f1826abea00], txn-recover: Recovering log 7 through 7
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
mongodb_1  | 2018-04-13T06:45:20.339+0000 I CONTROL  [initandlisten] 
mongodb_1  | 2018-04-13T06:45:20.660+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongodb_1  | 2018-04-13T06:45:20.660+0000 I NETWORK  [initandlisten] 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


我快崩溃了

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

赞同来自:


我改成了这个ip 172.19.0.2 就行了,真的是很奇怪
"27887cb46207cb3202bb5863169b81177f30165a9f7a8d120842ae2735ac0b4a": {
            "Name": "python_mongodb_1",
            "EndpointID": "ca3fc6f3d4782e14ae21cb8001ac482ce5677d19802be6df54738ca04ec7f090",
            "MacAddress": "02:42:ac:13:00:02",
            "IPv4Address": "172.19.0.2/16",
            "IPv6Address": ""
        }

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

赞同来自:

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

赞同来自:


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

要回复问题请先登录注册