1

This is my Dockerfile:

FROM centos:7

MAINTAINER rlabs

LABEL Remarks="This is a dockerfile example for Centos system"

# Install Java
RUN yum update -y \
&& yum install java-1.8.0-openjdk -y \
&& yum clean all \
&& rm -rf /var/cache/yum \
&& yum -y install httpd httpd-tools

# Install Python
RUN yum install python3 -y \
&& pip3 install --upgrade pip setuptools wheel \
&& if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi \
&& if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi \
&& yum clean all \
&& rm -rf /var/cache/yum
# load generic RPMs
RUN pip3 install boto3 \
 && yum -y install sudo \
 && yum -y install git \
 && yum -y install gem \
 && yum -y install wget \
 && yum -y install gcc

RUN yum -y install unzip \
 && yum -y install net-tools \
 && yum -y install tcpdump

# add local files to image
COPY aws/aws_config_template /root/.aws/config
COPY aws/aws_credentials_template /root/.aws/credentials

RUN useradd myuser
RUN ln -s /root/.aws /home/myuser


COPY mydir /site/mydir
RUN mkdir /site/log

COPY systemctl.py /usr/bin/systemctl
RUN chmod a+x /usr/bin/systemctl

EXPOSE 80
EXPOSE 8000
EXPOSE 8001

RUN chown -R 1000:1000 /site
USER myuser
WORKDIR /site/mydir

When I add this in Dockerfile:

ENTRYPOINT["ping"]
CMD["google.com]

and then run command:

docker run -d -p 8001:8001 myimage

Docker container runs fine. I am able to ssh into my container and run one shell script which runs the jar file and expose the 8001 port.

But if I try to run same shell script by adding CMD like this in Dockerfile CMD["sh","/site/mydire/myfile.sh"] and remove ENTRYPOINT from the Dockerfile and then run command:

docker run -d -p 8001:8001 myimage

nothing happens. On doing docker ps, container spins up for a second or two and then gets deleted automatically.If i do docker ps again, nothing shows up. Am I missing something here?

AWS_Developer
  • 508
  • 11
  • 32
  • FYI, you are missing the closing `"` in `CMD["/site/mydire/myfile.sh]` – Phil Jan 05 '22 at 03:41
  • 1
    Does this answer your question? [Dockerfile CMD instruction will exit the container just after running it](https://stackoverflow.com/questions/42218957/dockerfile-cmd-instruction-will-exit-the-container-just-after-running-it) – Phil Jan 05 '22 at 03:44
  • Same issue: '''$ docker run -p 8001:8001 myimage server starting... server started $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES''' – AWS_Developer Jan 05 '22 at 03:45

1 Answers1

0

This has little to nothing to do with the ENTRYPOINT thing.

When you run the container with the following:

ENTRYPOINT["ping"]
CMD["google.com]

you're running the ping command with an argument: ping google.com.

If you run that in a terminal, it will run endlessly: it never stops.

Similarly, if you run that in a Docker container, the container keeps on running. Since you also have an ssh daemon running in the container, you can ssh into the container, while the container is still running the ping command.

When you replace the two lines by

CMD["sh","/site/mydire/myfile.sh"]

the Docker container will run that script, but once the script is finished, it will exit the container. That may take a millisecond, a second, or longer (apparently a few seconds in your case). The container is no longer kept active after that, because there is no reason: it has executed its (final) command, and will exit.

So, in that sense, Phil's comment is right on the mark: you need to keep the container alive, with the -it option to the run command, for an interactive session.

9769953
  • 10,344
  • 3
  • 26
  • 37
  • So, do I need to run some command which keeps running for long while running the docker container? I have a jar file which I need to run using shell script as soon as container spins up. Could you please suggest a way of doing this? – AWS_Developer Jan 05 '22 at 06:17
  • The suggested duplicate has several answers that tell you how you can keep the container running. – 9769953 Jan 05 '22 at 12:12
  • But why can't you do it all in the container? Why does it have to be interactive? – 9769953 Jan 05 '22 at 12:13
  • I don't want it to be interactive. All I am looking is to run y JAR file. It runs also but after doing some processing, it gets stopped and as soon as the processing of the JAR file ends, the container also dies. – AWS_Developer Jan 06 '22 at 07:47
  • So wrap it all up in a script, inside the container. No need to have the container run and log in yourself while it's running. Make it a all-in-one container that runs it all for you; that's the purpose of a container. – 9769953 Jan 06 '22 at 08:59
  • Sorry, I didn’t get this. Wrap it all in a script means? – AWS_Developer Jan 07 '22 at 07:03
  • You run `myfile.sh` and the JAR file both in the same script. – 9769953 Jan 07 '22 at 08:48