获取用户名和密码到docker集装箱

在过去的几天里,我一直在努力设置一些docker容器和shell脚本来为我的应用程序创build一个运行环境。

简而言之,我有一台需要数据库操作的networking服务器。 我的目标是让最终用户将这些内容解压到他们的docker机器上,运行一个构build脚本(它只是构build相关的docker镜像),然后运行一个OneTime.sh脚本(创build必要的卷和数据库)系统会提示用户input数据库超级用户的用户名和密码。

我遇到的问题是获取这些值泊坞窗的形象。 这是我的脚本:

# Create the volumes for the data backend database. docker volume create --name psql-data-etc docker volume create --name psql-data-log docker volume create --name psql-data-lib # Create data store database echo -e "\n${TITLE}[Data Store Database]${NC}" docker run -v psql-data-etc:/etc/postgresql -v psql-data-log:/var/log/postgresql -v psql-data-lib:/var/lib/postgresql -p 9001:5432 -P --name psql-data-onetime postgres-setup # Close containers docker stop psql-data-onetime docker rm psql-data-onetime docker stop psql-transactions-onetime docker rm psql-transactions-onetime 

这里是docker文件:

 FROM ubuntu #Required environment variables: USERNAME, PASSWORD, DBNAME # Add the PostgreSQL PGP key to verify their Debian packages. # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 # Add PostgreSQL's repository. It contains the most recent stable release # of PostgreSQL, ``9.3``. RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3 # There are some warnings (in red) that show up during the build. You can hide # them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 # Note: The official Debian and Ubuntu images automatically ``apt-get clean`` # after each ``apt-get`` # Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed`` USER postgres # Complete configuration USER root RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf # Expose the PostgreSQL port EXPOSE 5432 # Add VOLUMEs to allow backup of config, logs and databases RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] # Run setup script ADD Setup.sh / CMD ["sh", "Setup.sh"] 

脚本“Setup.sh”如下:

 echo -n " User name: " read user echo -n " Password: " read password echo -n " Database Name: " read dbname /etc/init.d/postgresql start /usr/lib/postgresql/9.3/bin/psql --command "CREATE USER $user WITH SUPERUSER PASSWORD '$password';" /usr/lib/postgresql/9.3/bin/createdb -O $user $dbname exit 

为什么这不工作? (我不会提示input文本,并且会引发参数错误的错误)。 什么是正确的方式来做这样的事情? 感觉这可能是一个相当普遍的问题,但我不能在我的生活中发现这种行为的任何非复杂的例子。

这样做的主要目的是为了让最终用户的生活更轻松,所以如果我只是提示他们input用户名,密码和dbname(加上调用正确的脚本),这将是理想的。

编辑:

运行日志文件后,看起来像这样:

  User name: Password: Database Name: Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ..] 

编辑2:

更新到CMD [“sh”,“-x”,“Setup.sh”]之后

我得到:

  • echo -n用户名:+读取用户:错误的variables名称用户
  • echo -n密码:+读取密码:错误的variables名称密码
  • echo -n数据库名称:+读取dbname:坏的variablesdbname