简体   繁体   中英

Command `source` doesn't work in Dockerfile

I want build my images, here is my Dockerfile:

FROM ubuntu:16.04

MAINTAINER 'hulei886@aliyun.com'

ENV PHANTOMJS_VERSION 2.1.1 
ENV PYTHON_VERSION 3.6.3

# install python3.6.3
RUN apt-get update \
 && apt-get install -y build-essential \
                       git \
                       curl \
                       libssl-dev \ 
                       zlib1g-dev \
                       libncurses5-dev \
                       libncursesw5-dev \
                       libreadline-dev \
                       libsqlite3-dev \

 && apt-get install -y libgdbm-dev \
                       libdb5.3-dev \
                       libbz2-dev \
                       libexpat1-dev \
                       liblzma-dev \ 
                       tk-dev \

 && cd ~ \

 && git clone https://github.com/pyenv/pyenv.git .pyenv \
 && echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc \
 && echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc \
 && echo 'eval "$(pyenv init -)"' >> ~/.bashrc \
 && source ~/.bashrc \

 && curl -L https://raw.githubusercontent.com/yyuu/pyenv- installer/master/bin/pyenv-installer | bash \

 && pyenv install PYTHON_VERSION \

 && pyenv global PYTHON_VERSION \
 && cd .. \
 CMD [python]

but when i run "docker build . -t [mytag]", git clone command didn't clone anything,here is screenshot: 在此处输入图片说明

I need help to fix this problem,thanks!

The error message is pretty clear:

/bin/sh: 1: source: not found

No problem with git clone but with source in a /bin/sh shell. You need to use the . (dot) command instead.

When writing a Dockerfile, it's better to test the commands in a container with /bin/sh shell.

In your case, after the source error, there are other errors:

  • space in the curl URL
  • env variable wrongly used: missing $ prefix
  • useless cd ~ : only WORKDIR instruction will change the current directory when running containers from the resulting image
  • CMD instruction wrongly inserted in the RUN instruction

A fixed version would be:

[...]
  && git clone https://github.com/pyenv/pyenv.git .pyenv \
  && echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc \
  && echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc \
  && echo 'eval "$(pyenv init -)"' >> ~/.bashrc \
  && . ~/.bashrc \

  && curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash \

  && pyenv install $PYTHON_VERSION \
  && pyenv global $PYTHON_VERSION

WORKDIR /root
CMD [python]

You have to use source alternative in bash. You can do something like

. somefile

instead of

source somefile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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