Docker define more than one default command in Dockerfile

320 Views Asked by At

If I have a Docker file that has at the end:

ENTRYPOINT /bin/bash

and run the container via docker run and type in the terminal

gulp

that gives me running gulp that I can easily terminate with Ctrl+C

but when I put gulp as default command to Dockerfile this way:

CMD ["/bin/bash", "-c", "gulp"]

or this:

ENTRYPOINT ["/bin/bash", "-c", "gulp"]

then when I run container via docker run the gulp is running but I can't terminate it via Ctrl+C hotkey.

The Dockerfile I used to build the image:

FROM node:8

RUN apt-get update \
    && apt-get upgrade -y \
    && apt-get install -y libltdl-dev

WORKDIR /home/workspace

RUN npm install gulp -g

#works but cant kill gulp with Ctrl+C
#CMD ["/bin/bash", "-c", "gulp"]

#works but cant kill gulp with Ctrl+C
#ENTRYPOINT ["/bin/bash", "-c", "gulp"]

# need to type command gulp in cli to run it
# but I'm able to terminate gulp with Ctrl+C
ENTRYPOINT /bin/bash

It makes sense to me I can't terminate the default command for the container that is defined in Dockerfile because there would be no other command that could run once I terminate the default.

How can I state in Dockerfile that I want to run /bin/bash as default and on top of that gulp so If I terminate gulp I'll be switched back to the bash command line prompt?

2

There are 2 best solutions below

2
On

Gulp is build tool you need to install using run command. This will commit the changes on top of your base image.
If you want to use it as a default command either using ENTRYPOINT or CMD in your dockerfile, then you can definitely not kill it with a ctrl+c since it is not a shell process, but in fact a container that you are running.
If in case you have your dockerfile an ENTRYPOINT. You can stop the container using docker stop.

NOTE: A container cannot be killed using ctrl+c, it needs to be stopped via:
docker stop container_name

1
On

Since gulp is a build tool, you'd generally run it in the course of building your container, not while you're starting it. Your Dockerfile might look roughly like

FROM node:8
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . ./
RUN gulp
CMD yarn run start

When you run docker build, along the way it will print out things like

---> b35f4035db3f
Step 6/7 : RUN gulp
---> Running in 02071fceb21b

The important thing is that the last hex string that gets printed out in each step (the line before each Dockerfile command) is a valid Docker image ID. If your build goes wrong, you can

host$ sudo docker run --rm -it b35f4035db3f bash
root@38ed4261ab0f:/app# gulp

Once you've finished debugging the issue, you can check whatever fixes into your source control system, and rebuild the image as needed later.