Can't run container when building image from dockerfile

Hello everyone!

I want to build mysql container which can use systemctl command then I build dockerfile with ubuntu and then install mysql.
But when I run container, it’s always exited

this is my Dockerfile

FROM ubuntu:18.04

RUN apt update && apt install vim rsyslog net-tools mariadb-server -y

COPY start.sh /start.sh
CMD /start.sh

This is my file start.sh

systemctl restart mariadb

You have to run MariaDB in the foreground, otherwise the container will exit. Take a look at how it is made in the official MariaDB Dockerfile.

1 Like

The problem is that, docker will exit a container when the entrypoint/cmd command is done running.
and since “systemctl restart mariadb” will exit after it starts mariadb, it will exit the container.

So you need something that will never quit, and as @tekki mentioned, running mariadb in foreground will solve this

1 Like

Thank you so much for this answer!