0

enter image description here

I use docker build -t iot . to build a image my Dockerfile is :

FROM centos
USER root
ADD jdk1.8.0_101.tar.gz /root
COPY  run.sh /etc
RUN chmod 755 /etc/run.sh
CMD "/etc/run.sh"

my run.sh is:

  #!/bin/bash
    echo "aaaa"

I use docker run -itd iot to run a container,but I find my container can not be run. enter image description here

what should I do?

2 Answers 2

2

Your image builds and runs correctly. You just need to remove the d flag from run (for detached) or the docker command will exit immediately and run your container in the background. You can see that it actually exited with code zero according to the status column in docker ps -a.

You can corroborate this by running docker logs d63a (which is your container id). You should see aaaa.

0
0

Your description is inaccurate. When you docker run the container, it normally started, printed aaaa, and then exited.

So I guess what you would ask is "why my container cannot keep running, such as a daemon process". This is because you're executing a shell script which is actually a one-shot thing. Modify the CMD line in your Dockerfile to CMD "bash", your container will then not exit.

6
  • I changed my Dockerfile as CMD "bash" "/etc/run.sh" and it not work
    – mentongwu
    Commented Mar 26, 2018 at 2:47
  • Just CMD "bash", without the "/etc/run.sh" part.
    – Yuankun
    Commented Mar 26, 2018 at 2:49
  • I must run run.sh,this is an example,in fact I run my program in run.sh
    – mentongwu
    Commented Mar 26, 2018 at 2:54
  • OK, forget about my previous suggestion, and append one line to your run.sh: sleep infinity
    – Yuankun
    Commented Mar 26, 2018 at 2:58
  • Your means is that because the run.sh finishes running, the container stops. If run.sh keeps running, the container can run all the time?
    – mentongwu
    Commented Mar 26, 2018 at 3:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.