Run a service automatically in a docker container

你离开我真会死。 提交于 2019-11-27 17:18:16
Gigablah

To keep docker containers running, you need to keep a process active in the foreground.

So you could probably replace that last line in your Dockerfile with

CMD /bin/riak console

Or even

CMD /bin/riak start && tail -F /var/log/riak/erlang.log.1

Note that you can't have multiple lines of CMD statements, only the last one gets run.

sesm

Using tail to keep container alive is a hack. Also, note, that with -f option container will terminate when log rotation happens (this can be avoided by using -F instead).

A better solution is to use supervisor. Take a look at this tutorial about running Riak in a Docker container.

The explanation for:

If I run it using docker run -i -t quintenk/riak-dev /bin/bash the riak process is not started

is as follows. Using CMD in the Dockerfile is actually the same functionality as starting the container using docker run {image} {command}. As Gigablah remarked only the last CMD is used, so the one written in the Dockerfile is overwritten in this case.

By using CMD /bin/riak start && tail -f /var/log/riak/erlang.log.1 in the Buildfile, you can start the container as a background process using docker run -d {image}, which works like a charm.

"If I run it using docker run -i -t quintenk/riak-dev /bin/bash the riak process is not started"

It sounds like you only want to be able to monitor the log when you attach to the container. My use case is a little different in that I want commands started automatically, but I want to be able to attach to the container and be in a bash shell. I was able to solve both of our problems as follows:

In the image/container, add the commands you want automatically started to the end of the /etc/bash.bashrc file.

In your case just add the line /bin/riak start && tail -F /var/log/riak/erlang.log.1, or put /bin/riak start and tail -F /var/log/riak/erlang.log.1 on separate lines depending on the functionality desired.

Now commit your changes to your container, and run it again with: docker run -i -t quintenk/riak-dev /bin/bash. You'll find the commands you put in the bashrc are already running as you attach.

Because I want a clean way to have the process exit later I make the last command a call to the shell's read which causes that process to block until I later attach to it and hit enter.

arthur@macro:~/docker$ sudo docker run -d -t -i -v /raid:/raid -p 4040:4040 subsonic /bin/bash -c 'service subsonic start && read -p "waiting"'
WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: [8.8.8.8 8.8.4.4]
f27229a260c9

arthur@macro:~/docker$ sudo docker ps                                                                                                                                     
[sudo] password for arthur: 
ID                  IMAGE               COMMAND                CREATED              STATUS              PORTS
35f253bdf45a        subsonic:latest     /bin/bash -c service   2 days ago          Up 2 days           4040->4040

arthur@macro:~/docker$ sudo docker attach 35f253bdf45a

arthur@macro:~/docker$ sudo docker ps                                                                                                                                     
ID                  IMAGE               COMMAND             CREATED             STATUS              PORTS

as you can see the container exits after you attach to it and unblock the read. You can of course use a more sophisticated script than read -p if you need to do other clean up, such as stopping services and saving logs etc.

I use a simple trick whenever I start building a new docker container. To keep it alive, I use a ping in the entrypoint script.

So in the Dockerfile, when using debian, for instance, I make sure I can ping. This is btw, always nice, to check what is accessible from within the container.

...
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
 && apt-get install -y iputils-ping 
...
ENTRYPOINT ["entrypoint.sh"]

And in the entrypoint.sh file

#!/bin/bash
...
ping 10.10.0.1 >/dev/null 2>/dev/null

I use this instead of CMD bash, as I always wind up using a startup file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!