Docker撰写图像失败

时间:2015-08-01 19:45:06

标签: node.js redis docker docker-compose

我正在学习Dockerfiles和docker-compose。当我手动运行Dockerfiles并创建容器时,它们都可以正常工作,但是通过触发构建并使用docker-compose.yml脚本进行部署,订阅容器将停止工作。

成功运作的过程是:

docker build -t cu/broker:1.0.0 broker/
docker run -d --name broker -p 6379:6379 cu/broker:1.0.0
docker build -t cu/subscriber:1.0.0 subscriber/
docker run -d --name subscriber --link broker:db cu/subscriber:1.0.0
docker build -t cu/publisher:1.0.0 publisher/
docker run --name publisher --link broker:db -ti cu/publisher:1.0.0

运行这些命令后,我有两个正在运行的容器和一个交互式控制台,我可以将个别发布命令发送到redis服务器。

我没有将每个脚本与文件夹结构一起添加到此问题中,而是编写了一个shell脚本,可以重置所有内容并生成正确的结构和文件。

当我触发docker-compose.yml脚本时,它成功完成,但只有代理容器正在运行,其他两个脚本一启动就会终止。我不明白为什么,甚至使用--verbose标志我没有得到有用的信息来帮助调试这个。这是我用来运行脚本的命令。

docker-compose --verbose up -d

这是构建文件夹和文件结构的shell脚本config.sh。

#!/bin/sh
# builds the folder structures and dockerfiles

echo "STOPPING RUNNING CONTAINERS"
docker stop $(docker ps -a -q)
echo "DELETING ALL CONTAINERS"
docker rm $(docker ps -a -q)
echo "DELETING ALL IMAGES"
docker rmi $(docker images -q)
docker ps
docker images

if [ -d docker ]; then
    echo "DELETING EXISTING FILES AND DIRECTORIES"
    rm -rf docker
fi

echo "CREATING DIRECTORIES AND FILES"
mkdir docker
cd docker
echo -e "broker:" >> docker-compose.yml
echo -e "  build: broker/" >> docker-compose.yml
echo -e "  ports:" >> docker-compose.yml
echo -e "    - \"6379:6379\"\n" >> docker-compose.yml
echo -e "subscriber:" >> docker-compose.yml
echo -e "  build: subscriber/" >> docker-compose.yml
echo -e "  links:" >> docker-compose.yml
echo -e "    - \"broker:db\"\n" >> docker-compose.yml
echo -e "publisher:" >> docker-compose.yml
echo -e "  build: publisher/" >> docker-compose.yml
echo -e "  links:" >> docker-compose.yml
echo -e "    - \"broker:db\"\n" >> docker-compose.yml

mkdir broker
cd broker

echo "CREATING BROKER DOCKERFILE"
touch Dockerfile
echo -e "FROM redis:3.0.3" >> Dockerfile
echo -e  "RUN apt-get update -y && apt-get upgrade -y\n" >> Dockerfile
cd ..
mkdir publisher
cd publisher

echo "CREATING PUBLISHER DOCKERFILE"
touch Dockerfile
echo -e "FROM ubuntu:14.04" >> Dockerfile
echo -e  "RUN apt-get update -y && apt-get upgrade -y" >> Dockerfile
echo -e  "RUN apt-get install -y redis-server && service redis-server stop" >> Dockerfile
echo -e  "CMD redis-cli -h $DB_PORT_6379_TCP_ADDR\n" >> Dockerfile
cd ..
mkdir subscriber
cd subscriber

echo "CREATING SUBSCRIBER DOCKERFILE"
touch Dockerfile
echo -e "FROM node:0.12.7" >> Dockerfile
echo -e "RUN apt-get update -y && apt-get upgrade -y" >> Dockerfile
echo -e "RUN apt-get install -y apt-utils tree wget nano" >> Dockerfile
echo -e "WORKDIR /home" >> Dockerfile
echo -e "ADD index.js /home/index.js" >> Dockerfile
echo -e "RUN npm install ioredis" >> Dockerfile
echo -e "CMD ["node", "/home/index.js"]\n" >> Dockerfile

echo "CREATING JAVASCRIPT FILE"
echo -e "var redis = require('ioredis');" >> index.js
echo -e "var port = process.env.DB_PORT_6379_TCP_PORT;" >> index.js
echo -e "var ip = process.env.DB_PORT_6379_TCP_ADDR;\n" >> index.js
echo -e "client = redis.createClient(port, ip, {});\n" >> index.js
echo -e "console.log('REDIS PORT: '+port);" >> index.js
echo -e "console.log('REDIS IP:   '+ip);" >> index.js
echo -e "console.log('subscribed to "test" channel');\n" >> index.js
echo -e "client.subscribe('test');\n" >> index.js
echo -e "client.on('message', function(channel, message) {" >> index.js
echo -e "    console.log('MESSAGE RECEIVED');" >> index.js
echo -e "    console.log('CHANNEL: '+channel);" >> index.js
echo -e "    console.log('MESSAGE: '+message);" >> index.js
echo -e "});\n" >> index.js
cd ..

0 个答案:

没有答案