首页 > 解决方案 > 尝试在后台的 docker 文件中运行 npm start 命令

问题描述

我是码头工人的新手。

我正在尝试使用 apache2、.net core、node js 在 ubuntu 上构建一个 docker 文件。我正在尝试在文件中运行示例 nodejs 应用程序。但是每次“npm run”命令在构建时出现,它都会创建开发服务器,而不是进入下一步。

有没有办法我可以在后台运行它或者像我们在终端上执行的 (ctl+c) 一样退出它?

这是泊坞窗文件:

    FROM ubuntu:18.04

RUN echo "welcome to yellow pages"
RUN apt-get update
RUN apt-get install -y tzdata
RUN apt-get install -y apache2

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_RUN_DIR /var/run/apache2
RUN mkdir -p ${APACHE_RUN_DIR}
ENV APACHE_PID_FILE /var/run/apache2/apache2.pid
RUN touch /etc/apache2/sites-available/yellowpages.conf
RUN echo '<VirtualHost *:80> \n\
    ServerName myserver.mydomain.com \n\
    ServerAdmin webmaster@localhost \n\
    DocumentRoot /var/www/html \n\
    ErrorLog ${APACHE_LOG_DIR}/error.log \n\
    CustomLog ${APACHE_LOG_DIR}/access.log combined \n\
    </VirtualHost> \n'\
>> /etc/apache2/sites-available/yellowpages.conf

RUN cat /etc/apache2/sites-available/yellowpages.conf
RUN echo 'ServerName myserver.mydomain.com' >> /etc/apache2/apache2.conf
RUN a2ensite yellowpages.conf
RUN a2dissite 000-default.conf
RUN echo 'Hello, docker' > /var/www/index.html


# installing nodejs12 on to the container 
RUN apt-get install -y curl
RUN apt-get install -y sudo
RUN curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
RUN sudo sudo apt-get install -y nodejs

# installing .net core 3.1 on to the container
RUN apt-get install -y wget
RUN wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt-get update
RUN apt-get install -y dotnet-sdk-3.1
RUN dotnet --list-sdks
RUN dotnet --list-runtimes

#Running a demo node app

RUN npx create-react-app my-app

# COPY /my-app/package*.json ./
# RUN npm start

ENTRYPOINT ["/usr/sbin/apache2"]
CMD ["-D", "FOREGROUND"]  
RUN cd my-app && nohup npm start
EXPOSE 80 
EXPOSE 3000

这是我卡住的地方的输出

输出

因为这个 Docker 镜像没有被创建,因为它卡在这一步。

谁能帮我解决这个问题?

标签: dockerdockerfile

解决方案


您将需要运行一个像 supervisord 这样的主管程序作为 docker 的入口点,这样当它启动时,Apache 和 NPM 可以一起启动。

尝试https://riptutorial.com/docker/example/14132/dockerfile-plus-supervisord-conf作为起点。

一个轻量级的替代方法是将 shell-scrpt 作为您运行 apache 和 Node 进程的起点,如此处所述

#!/bin/bash

# Run npm in the background
npm start & 

# run apache in the foreground
/usr/sbin/apache2

DockerFile

...
ENTRYPOINT ["/mystart_script.sh"]
...

推荐阅读