为什么我需要Docker用于nodejs?

时间:2017-06-04 15:34:11

标签: node.js docker

我对Docker比较陌生。而且非常棒。

嗯,我理解像PHP这样的语言。我认为使用Docker确实有意义,因为PHP环境。

但是为什么我需要在Node.js上使用docker,而我可以简单地'npm install'和'npm start'而且我已经完成了。

我相信我在这里错过了一些东西。我错过了什么?

1 个答案:

答案 0 :(得分:0)

值得注意的要点是所有应用程序都依赖于机器构建。可以在控制台上启动简单的应用程序(在Node或PHP或任何东西中),但它们仍然需要服务器和包管理器。

该列表可能包含以下任何内容:

  • 选择基本服务器是为了平衡速度,图像大小,安全方法或更新谨慎(Ubuntu用于功能,CentOS用于警告,Alpine用于图像大小等)
  • 运行时软件,如PHP,Apache,NginX,Node等。
  • 在内部缓存文件夹上设置的服务器写入权限
  • 安装特定版本的cURL
  • 本地缓存或队列安装(Redis,Memcache等)
  • 安装了特定的PHP扩展,例如数据库扩展(或其他语言的系统等效)
  • 用户帐户
  • 包装管理软件,如Composer和NPM
  • 您的应用程序,从版本控制或构建服务器获取
  • 为您的应用安装软件包
  • 安装字体
  • 设置时区和本地化

这是我最近写的一个Dockerfile(用于一组相对简单的功能测试):

# Docker build script for the Command Agent system

FROM ubuntu

# Do a system update
RUN apt-get update && apt-get install -y

# Basic dependencies
RUN apt-get install -y git openssl

# Install PHP
# Composer needs openssl, json, phar, iconv/mbstring
# Composer recommends zlib
# PHPUnit needs dom, mbstring
RUN apt-get install -y php php-json php-phar php-mbstring php-dom php-zip
# @todo What names are php-ssl and php-zlib under?

# Install SSH server
# Expect is to handle tty automation
# Netcat is required to send messages
RUN apt-get install -y openssh-server expect netcat

RUN adduser --disabled-password --gecos "" nonpriv
# It looks like passwordless access does not work unless the user has a password!
RUN echo 'nonpriv:Password123' | chpasswd

# We need to be able to ask for a root password (so lets give it one)
RUN echo 'root:Password123' | chpasswd

# Copy over installation scripts
COPY install /tmp/install
RUN chmod u+rx /tmp/install/_*.sh && \
    chown -R nonpriv:nonpriv /tmp/install

# Install Composer
# See https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
RUN cd /tmp && sh /tmp/install/composer.sh

# Copy dependendencies
COPY composer.json /root/command-agent/composer.json
COPY composer.lock /root/command-agent/composer.lock

# Install deps using Composer (include dev deps)
# @todo Don't run composer as root, https://getcomposer.org/root
RUN cd /root/command-agent && php /tmp/composer.phar install

# Apply patch to PHPUnit. This is a bit hacky, but the only way I can see to
# fully turn off output buffering. Unfortunately these methods are private,
# so standard approaches to removing them (an override method) would not work.
# The only other alternative is using runkit, and I think that might be +more+
# hacky :)
COPY patch /root/command-agent/patch
RUN patch -p1 \
    /root/command-agent/vendor/phpunit/phpunit/src/Framework/TestCase.php \
    /root/command-agent/patch/disable_ob.patch

# Copy code
COPY bin /root/command-agent/bin
COPY src /root/command-agent/src
COPY test /root/command-agent/test
COPY entry-point.sh /root/command-agent/entry-point.sh
COPY expect.sh /root/command-agent/expect.sh
COPY phpunit.xml /root/command-agent/phpunit.xml
RUN ln -s /root/command-agent/vendor/bin/phpunit /root/command-agent/phpunit

# Allow non-priv user to read/exec root files
RUN chown -R root:nonpriv /root && \
    chmod -R g-w /root && \
    chmod -R g+rx /root

# This should be privileged access only
COPY test/functional/assets/agent_auth /root/.agent_auth
COPY test/functional/assets/agent_auth_wrong /root/.agent_auth_wrong
COPY test/functional/assets/bad_perms_agent_auth /root/.bad_perms_agent_auth
RUN chmod 400 /root/.agent_auth*

# Run tests
ENTRYPOINT ["sh", "/root/command-agent/entry-point.sh"]

这是非常冗长的,当然我每次进行全新部署时都不想重复。