Docker start service

How can I automaticly start a service within an existing docker container

The point of Docker containers is that you have an isolated environment for usually one process and its children. Docker containers are not for traditional services running in the background. You run the container in detached mode (in the background) and inside the container you run the process in the foreground and it will keep the container alive.

Packages installed from package repositories like APT are usually not compatible with this concept, so you need to run those processes yourself. You can use for example Supervisord., but if you really want to run containers and still use Systemd to start your services, you can use LXD on Linux instead of Docker or use virtual machines.

Yes, it is possible to run systemd in a Docker container, but it does not make sense for me, except for testing.

1 Like

If you knew my use of docker you would understand why it’s my only option

I am using a raspberry pi 4 and I am running Pi-hole on it, docker is running dnsmasq for a PXE boot server but after every reboot, I made it automatically start the container but I still have to type “service dnsmasq start” after every boot up

Virtual machines do not work well on raspberry pi

Edit: I thought I probably mention that Pi-hole uses it’s own dnsmasq so if I sent the port to 0 it dosnt work. So docker gives it a separate up address

I wrote about multiple solutions. Virtual machines was one of them only for running systemd but you don’t need systemd, only dnsmasq and you can run it without the service command. The service command just executes a predefined command to run it in the background which you usually don’t want unless you have a process that keeps the container alive and in addition to that you want an other process running in the background. I guess this is what you want, but it was not completely clear after reading your last post either. Try to be very specific so our answers can be that too.

For example I don’t know just guess that you want to run dnsmasq in a container that also runs Pi-hole. There is a Docker image for that: pihole/pihole

Maybe you are already using it, you just didn’t mention it. That image uses an init system similar to Supervisord. It is s6-init which has its own configuration but the concept is the same. You run one init process that can start other processes and handles signals like “TERM” to properly stop every process.

Do you use this pihole image I linked above? It is an important detail to know before we can help.

If you use this image, check its description on Docker Hub if you have not done it yet.

1 Like

and that is impossible because you can not have port=0
my pihole is not the problem, the PXE server is
and I said I can’t use virtual machines because raspberry pi 4

and systemd dosn’t work in docker because the system has not booted with systemd

I still don’t understand this part so I hope someone will come to help you

I may have misunderstood that but you miss the point. You need an init process/process manager that handles multiple services in one container if you want to start multiple process in one container. That process manager must be the first process which start everything else. Now you need to choose one. During our discussion I wrote 3 of them. The fact that the “system has not booted with systemd” is irrelevant here. First of all this is the only one that I do not even recommend to use and you probably mean “systemctl” does not work, not “systemd”. Systemd could work, the recent versions also supporrt the environment variable “container” with the value “docker” it is a Docker container but it needs to be run as the first process inside the container. But I emphesize again: this is the only process manager I don’t recommend s lets’s forget it.

So you have two other optons that you can choose from this topic and you may find other process managers too. S6 and Supervisor are the two that I know.

I know. This is why I started my previous post with

and only for the case that is not your case, using Systemd. So let’s forget about virtual machines too, since I understood you immediately after your first post about that.

I hope I could clarify the misunderstandings. Again, the point is that you need S6 or Supervisor or an equivalent process manager.

I must admit, so far the situation is still ambigous.

Responses can be as good as the questions/provided details, idealy in the first post!

I am still unclear on:

  • is pihole running bare metal or inside a container?
    • if pihole is run inside a container, which image is used?
    • if pihole is run inside a container, is dnsmasq provided by the image used or a different dnsmasq running in a different container?
    • If pihole is run inside a container, does it use --network=host to use the network namespace of the host and therefor act network-wise as every native process on the host?
  • what is “port=0” supposed to be? All ports? Multicast?
  • also please share the exact command (or compose file) used to create the container(s), as it will give us details you might miss to mention otherwise

Please provide all relevant details to enable us to fully understand your situation and get an idea of what should be solved (the big picture and not the problem of the minute please!).

1 Like

Port=0 is for the PXE server inside the container
Pi-hole is running bare metal as it need the port to be 53

You said use systems to start the service

Judged by the details shared so far, the only thing I can recommend is to check wether --network=host is a valid option for your dnsmasq container.

Appart from that there is not much to work with. I hope you do find a solution for what you try to do and share the result in this thread for the rest of the world to benefit from.

1 Like

and whats that going to do, the container has internet
thats not going to start the service

Can somebody please help me

Does nobody know how to automatically start a service in a socket container

Flagging replies as off-topic is not going to get you any help. :-1:

Well it was off topic
The answer was unrelated to the question

–network=host is clearly to do with network not starting a service

and I am not happy with how this is going
every time I need help, I don’t get it
thats why I stopped using docker, but now I need it as its free and more PCs or Raspberry Pi’s are not free

And I do not appreciate that from a moderator, a mod should help not say I am wrong for marking it as off topic when it is off topic

every time I need help, I don’t get it

Please understand we’re all just volunteers trying to help, and you are not helping us to help you. You’ve got two of the most active users being very patient to get details from you, in this topic and some of the earlier ones, to understand what you’re trying to achieve. And you’re hardly answering their questions, but instead being hostile towards them and feel you’re not getting any help? :exploding_head:

Aside, as for expecting help, and assuming that at some point you found the solution for your earlier topics: it would be great to share that for future readers as well. So, please add resolutions to your old topics.

And to increase the chances to get help here again, please go over the requests for details above that you did not respond to yet. Or get a paid subscription and see if Docker Inc. is willing to spend the crazy amount of time that @rimelek and @meyay did.

Any new flag or hostile response from you will get your account banned.

If I re-create the container only running one process and replace bash with the process name will it work
Because I will need to install the process, so the first time I start the container the process won’t exist

No one is hostile towards you. Your communication style just does not allign with our, The level of details you think are relevant do not satisfy the the details we need to get a big picture and think things thru. It is up to you if you provide enough details and enable someone in actualy understanding your situation. You choose to not provide these details and I choose to stop responding because of that.

The port=0 issue is what --network=host was aimed for. Without this beeing sorted out it does not matter if the “start service” topic is solved. Do we agree on this?

Most people run pihole with --network=host AND use the intergrated dnsmasq to provide PXE. What’s wrong with that solution. Not using that solution is what makes you feel that you need to start a service in docker in the first place.

Personaly, I am not a fan to assist someone with the “problem of the minute” because someone tries to force patterns of the non-container world to containers.

So if the objective is to run pihole+dnsmasq to use PXE, then run the pihole image with --network=host . If your objective still is to run a service in a container then I would suggest to take a look at how pihole leverages s6-overlay to run their stuff as services.

Good luck!

1 Like

The port=0 issue is what --network=host was aimed for.

That was never the issue you asked why I can’t run dnsmasq and pihole

Most people run pihole with --network=host

And again what did I say, pihole is running bare metal

use the intergrated dnsmasq to provide PXE

This can not be done as pihole uses port 53 and TFTP needs dns disabled

If I re-create the container only running one process and replace bash with the process name will it work
Because I will need to install the process, so the first time I start the container the process won’t exist

Sorry it does turn out that I can remove port=0 with no issues
so everything can run bare metal