Docker错误:容器ID后跟“未找到命令”

时间:2015-12-10 17:41:11

标签: mysql wordpress bash docker

我正在编写一个脚本。该脚本在很大程度上是不完整的,但到目前为止,我希望它能够成功运行容器。当我执行脚本时,我收到了一个容器ID和“找不到命令”的错误。例如:./wordpress: line 73: 3c0fba4984f3b70f0eb3f1c15a7b157f4862b9b243657a3d2f7141029fb6641a: command not found

我正在使用的脚本如下:

#!/bin/bash

echo "Setting Constants"
MYSQL_ROOT_PASSWORD='password'
MYSQL_DATABASE='wordpress'
WORDPRESS_DB_PASSWORD='password'
WP_PORT='80'
DB_PORT='3306'

EPOCH=$(date +%s)    # append EPOCH to container names for uniqueness
#FILE='blogcontainers'  # filename containing container IDs

DB_CONTAINER_NAME="myblogdb$EPOCH"
WP_CONTAINER_NAME="myblog$EPOCH"

DB_IMG_NAME='blogdb'            # MySQL Docker image
WP_IMG_NAME='blog'          # WordPress Docker image

cd ~/myblog
WP_CID_FILE="$PWD/blog.cid"
DB_CID_FILE="$PWD/blogdb.cid"

if [ -f $DB_CID_FILE ]; then
    DB_IMG_ID=$(sed -n '1p' $DB_CID_FILE) 
else
        echo "dbcid not found"
    # set to baseline image
    DB_IMG_ID="f09a5b2903dc"        
fi

if [ -f $WP_CID_FILE ]; then
    WP_IMG_ID=$(sed -n '1p' $WP_CID_FILE)  
else
    echo "wpcid not found"
    # set to baseline image
    WP_IMG_ID="a8d48bc2313d"
fi

DB_PATH='/var/lib/mysql'         # standard MySQL path
WP_PATH='/var/www/html'          # standard WordPress path
LOCAL_DB_PATH="/$PWD$DB_PATH"
LOCAL_WP_PATH="/$PWD$WP_PATH"


echo "Starting MySQL Container"
#DB_ID=
$(docker run \
     -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD \
     -e MYSQL_DATABASE=$MYSQL_DATABASE \
     -v $LOCAL_WP_PATH:$DB_PATH \
     -v /$PWD/.bash_history:$WP_PATH \
     --name $DB_CONTAINER_NAME \
     -p $DB_PORT:3306 \
     --cidfile $DB_CID_FILE \
     -d \
     $DB_IMG_ID)



echo "Starting WordPress Container"
#WP_ID=
$(docker run \
     -e WORDPRESS_DB_PASSWORD=$WORDPRESS_DB_PASSWORD \
     --link $DB_CONTAINER_NAME:$DB_IMG_NAME \
     -p $WP_PORT:80 \
     -v $LOCAL_WP_PATH:$WP_PATH \
     -v /$PWD/.bash_history:/root/.bash_history \
     --name $WP_CONTAINER_NAME \
     --cidfile $WP_CID_FILE \
     -d \
     $WP_IMG_ID)

echo $WP_CONTAINER_NAME
echo $WP_IMG_ID

echo "reached end"

#echo $WP_ID > $FILE                 # copy WordPress container ID to file
#echo $DB_ID >> $FILE                # append MySQL container ID to file

执行代码后,通常会运行一个MySQL容器实例。例如:

    $ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4f2e9ab14c2e        f09a5b2903dc        "/entrypoint.sh mysql"   2 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   myblogdb1449768739

此外, blog.cid blogdb.cid 都已成功创建,包含容器ID。

$ cat blog.cid
e6005bcb4dba524b121d02b301fbe421d67d60986c55d554a0e20443df27ed18
$ cat blogdb.cid
4f2e9ab14c2ea5361557a3714477d7758c993af3b08bbc7db529282a41f90959

我一直在排除故障并寻找答案,但我认为是时候让另一组眼睛看一看。一如既往,欢迎任何投入/批评。

1 个答案:

答案 0 :(得分:2)

您使用的是$(docker run ...),而不仅仅是docker run ...。命令substitute($(...))运行命令,捕获输出,并扩展到该输出。因此,您尝试将docker run的输出作为命令运行。