Docker Community Forums

Share and learn in the Docker community.

Systemd not working inside the docker container customise base image

Hello,

I have built a docker image. There I am using the yocto built rootfs. The systemd is already integrated with that rootfs. but when I login into the container using "docker exec -it mycontainer_name sh " and I am trying to start an application through systemctl command i.e. “systemctl start app_name.service”. But I am getting below error:

System has not been booted with systemd as init system (PID 1). Can’t operate.

My Docker file looks like this:

FROM scratch  
ADD My_dev_env.rootfs.tar.bz2 /

#copy the executables required inside the docker image
COPY some_test.sh /usr/bin/  

#set the work directory for image
WORKDIR /usr/bin

RUN chmod 777 /usr/bin/some_test.sh 

#Tell image what to do on runnig as a container
 CMD ["./some_test.sh "]

And I am running this docker container as systemd service on my host my docker_service file is as below:

[Unit]
Description=run_rungdptest Container
After=docker.service
Requires=docker.service

[Service]
Type=simple
User=root
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker rm docker_container_name
ExecStart=/usr/bin/docker run \
	--name docker_container_name \
    docker_image_name
ExecStop=/usr/bin/docker stop docker_container_name
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

But, even the systemd is already integrated in the rootfs that I am using here. Eventhen systemd is not working inside the docker container. Can anybody suggest me on this, how can I make systemd working please. Any suggestion will help me a lot.

Regards,
Siddhartha V

There are lots of reasons to run systemd inside a system, such as:

Multiservice containers—Lots of people want to take existing multi-service applications out of VMs and run them inside of containers. We would prefer that they break apart these applications into microservices, but some people can’t or don’t have time yet. So running them as services launched out of unit files by systemd makes sense.
Systemd unit files—Most applications that run inside of containers are built from code that was run in VMs or on host systems. These applications have a unit file that was written for the application and understands how to run the application. It can be better to launch the service via the supported method, rather than to hack up your own init service.
Systemd is a process manager—It handles the management of services like reaping, restarting, and shutting down better than any other tool.
That being said, there are also lots of reasons not to run systemd in containers. The main one is that systemd/journald controls the output of containers, whereas tools like Kubernetes and OpenShift expect the containers to log directly to stdout and stderr. So, if you are going to manage your containers via Orchestrator like these, then you should think twice about using systemd-based containers. Additionally, the upstream community of Docker and Moby were often hostile to the use of systemd in a container.

1 Like