简体   繁体   English

Docker-compose和节点容器不是主要的容器

[英]Docker-compose and node container as not the primary one

I'm new to Docker and I've successfully set up the PHP/Apache/MySQL. 我是Docker的新手,并且已经成功设置了PHP / Apache / MySQL。 But once I try to add the node container (in order to use npm ) it always shuts the container down upon composing up. 但是一旦我尝试添加node容器(为了使用npm ),它总是在组合后关闭容器。 And yes, I understand that I can use node directly without involving docker , but I find it useful for myself. 是的,我知道我可以直接使用node ,而无需涉及docker ,但是我发现它对自己很有用。

And as for composer , I want to use volumes in the node container in order to persist node_modules inside of src folder. 至于composer ,我想在node容器中使用卷,以便将node_modules持久node_modulessrc文件夹中。

I compose it up using docker-compose up -d --build command. 我使用docker-compose up -d --build命令将其组成。 During composing it shows no errors (even node container seems to be successfully built). 在编写过程中,它没有显示任何错误(即使node容器似乎也已成功构建)。 If it might help, I can share the log file (it's too big to include it here). 如果有帮助,我可以共享日志文件(它太大了,无法在此处包含它)。

PS. PS。 If you find something that can be improved, please let me know. 如果您发现可以改进的地方,请告诉我。 Thank you in advance! 先感谢您!

Dockerfile Docker文件

FROM php:7.2-apache
RUN apt-get update
RUN a2enmod rewrite

RUN apt-get install zip unzip zlib1g-dev
RUN docker-php-ext-install pdo pdo_mysql mysqli zip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer global require laravel/installer
ENV PATH="~/.composer/vendor/bin:${PATH}"

docker-compose.yml docker-compose.yml

version: '3'

services:
  app:
    build:
      .
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
      - nodejs
    ports:
      - 80:80

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: qwerty

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql:db
    ports:
      - 8765:80
    environment:
      MYSQL_ROOT_PASSWORD: qwerty
      PMA_HOST: mysql
    depends_on:
      - mysql

  nodejs:
    image: node:9.11
    volumes:
      - ./src:/var/www/html

As this Dockerfile you are using shows, you are not actually runing any application in the node container so as soon as it builds and starts up - it shuts down because it has nothing else to do. 由于这个 Dockerfile您使用的节目,你实际上并没有那么尽快乳宁在节点容器中的任何应用程序,它建立并启动-它关闭,因为它没有其他事情可做。

Solution is simple - provide a application that you want to run into the container and run it like: 解决方案很简单-提供一个您要运行到容器中并运行的应用程序,如下所示:

I've modified a part of your compose file 我已经修改了您的撰写文件的一部分

  nodejs:
    image: node:9.11
    command: node app.js
    volumes:
      - ./src:/var/www/html

Where app.js is the script in which your app is written, you are free to use your own name. 其中app.js是编写应用程序的脚本,您可以自由使用自己的名称。

edit providing a small improvement you asked for 编辑提供您要求的小改进

You are not waiting until your database is fully initialized ( depends_on is not capable of that), so take a look at one of my previous answers dealing with that problem here 您不会等到数据库完全初始化之后( depends_on无法执行此操作),因此请在此处查看我之前针对该问题的答案之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM