Troubleshooting "Docker Not Adding User To MariaDB With Dockerfile"

docker mariadb

The Players:

  • Docker: A platform for running applications in isolated containers.
  • MariaDB: An open-source relational database management system.
  • Dockerfile: A text file with instructions to build a Docker image (blueprint for a container).

The Problem:

By default, the official MariaDB Docker image creates a user named "mysql" with a specific user ID (UID) and group ID (GID). However, sometimes you might want to use a different user in your Dockerfile for security reasons.

Here's where the issue arises: If you try to specify a new user in your Dockerfile, it might not override the default "mysql" user created by the MariaDB image.

Possible Reasons:

  • Conflicting Instructions: Your Dockerfile instructions to create a user might be placed after the MariaDB image's built-in user creation.
  • Missing Information: You might not be providing all the necessary details (UID and GID) when creating the user in your Dockerfile.


  • Override the Default User: There are ways to instruct the MariaDB image to use your specified user during the build process. This might involve environment variables or specific commands in your Dockerfile.
  • Use Existing User: If you don't necessarily need a new user, you can leverage the default "mysql" user created by the MariaDB image and set its password using environment variables in your Dockerfile.

Finding More Information:

For detailed instructions on overriding the default user or using environment variables, you can refer to resources like the official MariaDB Docker image documentation or community forums by searching for "MariaDB Docker custom user".

Using Environment Variables (Recommended):

This approach leverages the official MariaDB Docker image's built-in functionality to create a user.

# Use the official MariaDB image
FROM mariadb:latest

# Set environment variables for user creation (optional)
ENV MYSQL_USER my_custom_user
ENV MYSQL_PASSWORD my_secure_password

# Let the MariaDB image handle user creation
CMD ["mysqld"]


  • We use the mariadb:latest image as the base.
  • We define optional environment variables:
    • MYSQL_USER: This sets the username for the database user.
    • MYSQL_PASSWORD: This sets the password for the database user.
    • MYSQL_DATABASE: This sets the name of the database to create.
  • The CMD instruction simply runs the mysqld command, which starts the MariaDB server. The MariaDB image will automatically create the user and database based on the provided environment variables (if defined).

Explicit User Creation (Less Common):

This approach explicitly creates a user within your Dockerfile. Note that this might conflict with the MariaDB image's default user creation if not done carefully.

# Use the official MariaDB image
FROM mariadb:latest

# Define a specific user ID (UID) and group ID (GID) (optional)

# Create the user with the specified UID and GID
RUN groupadd -r mysql -g $GROUP_ID && useradd -r -u $USER_ID -g mysql mysql

# ... rest of your Dockerfile instructions ...

# Start the MariaDB server
CMD ["mysqld"]


  • We define optional arguments for user ID (USER_ID) and group ID (GROUP_ID).
  • The RUN instruction creates a group named mysql with the specified GID and then creates a user named mysql with the specified UID and assigns it to the mysql group.
  • The remaining part of your Dockerfile would likely involve setting up the database and other configurations.
  • Finally, the CMD instruction starts the MariaDB server.

Important Note:

The second approach requires careful handling to avoid conflicts with the MariaDB image's user creation. The first approach using environment variables is generally recommended for most cases.

Entrypoint Script:

This method involves creating an entrypoint script that executes after the image is built. The script can then handle user creation based on your specifications.

Here's a basic example:


FROM mariadb:latest

# Define optional environment variables
ENV MYSQL_USER my_custom_user
ENV MYSQL_PASSWORD my_secure_password

COPY entrypoint.sh /docker-entrypoint.sh

# Set the entrypoint to the script
ENTRYPOINT ["/docker-entrypoint.sh"]

CMD ["mysqld"]



# Check if environment variables are set
if [ -z "$MYSQL_USER" ] || [ -z "$MYSQL_PASSWORD" ] || [ -z "$MYSQL_DATABASE" ]; then
  echo "ERROR: Missing environment variables for user creation"
  exit 1

# Create the user using mysqladmin with provided details
mysqladmin -u root create "$MYSQL_USER"@"%" identified by "$MYSQL_PASSWORD"

# Create the database (optional)
mysqladmin -u root create "$MYSQL_DATABASE"

# Start the MariaDB server
CMD ["mysqld"]


  • We define environment variables similar to the previous example.
  • We copy a script named entrypoint.sh into the container at /docker-entrypoint.sh.
  • The ENTRYPOINT instruction sets the script to be executed when the container starts.
  • The entrypoint.sh script checks if the environment variables are set.
  • If the variables are set, it uses mysqladmin with the root user to create the desired user and database.
  • Finally, the script calls the mysqld command to start the MariaDB server.

Multi-Stage Builds:

This approach uses a multi-stage Docker build process. In the first stage, we create a temporary image to handle user creation. In the second stage, we copy only the necessary files from the first stage and start the MariaDB server.

Here's a simplified example:


# Stage 1: User Creation
FROM mariadb:latest

RUN groupadd -r mysql && useradd -r -u 1000 -g mysql mysql

# Stage 2: Final Image
FROM debian:slim

# Copy only necessary files from stage 1
COPY --from=0 /var/lib/mysql /var/lib/mysql

# Start the MariaDB server with user set to 'mysql' (created in stage 1)
CMD ["mysqld"]


  • The first stage (mariadb:latest) creates the user mysql with specific UID and GID.
  • The second stage uses a minimal image like debian:slim.
  • We copy only the required data directory (/var/lib/mysql) from the first stage, which contains the user information.
  • Finally, we start the MariaDB server.


  • The multi-stage build approach can be more complex to manage but offers a cleaner final image without unnecessary files from the MariaDB image.
  • Always ensure proper access control within the container for security reasons.

These are just a couple of alternative methods. The best approach depends on your specific needs and security considerations.

docker mariadb

Mastering Subqueries in jOOQ: The EXISTS Clause for Powerful SQL Queries

Understanding EXISTS in SQL:The EXISTS clause in SQL checks if a subquery returns at least one row.It's commonly used for semi-joins (filtering based on subquery results) or anti-joins (excluding rows based on subquery results)...

Working with Far-Future Dates in MariaDB: Alternatives to FROM_UNIXTIME

MariaDB's FROM_UNIXTIME FunctionIn MariaDB, FROM_UNIXTIME is a function that converts a Unix timestamp (number of seconds since the epoch...

MariaDB Version Mystery Solved: Uncovering the Details Behind 10.4.14-MariaDB

The text "10. 4.14-MariaDB" indicates the version of MariaDB (10. 4.14). There isn't any programming involved in recognizing that...

When to Use (and Not Use) System-Versioned Tables for Testing Empty Tables in MariaDB

System-Versioned Tables in MariaDBMariaDB offers a powerful feature called system-versioned tables. These tables go beyond storing just the current data; they maintain a history of all changes made to the table...