Docker-compose up不会启动容器

时间:2015-12-27 15:46:15

标签: docker docker-compose

Dockerfile:

FROM shawnzhu/ruby-nodejs:0.12.7

RUN \
    apt-get install git \   
    && npm install -g bower gulp grunt \
    gem install sass

RUN useradd -ms /bin/bash devel

# Deal with ssh
COPY ssh_keys/id_rsa /devel/.ssh/id_rsa
COPY ssh_keys/id_rsa.pub /devel/.ssh/id_rsa.pub
RUN echo "IdentityFile /devel/.ssh/id_rsa" > /devel/.ssh/config

# set root password
RUN echo 'root:password' | chpasswd

# Add gitconfig
COPY .gitconfig /devel/.gitconfig

USER devel

WORKDIR /var/www/

EXPOSE 80

docker-compose.yml文件:

nodejs:
  build: .
  ports:
    - "8001:80"
    - "3000:3000"
  volumes:
    - ~/Web/docker/nodejs/www:/var/www

命令:

$ docker-compose build nodejs

$ docker images

REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nodejs_nodejs          latest              aece5fb27134        2 minutes ago       596.5 MB
shawnzhu/ruby-nodejs   0.12.7              bbd5b568b88f        5 months ago        547.5 MB

$ docker-compose up -d nodejs

Creating nodejs_nodejs_1

$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

$ docker ps --all

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
c24c6d0e756b        nodejs_nodejs       "/bin/bash"         About a minute ago   Exited (0) About a minute ago                       nodejs_nodejs_1

正如您所看到的,docker-compose up -d应该创建了一个容器并在后台运行它,但事实并非如此。相反,它退出了代码0。

2 个答案:

答案 0 :(得分:13)

如果您的Dockerfile没有做任何事情(例如,要在端口80上侦听的Web服务器),它将在完成指令运行后立即被丢弃。因为Docker containers should be "ephemeral"

如果您只想启动容器并通过终端与其进行互动,请不要使用docker-compose up -d,请改用以下内容:

docker run -it --entrypoint=/bin/bash [your_image_id]

这将启动您的容器并运行/bin/bash-it可帮助您保持终端会话与容器交互。完成工作后,按Ctrl-D退出。

答案 1 :(得分:1)

我有一个类似的问题,SQL Server 2017容器在创建后不久就退出了。在容器内运行的进程应该长时间运行,否则Docker将退出容器。在docker-compose场景中,我实现了tty:true方法,这里记录了https://www.handsonarchitect.com/2018/01/docker-compose-tip-how-to-avoid-sql.html