V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
aaa5838769
V2EX  ›  Docker

关于 docker-compose 问题

  •  
  •   aaa5838769 · 2019-01-29 16:48:06 +08:00 · 3803 次点击
    这是一个创建于 2116 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 今天刚学习 docker-compose,我使用 docker-compose+Dockerfile。只创建了容器了,没有运行容器,直接退出了,不知道是什么原因。
    • 下面是配置文件和启动命令
    [root@localhost compose]# cat docker-compose.yml 
    version: '3'
    services:
      web:
        build: .
        ports:
          - "8888:80"
    [root@localhost compose]# cat Dockerfile 
    FROM centos:7
    MAINTAINER xy
    RUN yum install -y httpd php php-gd php-mysql;systemctl enable httpd.service
    RUN echo "<?php phpinfo()?>" > /var/www/html/index.php
    EXPOSE 80
    
    [root@localhost compose]# docker-compose up
     ---> d45344adbd48
    Step 6/6 : EXPOSE 80
     ---> Running in 8ba263e41682
    Removing intermediate container 8ba263e41682
     ---> b826f2af0a7c
    Successfully built b826f2af0a7c
    Successfully tagged compose_web:latest
    WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
    Creating compose_web_1 ... 
    Creating compose_web_1 ... done
    Attaching to compose_web_1
    compose_web_1 exited with code 0
    
    [root@localhost compose]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    
    [root@localhost compose]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
    69675d705d29        compose_web         "/bin/bash"              14 seconds ago      Exited (0) 14 seconds ago                       compose_web_1
    54794397b730        6d53ff0be550        "/bin/sh -c 'yum ins …"   2 hours ago         Exited (1) 2 hours ago    
    
    [root@localhost compose]# docker-compose ps
        Name         Command    State    Ports 
    ------------------------------------------
    compose_web_1   /bin/bash   Exit 0 
    
    9 条回复    2019-04-03 18:06:38 +08:00
    vissssa
        1
    vissssa  
       2019-01-29 17:50:11 +08:00
    docker-compose.yml 加个 command 就有显示了
    keepeye
        2
    keepeye  
       2019-01-29 18:06:33 +08:00
    看 dockerfile 你没运行服务啊,CMD 呢?
    keepeye
        3
    keepeye  
       2019-01-29 18:08:40 +08:00
    ```
    FROM ubuntu:14.04
    MAINTAINER xxx
    ADD bin/mtd /root/run
    CMD ["/root/run"]
    ```
    kaneg
        4
    kaneg  
       2019-01-29 19:59:15 +08:00 via iPhone
    docker 默认运行结束后就退出了,你需要一个不会结束的程序,比如 sleep infinity 或者 cat
    aaa5838769
        5
    aaa5838769  
    OP
       2019-01-30 09:38:41 +08:00
    @keepeye 我的 Dockerfile,RUN 加了这么一句话 systemctl enable httpd.service
    aaa5838769
        6
    aaa5838769  
    OP
       2019-01-30 09:39:30 +08:00
    @vissssa 好的,我一会试试,刚学习,很多知识点不知道
    aaa5838769
        7
    aaa5838769  
    OP
       2019-01-30 09:52:08 +08:00
    @vissssa docker-compose.yml command 可以执行这句话不 systemctl start httpd,需要加别的参数来配合不?
    keepeye
        8
    keepeye  
       2019-01-30 10:08:57 +08:00
    @aaa5838769 httpd 不要后台运行,docker 要求前台运行的程序,像 top,tail -f 等,否则 docker 命令跑完了直接就 exit 了
    BlackHole1
        9
    BlackHole1  
       2019-04-03 18:06:38 +08:00 via Android
    最后加个 CMD bash
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5913 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:36 · PVG 10:36 · LAX 18:36 · JFK 21:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.