How to create a bash script file in Docker?

I wrote a script that the task is to create a text file and write into two lines.

This has been done, but more than once and I do not understand why because the script file does not contain any cycle and once the file is executed.

  • Docker: Cannot start container [8] System error: invalid argument
  • Using docker behind a proxy on an EC2 instance
  • Docker containers can't access local network DNS
  • Why is 'fig build' returning: AttributeError: 'WrappedSocket' object has no attribute 'recvfrom'?
  • Big size of python image in Docker
  • How do I register a new NuGet package source with dotnet CLI on Ubuntu 14.04?
  • #!/bin/bash
    
    <other, non-relevant variables and commands>
    
    PHP_ROOT_DIR=/etc/php-5.6.22
    PHP_CGI_WRAPPER=php-5622
    
    mkdir -p $(dirname $0)/cgi-bin
    touch $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
    echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
    echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER
    
    <other, non-relevant variables and commands>
    
    exit 0
    

    output file content:

    #!/bin/bash
    exec /etc/php-5.6.22/bin/php-cgi
    #!/bin/bash
    exec /etc/php-5.6.22/bin/php-cgi
    #!/bin/bash
    exec /etc/php-5.6.22/bin/php-cgi
    

    Update:

    This result occurs when run Docker container.

    If I reduce my script to only of above part (removed non-relevant code parts), then working correctly on host OS and Docker in a container equally.

    But the problem is still not fixed.

    full version script file is this:

    #!/bin/bash
    
    source $(dirname $0)/create_vhost_config.sh
    source $(dirname $0)/create_sample_html.sh
    
    IP=$(ip route get 1 | awk '{print $NF;exit}')
    
    SRC_DIR=/usr/local/src
    
    HTTPD_VERSION=2.4.20
    HTTPD_ROOT_DIR=/etc/apache2
    HTTPD_CONF_FILE=/etc/apache2/conf/apache2.conf
    
    PHP_VERSION=5.6.22
    PHP_ROOT_DIR=/etc/php-5.6.22
    PHP_CGI_WRAPPER=php-5622
    
    
    
    for i in 1 2
    do
        mkdir -p /var/www/vhosts/test$i/public
        mkdir -p /var/www/vhosts/test$i/log  
        create_html ${IP} "test$i" "/var/www/vhosts/test$i/public/index.html"
        create_php_info "/var/www/vhosts/test$i/public/index.php"
        create_simple_vhost "test$i" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
    done
    
    # add PHP handler to Apache configuration
    sed -i -e 's@    DirectoryIndex index.html@    DirectoryIndex index.php index.html@g' $HTTPD_CONF_FILE
    echo -e "\n<FilesMatch \"\.php$\">" >> $HTTPD_CONF_FILE
    echo -e "\tSetHandler application/x-httpd-php" >> $HTTPD_CONF_FILE
    echo -e "</FilesMatch>" >> $HTTPD_CONF_FILE
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    
    # *************************************** Creating PHP-CGI wrappers ***************************************
    mkdir -p /var/www/cgi-bin
    touch /var/www/cgi-bin/$PHP_CGI_WRAPPER
    echo -e "#!/bin/bash" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
    echo -e "exec ${PHP_ROOT_DIR}/bin/php-cgi" >> /var/www/cgi-bin/$PHP_CGI_WRAPPER
    
    #restart apache
    chmod -R +x /var/www/cgi-bin/
    
    # *************************************** Configure with mod_cgi/mod_cgid ***************************************
    sed -i -e 's@#LoadModule cgid_module modules/mod_cgid.so@LoadModule cgid_module modules/mod_cgid.so@g' $HTTPD_CONF_FILE
    sed -i -e 's@#LoadModule actions_module modules/mod_actions.so@LoadModule actions_module modules/mod_actions.so@g' $HTTPD_CONF_FILE
    echo -e "\nScriptAlias /php/ /var/www/cgi-bin/" >> $HTTPD_CONF_FILE
    echo -e "<Directory /var/www/cgi-bin/>" >> $HTTPD_CONF_FILE
    echo -e "\tRequire all granted" >> $HTTPD_CONF_FILE
    echo -e "</Directory>" >> $HTTPD_CONF_FILE
    
    rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
    
    for i in 1 2
    do
        create_mod_cgi_conf "test$i" "/php/php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
    done
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    
    
    # *************************************** Configure with mod_fcgid ***************************************
    cd $SRC_DIR
    wget http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/$(wget -O- http://xenia.sote.hu/ftp/mirrors/www.apache.org//httpd/mod_fcgid/ | egrep -o 'mod_fcgid-[0-9\.]+.tar.gz' | sort -V  | tail -1)
    tar -xf mod_fcgid-[0-9].[0-9].[0-9].tar.gz
    cd mod_fcgid-[0-9].[0-9].[0-9]
    APXS=/etc/apache2/bin/apxs ./configure.apxs
    make -j"$(nproc)"
    make install
    
    rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
    
    for i in 1 2
    do
        create_mod_fcgid_conf "test$i" "php-5622" "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
    done
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    
    
    # Configure with FPM
    find /var/www/vhosts/test*/public/ -exec chown daemon:daemon {} +
    sed -i -e 's@#LoadModule proxy_module modules/mod_proxy.so@LoadModule proxy_module modules/mod_proxy.so@g' $HTTPD_CONF_FILE
    sed -i -e 's@#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so@g' $HTTPD_CONF_FILE
    echo "include=etc/fpm.d/*.conf" >> $PHP_ROOT_DIR/etc/php-fpm.conf
    cp $SRC_DIR/php-$PHP_VERSION/sapi/fpm/init.d.php-fpm $PHP_ROOT_DIR/sbin/php-fpmd
    chmod 0700 $PHP_ROOT_DIR/sbin/php-fpmd
    find /var/www/vhosts/test*/public/ -exec chown www-data:www-data {} +
    
    rm -rf $PHP_ROOT_DIR/etc/fpm.d/*
    rm -rf $HTTPD_ROOT_DIR/conf/conf.d/*
    
    for i in 1 2
    do
        create_php_fpmd_conf "test$i.home" "$IP" 5000$i "$PHP_ROOT_DIR/etc/fpm.d/test$i.home.conf"
        create_fpm_conf "test$i" "$IP" 5000$i "$HTTPD_ROOT_DIR/conf/conf.d/test$i.home.conf"
    done
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    $PHP_ROOT_DIR/sbin/php-fpmd start
    
    
    
    # install Composer for PHP
    cd /tmp
    $PHP_ROOT_DIR/bin/php -r "readfile('https://getcomposer.org/installer');" | $PHP_ROOT_DIR/bin/php
    mv composer.phar /usr/local/bin/composer
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    $PHP_ROOT_DIR/sbin/php-fpmd restart
    
    
    #install PHP-PEAR
    cd $SRC_DIR && \
    wget http://pear.php.net/go-pear.phar -O go-pear.phar
    $PHP_ROOT_DIR/bin/php go-pear.phar
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    $PHP_ROOT_DIR/sbin/php-fpmd restart
    
    
    #install XDebug
    setterm -bold on
    cd $SRC_DIR
    wget https://xdebug.org/files/$(wget -O- https://xdebug.org/files/ | egrep -o 'xdebug-[0-9\.]+.tgz' | sort -V  | tail -1)
    tar -xvf xdebug-[0-9].[0-9].[0-9].tgz
    cd xdebug-[0-9].[0-9].[0-9]
    $PHP_ROOT_DIR/bin/phpize
    ./configure --enable-xdebug --with-php-config=$PHP_ROOT_DIR/bin/php-config
    make -j"$(nproc)"
    make install
    echo $(find / -type d -name no-debug-zts*)/xdebug.so >> $PHP_ROOT_DIR/etc/php.ini
    
    $HTTPD_ROOT_DIR/bin/apachectl restart
    $PHP_ROOT_DIR/sbin/php-fpmd restart
    
    exit 0
    

  • LogStash container restarts on Kube pod
  • How to run a CMD and open Bash when running a docker container?
  • How to use Docker for Dev env
  • How to start a docker container (ubuntu image)
  • NPM install in Vagrant shared folder leads to filesystem issues
  • openshift origin v0.3.3 error starting docker registry pod on centos 6.6
  • One Solution collect form web for “How to create a bash script file in Docker?”

    Change this:

    echo -e "#!/bin/bash" >> $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER

    to this:

    echo -e "#!/bin/bash" > $(dirname $0)/cgi-bin/$PHP_CGI_WRAPPER

    io-redirection

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.