dockerfle mysql install linking data dir

Viewed 43

I need to create docker file with ubuntu and mysql (not separate containers)

when I add

apt-get install -y mysql-server 

in dockerfile

installer create var/lib/mysql and put here data.

so when I configure it in docker-compose

volumes:
    - ./var/mysql:/var/lib/mysql

and in entrypoint try create database

/usr/bin/mysql -u root -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};"
    /usr/bin/mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '${MYSQL_ROOT_PASSWORD}';"

./var/mysql is empty direcory mysql dont start

so how to do it correctly ?

1 Answers

You want to combine Ubuntu and MySQL, in one Docker Container.

But the best way is to use Docker as it is meant to be, by splitting this kind of Applications into their own containers. Create a Ubuntu Container en create a MySQL container, and give Ubuntu access to MySQL.

See the example below:

docker-compose.yml

Create a docker-compose.yml in a folder.

version: '3.9'

services:

  ubuntu:
    build: .
    image: ubuntusql
    container_name: ubuntu
    command: ["sleep", "300d"]

  mysqlcontainer:
    image: mysql
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ./database:/var/lib/mysql
      - ./mysql_init:/docker-entrypoint-initdb.d
    ports:
      - "33060:33060"
      - "3306:3306"

Dockerfile

In the same folder, create a Dockerfile for creating your own Ubuntu image.

The reason for your own image is that you want to have mysql_client available in Ubuntu

FROM ubuntu

WORKDIR /root

RUN apt-get update && apt-get upgrade \
    && apt-get --assume-yes install mysql-client

mysql_init.sql

Create a subfolder called mysql_init and create a mysql_init.sql file there. (See the docker-compose.yml, it expects that file to be available)

CREATE DATABASE IF NOT EXISTS exampledb;

USE exampledb;

CREATE USER dbuser IDENTIFIED BY 'dbpassword';

GRANT ALL PRIVILEGES ON exampledb.* TO dbuser ;

CREATE TABLE contacts (
    name VARCHAR(255),
    address VARCHAR(255)
);

INSERT INTO contacts (name, address) VALUES ('John', 'Street');
INSERT INTO contacts (name, address) VALUES ('Mary', 'Another Street');
INSERT INTO contacts (name, address) VALUES ('Donald', 'No Street');

Database folder

Create a subfolder called 'database'. The docker-compose.yml expects it there and the MySQL Docker Container will create the database in that folder.

Start up Docker-Compose

docker compose up

Testing from Ubuntu

Enter the Ubuntu container:

docker exec -it ubuntu /bin/bash

In Ubuntu Container, start MySQL and check if you can access the MySQL database and data

$ docker exec -it ubuntu /bin/bash
root@10c8ef06a399:~# mysql -h mysql -u dbuser -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use exampledb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------+
| Tables_in_exampledb |
+---------------------+
| contacts            |
+---------------------+
1 row in set (0.00 sec)

mysql> select * from contacts;
+--------+----------------+
| name   | address        |
+--------+----------------+
| John   | Street         |
| Mary   | Another Street |
| Donald | No Street      |
+--------+----------------+
3 rows in set (0.01 sec)

mysql> exit
Bye
root@10c8ef06a399:~# exit
exit
$ 
Related