others-how to solve 'mysql docker container can not start'

1. Purpose

In this post, I would demo how to start mysql docker container, and how to solve the below error when start mysql docker container, At last I would demo how to choose an alternative for mysql docker.

[Entrypoint] MySQL Docker Image 8.0.23-1.1.19
[Entrypoint] Initializing database
2021-03-31T08:49:29.393676Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.23) initializing of server in progress as process 23
2021-03-31T08:49:29.406961Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-03-31T08:49:30.401119Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-03-31T08:49:32.479869Z 6 [Warning] [MY-010453] [Server] [email protected] is created with an empty password ! Please consider switching off the --initialize-insecure option.
[Entrypoint] Database initialized
/entrypoint.sh: line 205:    66 Killed                  "[email protected]" --daemonize --skip-networking --socket="$SOCKET"
[Entrypoint] MySQL Docker Image 8.0.23-1.1.19
[Entrypoint] Starting MySQL 8.0.23-1.1.19
/entrypoint.sh: line 207:    25 Killed                  env MYSQLD_PARENT_PID=$$ "[email protected]"

2. Environment

  • Docker Version: 20.10.2

3. The way we start mysql docker container

3.1 Verify that you have docker service running

$ docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
  

3.2 Create docker volume for mysql container

A docker volume is a persistent data stored in /var/lib/docker/volumes/… You can either declare it in a Dockerfile, which means each time a container is started from the image, the volume is created (empty), even if you don’t have any -v option. … It is used to quickly allow other containers to mount said volume.

$ docker volume create mysql-vol

You can manage volumes using Docker CLI commands or the Docker API. Volumes work on both Linux and Windows containers. Volumes can be more safely shared among multiple containers. Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.

Let’s verify the volume is created successfully:

$ docker volume inspect mysql-vol
[
    {
        "CreatedAt": "2021-03-31T16:59:13+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/mysql-vol/_data",
        "Name": "mysql-vol",
        "Options": {},
        "Scope": "local"
    }
]

3.3 Start mysql container

We can now start the mysql container as follows:

$ docker run --name mysql033104 \
    --restart=always \
    -v mysql-vol:/var/lib/mysql \
    -p 3306:3306 \
    -e MYSQL_ROOT_PASSWORD=my_root_password \
    -e MYSQL_DATABASE=testdb \
    -e MYSQL_USER=testuser \
    -e MYSQL_PASSWORD=123456 \
    -d mysql/mysql-server:latest

You can change the following variables:

  • MYSQL_ROOT_PASSWORD root user password
  • MYSQL_DATABASE The name of the database created after the first startup
  • MYSQL_USER · MYSQL_PASSWORD username and password

You can get more information from this site.

When you execute the above commands, it would cost you about ten minutes to download the docker image if you did not install the image before.

After downloading , you can verify the mysql docker image is started by running the below command:

$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS                            PORTS                     NAMES
3d13716f4b33   mysql/mysql-server:latest   "/entrypoint.sh mysq…"   10 minutes ago   Restarting (137) 23 seconds ago                             mysql033104

4. Debug the mysql container problem

We can debug the issue as follows:

$ docker logs 3d13716f4b33

Remember that the id ‘3d13716f4b33’ is the container ID of mysql, you can view it by executing ‘docker ps’ command.

We get this:

2021-03-31T08:49:32.479869Z 6 [Warning] [MY-010453] [Server] [email protected] is created with an empty password ! Please consider switching off the --initialize-insecure option.
[Entrypoint] Database initialized
/entrypoint.sh: line 205:    66 Killed                  "[email protected]" --daemonize --skip-networking --socket="$SOCKET"
[Entrypoint] MySQL Docker Image 8.0.23-1.1.19
[Entrypoint] Starting MySQL 8.0.23-1.1.19
/entrypoint.sh: line 207:    25 Killed                  env MYSQLD_PARENT_PID=$$ "[email protected]"

5. The solution

5.1 Solution of mysql

After googling a lot, I found the cause of my problem, My server is a low-end vps, it does not have enough memory, here is the memory info:

$ free -m
              total        used        free      shared  buff/cache   available
Mem:            441         206          63           2         171         222

The default configuration is designed to permit a MySQL server to start on a virtual machine that has approximately 512MB of RAM. So , we should increase our server’s memory.

So we can change the database to a more lightweight relational database like h2db

5.2 Alternative to mysql: h2db

We can start h2db docker instance as follows:

First: download the h2db docker image

$ docker pull oscarfonts/h2

Then start the container:

$ docker run -d -p 1521:1521 -p 81:81 -v /hostpath/h2db/data:/opt/h2-data --name=MyH2Instance oscarfonts/h2

At last, verify the instance is running:

$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                                        NAMES
7a34f386fb9e   oscarfonts/h2   "/bin/sh -c 'java -c…"   6 seconds ago   Up 3 seconds   0.0.0.0:81->81/tcp, 0.0.0.0:1521->1521/tcp   MyH2Instance

And view the logs of the h2db docker instance:

$ docker logs 7a34f386fb9e
TCP server running at tcp://172.17.0.4:1521 (others can connect)
Web Console server running at http://172.17.0.4:81 (others can connect)

Now it works!

5. Summary

In this post, I demonstrated how to start mysql docker container, and showed how to solve the killed issue when starting mysql container, you should allocate enough memory for mysql instance. You can also resort to the h2db database , which is a more lightweight relational database that can be used as test purpose. Thanks for your reading.

-->