docker-compose命令在什么范围内运行

时间:2019-01-08 13:18:13

标签: docker docker-compose

我遇到了在docker compose文件中为我的容器运行启动脚本(例如npm run gulp-dist)的问题。我将问题归结为节点版本兼容性问题,这使我有些困惑。

如果我使用docker-compose run workspace bash输入容器,然后运行node -v,则会按预期(以及我的脚本要求)返回v10.5.0。 但是,如果在docker-compose中设置了command: node -v,则在用v4.2.6调出容器时会显示docker-compose up workspace

所以我想知道其中是我在docker-compose中指定的运行命令(我以为它们一旦启动就在容器中运行)。以及如何在容器中运行命令-我想在docker-compose中指定该命令,因为我在两个不同的docker-compose文件(一个用于dev env,一个用于生产)中运行另一个命令。

注意:我的开发机的节点版本为11,所以我不知道四个节点在哪里。

此外,如果运行docker-compose run workspace bash然后运行原始脚本,则可以正常工作-作为docker-compose命令运行时,它只是失败。

这是我的dockerfile(对不起,很大):

# FROM laradock/workspace:1.8-71
# copied the contents of the above laradock workspace 
# dockerfile and replaced put here directly.

FROM phusion/baseimage:latest

MAINTAINER Mahmoud Zalt <mahmoud@zalt.me>

RUN DEBIAN_FRONTEND=noninteractive
RUN locale-gen en_US.UTF-8

ENV LANGUAGE=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LC_CTYPE=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV TERM xterm

# Add the "PHP 7" ppa
RUN apt-get install -y software-properties-common && \
        add-apt-repository -y ppa:ondrej/php

#
#--------------------------------------------------------------------------
# Software's Installation
#--------------------------------------------------------------------------
#

# Install "PHP Extentions", "libraries", "Software's"
RUN apt-get update && \
        apt-get install -y --allow-downgrades --allow-remove-essential \
        --allow-change-held-packages \
        php7.1-cli \
        php7.1-common \
        php7.1-curl \
        php7.1-intl \
        php7.1-json \
        php7.1-xml \
        php7.1-mbstring \
        php7.1-mcrypt \
        php7.1-mysql \
        php7.1-pgsql \
        php7.1-sqlite \
        php7.1-sqlite3 \
        php7.1-zip \
        php7.1-bcmath \
        php7.1-memcached \
        php7.1-gd \
        php7.1-dev \
        pkg-config \
        libcurl4-openssl-dev \
        libedit-dev \
        libssl-dev \
        libxml2-dev \
        xz-utils \
        libsqlite3-dev \
        sqlite3 \
        git \
        curl \
        vim \
        nano \
        postgresql-client \
        && apt-get clean

#####################################
# Composer:
#####################################

# Install composer and add its bin to the PATH.
RUN curl -s http://getcomposer.org/installer | php && \
        echo "export PATH=${PATH}:/var/www/vendor/bin" >> ~/.bashrc && \
        mv composer.phar /usr/local/bin/composer

# Source the bash
RUN . ~/.bashrc

#
# other - workspace specific config
#

RUN apt-get -y update && \
        apt-get install pkg-config libmagickwand-dev -y && \
        pecl install imagick

#####################################
# Non-Root User:
#####################################

# Add a non-root user to prevent files being created with root permissions on host machine.

ENV PUID 1000
ENV PGID 1000

RUN groupadd -g ${PGID} laradock && \
        useradd -u ${PUID} -g laradock -m laradock && \
        apt-get update -yqq

#####################################
# Set Timezone
#####################################

ARG TZ=UTC
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

#####################################
# Composer:
#####################################

# Add the composer.json
COPY ./composer.json /home/laradock/.composer/composer.json

# Make sure that ~/.composer belongs to laradock
RUN chown -R laradock:laradock /home/laradock/.composer
USER laradock

# Check if global install need to be ran
ARG COMPOSER_GLOBAL_INSTALL=false
ENV COMPOSER_GLOBAL_INSTALL ${COMPOSER_GLOBAL_INSTALL}
RUN if [ ${COMPOSER_GLOBAL_INSTALL} = true ]; then \
        # run the install
        composer global install \
        ;fi

USER root

#####################################
# Node / NVM:
#####################################

# Check if NVM needs to be installed
ARG NODE_VERSION=10.5.0
ENV NODE_VERSION 10.5.0
ENV NVM_DIR /home/laradock/.nvm
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash && \
        . $NVM_DIR/nvm.sh && \
        nvm install ${NODE_VERSION} && \
        nvm use ${NODE_VERSION} && \
        npm install -g gulp bower vue-cli \
        ;fi

# link node and nodejs
RUN ln -s /usr/bin/nodejs /usr/bin/node


# Wouldn't execute when added to the RUN statement in the above block
# Source NVM when loading bash since ~/.profile isn't loaded on non-login shell
RUN echo "" >> ~/.bashrc && \
        echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
        echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> ~/.bashrc \
        ;fi

# install required things
RUN apt-get update && apt-get install apt-transport-https && \
        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
        echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
        apt-get update && apt-get install -y --allow-unauthenticated yarn mysql-client

# Add NVM binaries to root's .bashrc
USER root

RUN apt-get install npm -y

# set npm registry address
RUN npm config set registry http://registry.npmjs.org/


#
#--------------------------------------------------------------------------
# Final Touch
#--------------------------------------------------------------------------
#

# Clean up
USER root
RUN apt-get clean && \
        rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Set default work directory
WORKDIR /var/www

# # copy in our code, so as not to rely on a volume in prod
COPY . /var/www

# ensure directories we need are writable
RUN chmod -R o+w /var/www/user-api-laravel/storage
RUN chmod -R o+w /var/www/user-api-laravel/bootstrap/cache
RUN chmod -R o+w /var/www/auto/storage
RUN chmod -R o+w /var/www/auto/bootstrap/cache

# install php project dependencies

RUN cd /var/www/user-api-laravel && composer install
RUN cd /var/www/auto && composer install

WORKDIR /var/www

USER root

# install auto-scalar deps
RUN cd /var/www/auto-scaler && npm i

# php.ini for cli
ADD ./php-cli.ini /etc/php/7.1/cli/php.ini

以及docker-compose的相关部分:

workspace:
    build:
      context: ./www-workspace
      args:
        - TZ=${WORKSPACE_TIMEZONE}
        - NODE_VERSION=${WORKSPACE_NODE_VERSION}
    command: [bash, -c, "cd /var/www/spa && npm run dist-prod"]

1 个答案:

答案 0 :(得分:0)

仍然不知道命令在什么上下文中运行,但是使我的工作正常。这是由于通过NVM安装了节点。或者至少在我按照@Noogen的建议通过preg_*安装后,我可以对我的容器运行命令,并且它们可以访问正确的节点版本。我不得不选择一个较低的节点版本(不是我可以用NVM指定的10.5.0),但最终它起作用了,所以没有后顾之忧。