在此文件所在目录下执行docker-compose up -d启动容器,再执行docker ps -a查看容器状态,信息如下所示:
root@rabbitmq:/usr/local/work/test# docker-compose up -d
Creating test_master_1 ... done
root@rabbitmq:/usr/local/work/test# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb433fe9984d java:8u111-jdk "/bin/bash" About a minute ago Exited (0) About a minute ago test_master_1
信息显示我们启动的容器状态为Exited (0) About a minute ago,也就是说虽然创建了容器,但是该容器并未正常运行;
控制终端缺失
启动失败是因为缺失了控制终端的配置,这里有两种方式修复;
使用tty参数(推荐使用)
修改docker-compose.yml,增加一个配置tty:true,如下:
master:
image: java:8u111-jdk
tty: true
先执行docker-compose down将之前的容器删除,再执行docker-compose up -d启动,可以发现启动成功,并且可以成功进入容器进行操作:
root@rabbitmq:/usr/local/work/test# docker-compose up -d
Creating test_master_1 ... done
root@rabbitmq:/usr/local/work/test# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f51debaa26ec java:8u111-jdk "/bin/bash" 2 seconds ago Up 2 seconds test_master_1
root@rabbitmq:/usr/local/work/test# docker exec -it test_master_1 /bin/bash