Docker container not getting up

Viewed 101

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?

1 Answers

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.

Related