0

I am new in docker, I want to build an image with Ubuntu 20.04 and bind9 service installation.

below is my code of docker file

FROM ubuntu:20.04

ENV TZ=Asia
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update && apt-get install -y \
    apt-utils \
    systemctl \
    bind9     

CMD ["/usr/sbin/named", "-g", "-c", "/etc/bind/named.conf", "-u", "bind"]

CMD systemctl restart bind9

When I execute following command to build an image,

sudo docker image build --tag bind9server . 

It works fine.

Step 6/6 : CMD systemctl restart bind9
 ---> Running in f982f314c216

But when I run this docker image, I am getting an error like below

ERROR:systemctl:Unit bind9.service could not be found.

Can anyone help me, after installation of Bind9, why I am getting an error with above command?

Error comes with Docker only, if I run same command in Host environment which is Ubuntu 20.04 then it works fine.

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • Trying to run `systemd` inside Docker is a lot more complex than this. You will probably need to find a different way to approach your problem. – tripleee Oct 21 '21 at 14:34
  • The second `CMD` overrides the first; you can only have one anyway. – tripleee Oct 21 '21 at 14:34
  • To a first approximation `systemctl` (and similar commands like `service` or `/etc/init.d` scripts) just don't work in Docker; but if you delete the second `CMD`, does the first one start `named` in the foreground as the single main container process? – David Maze Oct 21 '21 at 14:41

1 Answers1

1

First thing: you should use a long running command for your last CMD, otherwise the container will exit once done. The purpose of CMD is to provide a default entry-point to your container, so even if there was a bind9 service, you container would exit immediately. Also, only the last CMD takes effect (see docs).

Second: see this question for an explanation of why it's not such a good idea to run with systemd inside a Docker container. You are much better off with your first try, that is calling named directly.

Third: on a "normal" host, when you are unsure about an unit name such as bind9, you can try to inspect all the known units with systemctl list-units --all. It looks like your service is called named.

Ostap
  • 300
  • 1
  • 6
  • Hi @Ostap, first of all thank you so much for your answer, for long running, yes, I have installed tcpdump as well and it is listening so it works continuously... – Siddhpura Amit Oct 22 '21 at 11:33
  • Glad to be helpful :-) Please consider the overhead of running tcpdump if this will be a production service. Have a nice week and please consider [accepting the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) if it solves your query. – Ostap Oct 25 '21 at 10:26
  • I have upvote your answer, for your solution but I want to setup this as per requirement, so wanted to know if there is anyway – Siddhpura Amit Oct 26 '21 at 05:19