使用docker-compose

时间:2015-06-30 15:47:48

标签: docker boot2docker dockerfile docker-compose

我尝试运行一个带有2个容器的实例,1个带有mysql的容器和带有node的其他容器。

docker-compose.yml 文件中:

api:
  build: ./server
  ports:
    - 8001:8001
  links:
    - mysql:mysql

mysql:
  image: mysql
  environment:
    MYSQL_DATABASE: ghostDB
    MYSQL_ROOT_PASSWORD: root
  volumes:
    - /data/mysql:/var/lib/mysql

服务器/ 的Dockerfile:

FROM node:0.12

ENV PORT 8001
ENV MYSQL_DATABASE ghostDB
ENV MYSQL_USER root
ENV MYSQL_PASSWORD root
ENV MYSQL_HOST mysql
ENV MYSQL_PORT 3306

ENV API_DIR /usr/src/server-celerative

COPY . \${API_DIR}

WORKDIR \${API_DIR}

RUN npm install

RUN node index.js

index.js

var db = mysql.createConnection({
    host: 'mysql',
    port: '3306',
    user: 'root',
    password: 'root',
    database: 'ghostDB'
});

但我有输出:

Error: getaddrinfo ENOTFOUND mysql
    at errnoException (dns.js:44:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
    --------------------

我不明白为什么不工作。

有人帮忙吗?

注意:我使用boot2docker。

2 个答案:

答案 0 :(得分:1)

docker-compose v1无法确保在启动节点容器之前完全启动和初始化mysql进程。因此,您的节点容器负责测试mysql是否可用以及等待/重试,直到它可用为止。

您可以在应用程序中使用节点执行此操作(pooling-connections可能就是这样);或者提供一个测试mysql连接的boostrap shell脚本,一旦可用就会启动节点。

答案 1 :(得分:0)

@Component({ selector: 'ec-money-field', template: ` <md-input-container *ngIf="editMode"> <input #input mdInput class="value" type="text" (input)="updateInnerValue(input.value)" (blur)="formatTextValue()" [formControl]="control" /> </md-input-container> `, providers: [ {provide: NG_VALUE_ACCESSOR, multi: true, useExisting: forwardRef(() => MoneyFieldComponent)}, ] }) export class MoneyFieldComponent implements OnInit, ControlValueAccessor { private valueInCents = 0; control = new FormControl(0); private onChange: Function = (_: any) => {}; private onTouch: Function = (_: any) => {}; constructor() { } @Input() get value(): number { return this.valueInCents; }; // if you update the component by using the value property, // propagate that change to the text field set value(newValueInCents: number) { this.valueInCents = newValueInCents; this.control.setValue(centsToDollars(newValueInCents)); } ngOnInit() { } // convert the masked value - i.e. what the user types // into the actual numerical value that will be stored // You'll have to provide your own conversion function // to convert the user typing 1(855) 555 1234 to 1865551234 updateInnerValue(dollarValueString: string) { this.valueInCents = dollarsToCents(dollarValueString); this.onChange(this.valueInCents); } formatTextValue() { this.value = this.value; } writeValue(newValue: number): void { this.value = newValue; } registerOnChange(fn: any): void { this.onChange = fn; } registerOnTouched(fn: any): void { this.onTouch = fn; } } 之后的mysql部分添加行container_name: mysql

可以添加以下Bash代码以确保3306端口已打开:

image: mysql