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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"]
答案

仍然不知道命令运行的上下文,但使我的工作。这是由于节点是通过NVM安装的。或者至少在我安装时,正如@Noogen建议的那样,通过curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -我可以对我的容器运行命令,他们可以访问正确的节点版本。我不得不选择较低的节点版本(不是10.5.0,因为我可以用NVM指定),但最终它的工作没有后顾之忧。

以上是关于docker-compose命令在什么范围内运行的主要内容,如果未能解决你的问题,请参考以下文章

如何在 docker-compose.yml 中重建 docker 容器?

使用 docker-compose 时如何在容器内使用主机用户修改卷文件

尝试在运行 docker-toolbox 的 Windows 上使用 docker-compose 在 docker 容器内运行 webpack-dev-server 时出现“来自服务器的空回复”

Postgresql容器未在docker-compose文件中运行-这是为什么?

如何在 docker-compose 中访问 Postgres 数据库

Docker异常docker-compose up throw UnicodeDecodeError: 'ascii' codec can't decode byte 0x(代