Answers: <p>In the official MariaDB image, <code>my.cnf</code> is configured to read additional configuration files from the <code>/etc/mysql/conf.d/</code> and <code>/etc/mysql/maria.conf.d/</code> directories:</p>
  
  <pre><code>root@bbd24e22be98:/# tail /etc/mysql/my.cnf 
  
  #
  # * IMPORTANT: Additional settings that can override those from this file!
  #   The files must end with '.cnf', otherwise they'll be ignored.
  #
  !include /etc/mysql/mariadb.cnf
  !includedir /etc/mysql/conf.d/
  
  root@bbd24e22be98:/# tail /etc/mysql/mariadb.cnf 
  # * Character sets
  # 
  # Default is Latin1, if you need UTF-8 set all this (also in client section)
  #
  #character_set_server   = utf8 
  #collation_server       = utf8_general_ci 
  # Import all .cnf files from configuration directory
  !includedir /etc/mysql/mariadb.conf.d/
  </code></pre>
  
  <p>Based on that, it is quite easy to setup utf8 everywhere by writing a configuration file to override the relevant settings, copying it at the right place from your Dockerfile:</p>
  
  <pre><code>$ cat db/utf8.cnf 
  [client]
  default-character-set=utf8
  
  [mysql]
  default-character-set=utf8
  
  [mysqld]
  init_connect='SET collation_connection = utf8_unicode_ci'
  init_connect='SET NAMES utf8'
  character-set-server=utf8
  collation-server=utf8_unicode_ci 
  skip-character-set-client-handshake
  </code></pre>
  
  
  
  <pre><code>$ cat db/Dockerfile 
  # Base image
  FROM mariadb:10.4
  
  # utf8 everywhere
  COPY utf8.cnf /etc/mysql/mariadb.conf.d/
  ENV MYSQL_RANDOM_ROOT_PASSWORD=yes
  </code></pre>
  
  <p>I let you compare the output of <code>show variables</code> after those changes with the one provided in the question:</p>
  
  <pre><code>MariaDB [(none)]&gt; show variables like 'char%'
      -&gt; ;
  +--------------------------+----------------------------+
  | Variable_name            | Value                      |
  +--------------------------+----------------------------+
  | character_set_client     | utf8                       |
  | character_set_connection | utf8                       |
  | character_set_database   | utf8                       |
  | character_set_filesystem | binary                     |
  | character_set_results    | utf8                       |
  | character_set_server     | utf8                       |
  | character_set_system     | utf8                       |
  | character_sets_dir       | /usr/share/mysql/charsets/ |
  +--------------------------+----------------------------+
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-29T23:06:50.810Z
Answers: 2019-01-29T23:06:50.810Z
Answers: Sylvain Leroux
Body: <p>I noticed when running a stock MariaDB Docker image in a container, the default character set is <code>latin1</code>:</p>
  
  <pre><code>CID=$(sudo docker run -d --rm -e MYSQL_ROOT_PASSWORD=p mariadb:10.3)
  echo "show variables like 'char%';" | sudo docker exec -i "$CID" mysql -t -u root -pp
  +--------------------------+----------------------------+
  | Variable_name            | Value                      |
  +--------------------------+----------------------------+
  | character_set_client     | latin1                     |
  | character_set_connection | latin1                     |
  | character_set_database   | latin1                     |
  | character_set_filesystem | binary                     |
  | character_set_results    | latin1                     |
  | character_set_server     | latin1                     |
  | character_set_system     | utf8                       |
  | character_sets_dir       | /usr/share/mysql/charsets/ |
  +--------------------------+----------------------------+
  </code></pre>
  
  <p>I <em>suspect</em>(correct me if I'm wrong) this may be related to the container's system locale :</p>
  
  <pre><code>echo "locale" | sudo docker exec -i "$CID" bash
  [sudo] password for sylvain:        
  LANG=
  LANGUAGE=
  LC_CTYPE="POSIX"
  LC_NUMERIC="POSIX"
  LC_TIME="POSIX"
  LC_COLLATE="POSIX"
  LC_MONETARY="POSIX"
  LC_MESSAGES="POSIX"
  LC_PAPER="POSIX"
  LC_NAME="POSIX"
  LC_ADDRESS="POSIX"
  LC_TELEPHONE="POSIX"
  LC_MEASUREMENT="POSIX"
  LC_IDENTIFICATION="POSIX"
  LC_ALL=
  </code></pre>
  
  <p>Is there a way to create a MariaDB image using utf8 everywhere without having to write and mount a custom <code>my.cnf</code> configuration file? Ideally, I'm looking for a Dockerfile-based solution.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-01-29T21:44:40.487Z
Comments: You can change character_set_server at runtime, so mostly you can do it with a mysql command in your docker file. That said, using a custom my.cnf is the way to go to tweak parameters according to your needs and the ressources you give the container. Usually templating the my.cnf and rendering from env variables is a good approach, there's probably others I don't know (hence the comment only)
Comments: Tensibai
Comments: CC BY-SA 4.0
Comments: 2019-01-29T22:26:02.593Z
Comments: _" mostly you can do it with a mysql command in your docker file"_ I investigated something like that. But the server isn't running when the image is built. I've done a couple more research since my initial post. Apparently, the daemon accepts some startup option I could use for that. I will post an answer if I manage to make it work.
Comments: Sylvain Leroux
Comments: CC BY-SA 4.0
Comments: 2019-01-29T22:27:12.963Z
Comments: _"Usually templating the my.cnf and rendering from env variables is a good approach"_ That sounds interesting. Could you elaborate a  little or point me to some relevant example?
Comments: Sylvain Leroux
Tags: <docker>
Title: How to create a MariaDB/MySQL image using utf8 instead of the default latin1 charset?
UserDisplayName: Sylvain Leroux
ThreadText: # How to create a MariaDB/MySQL image using utf8 instead of the default latin1 charset?
  
  (Asked by: username_0 on 2019-01-29)
  
  I noticed when running a stock MariaDB Docker image in a container, the default character set is `latin1`:
  
  ```
  CID=$(sudo docker run -d --rm -e MYSQL_ROOT_PASSWORD=p mariadb:10.3)
  echo "show variables like 'char%';" | sudo docker exec -i "$CID" mysql -t -u root -pp
  +--------------------------+----------------------------+
  | Variable_name            | Value                      |
  +--------------------------+----------------------------+
  | character_set_client     | latin1                     |
  | character_set_connection | latin1                     |
  | character_set_database   | latin1                     |
  | character_set_filesystem | binary                     |
  | character_set_results    | latin1                     |
  | character_set_server     | latin1                     |
  | character_set_system     | utf8                       |
  | character_sets_dir       | /usr/share/mysql/charsets/ |
  +--------------------------+----------------------------+
  
  ```
  
  I *suspect*(correct me if I'm wrong) this may be related to the container's system locale :
  
  ```
  echo "locale" | sudo docker exec -i "$CID" bash
  [sudo] password for username_0:        
  LANG=
  LANGUAGE=
  LC_CTYPE="POSIX"
  LC_NUMERIC="POSIX"
  LC_TIME="POSIX"
  LC_COLLATE="POSIX"
  LC_MONETARY="POSIX"
  LC_MESSAGES="POSIX"
  LC_PAPER="POSIX"
  LC_NAME="POSIX"
  LC_ADDRESS="POSIX"
  LC_TELEPHONE="POSIX"
  LC_MEASUREMENT="POSIX"
  LC_IDENTIFICATION="POSIX"
  LC_ALL=
  
  ```
  
  Is there a way to create a MariaDB image using utf8 everywhere without having to write and mount a custom `my.cnf` configuration file? Ideally, I'm looking for a Dockerfile-based solution.
  
  **Tags:** docker
  
  > **Comments:**
  > **username_1**: You can change character_set_server at runtime, so mostly you can do it with a mysql command in your docker file. That said, using a custom my.cnf is the way to go to tweak parameters according to your needs and the ressources you give the container. Usually templating the my.cnf and rendering from env variables is a good approach, there's probably others I don't know (hence the comment only)  
  > **username_0**: _" mostly you can do it with a mysql command in your docker file"_ I investigated something like that. But the server isn't running when the image is built. I've done a couple more research since my initial post. Apparently, the daemon accepts some startup option I could use for that. I will post an answer if I manage to make it work.  
  > **username_0**: _"Usually templating the my.cnf and rendering from env variables is a good approach"_ That sounds interesting. Could you elaborate a  little or point me to some relevant example?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-01-29. Score: 2 | Accepted answer
  
  In the official MariaDB image, `my.cnf` is configured to read additional configuration files from the `/etc/mysql/conf.d/` and `/etc/mysql/maria.conf.d/` directories:
  
  ```
  root@bbd24e22be98:/# tail /etc/mysql/my.cnf 
  
  #
  # * IMPORTANT: Additional settings that can override those from this file!
  #   The files must end with '.cnf', otherwise they'll be ignored.
  #
  !include /etc/mysql/mariadb.cnf
  !includedir /etc/mysql/conf.d/
  
  root@bbd24e22be98:/# tail /etc/mysql/mariadb.cnf 
  # * Character sets
  # 
  # Default is Latin1, if you need UTF-8 set all this (also in client section)
  #
  #character_set_server   = utf8 
  #collation_server       = utf8_general_ci 
  # Import all .cnf files from configuration directory
  !includedir /etc/mysql/mariadb.conf.d/
  
  ```
  
  Based on that, it is quite easy to setup utf8 everywhere by writing a configuration file to override the relevant settings, copying it at the right place from your Dockerfile:
  
  ```
  $ cat db/utf8.cnf 
  [client]
  default-character-set=utf8
  
  [mysql]
  default-character-set=utf8
  
  [mysqld]
  init_connect='SET collation_connection = utf8_unicode_ci'
  init_connect='SET NAMES utf8'
  character-set-server=utf8
  collation-server=utf8_unicode_ci 
  skip-character-set-client-handshake
  
  ```
  
  ```
  $ cat db/Dockerfile 
  # Base image
  FROM mariadb:10.4
  
  # utf8 everywhere
  COPY utf8.cnf /etc/mysql/mariadb.conf.d/
  ENV MYSQL_RANDOM_ROOT_PASSWORD=yes
  
  ```
  
  I let you compare the output of `show variables` after those changes with the one provided in the question:
  
  ```
  MariaDB [(none)]> show variables like 'char%'
      -> ;
  +--------------------------+----------------------------+
  | Variable_name            | Value                      |
  +--------------------------+----------------------------+
  | character_set_client     | utf8                       |
  | character_set_connection | utf8                       |
  | character_set_database   | utf8                       |
  | character_set_filesystem | binary                     |
  | character_set_results    | utf8                       |
  | character_set_server     | utf8                       |
  | character_set_system     | utf8                       |
  | character_sets_dir       | /usr/share/mysql/charsets/ |
  +--------------------------+----------------------------+
  
  ```
  
  
  ---
  
  