1

I created an image from a Dockerfile:

FROM debian:latest

RUN apt-get update && apt-get install -y \
mosquitto \
&& rm -rf /var/lib/apt/lists/*

# mosquitto setup
EXPOSE 1883
COPY config/broker/conf.d/ /etc/mosquitto/conf.d/
CMD mosquitto

This works, but I'd like to start mosquitto as a service (is it recommended at all?). But when I replace the last line with

CMD service mosquitto start

the service doesn't start & I the container isn't running (but I don't get an error message during image creation)

Munchkin
  • 4,528
  • 7
  • 45
  • 93

2 Answers2

1

Using a container as a way to start a host service seems to me to be missing the point of containers. Apps that are "containerized" should be such that they can be started/stopped at anytime. If you are looking to have a long-running container that provides a service that you want to always be running, then add the --restart=always option on to your docker run command. That way when the host starts/restarts, and the Docker service is started, your container will auto-start as well. A good list of do's & don'ts about containerized apps is The 12 Factor App

JD Allen
  • 799
  • 5
  • 12
0

the service doesn't start & I the container isn't running

Check docker ps -a to see if a container has just exited: for your container to run, you need to make sure its main process stays in foreground and does not exit immediately.

Try the docker image toke/docker-mosquitto to see if containers run from it persist.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That's what I was trying to say: after `docker start` and `docker ps -a` my container states `exited`. I'd like to use my own Dockerfile, because I do sth. more. The Dockerfile above is just a minimal snippet for reproducing. – Munchkin Jul 05 '16 at 06:50
  • @Munchkin That means your main command starts and exits immediately. Try to configure your image the way https://github.com/toke/docker-mosquitto does. – VonC Jul 05 '16 at 06:51
  • I just had a look into that repo. Does `CMD ["/usr/sbin/mosquitto", "-c", "/mqtt/config/mosquitto.conf"]` start mosquitto as a service, like `service mosquitto start` would do? – Munchkin Jul 05 '16 at 07:24
  • 1
    @Munchkin No, `CMD` is parameters to `ENTRYPOINT` (see http://stackoverflow.com/a/35453524/6309). And `ENTRYPOINT` is calling https://github.com/toke/docker-mosquitto/blob/master/docker-entrypoint.sh which is a simple wrapper around exec. Try it out and see if that container persists, or exit immediately. – VonC Jul 05 '16 at 07:26
  • Just tried it out: I get an error at `docker run`: _docker-entrypoint.sh not found or does not exist_. In addition, I don't want to mount a volume. Now I ask myself, if it necessary to start mosquitto as a service inside the container, because with `CMD mosquitto`, mosquitto will start on every `docker start`... – Munchkin Jul 05 '16 at 08:52
  • @Munchkin `docker-entrypoint.sh not found` means you did not copy it in your Dockerfile, as https://github.com/toke/docker-mosquitto/blob/d5b062823b4fe30cab1c01241aa0dd219208e908/Dockerfile#L20 does. – VonC Jul 05 '16 at 09:17