Run MySQL using Docker Compose

To run MySQL using Docker Compose

Run MySQL using Docker Compose

To run MySQL using Docker Compose, you need to create a docker-compose.yml file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '3.1'
services:
db:
image: mysql:latest
container_name: mysql
restart: always
command: --lower_case_table_names=1
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
ports:
- "127.0.0.1:3306:3306"
adminer:
image: adminer
restart: always
ports:
- 8000:8080

Then run the following command in the same directory as the docker-compose.yml file:

1
docker-compose up -d

You can now use adminer to connect to the MySQL server by visiting http://localhost:8000.

lower-case-table-names=1 is used to make table names case-insensitive. This is useful when you are running MySQL on a case-insensitive file system, such as Windows or macOS. see https://dev.mysql.com/doc/refman/8.3/en/identifier-case-sensitivity.html for more information.

You can use a sample table to test the MySQL server. Here is an example of a user table:

1
2
3
4
5
6
7
8
CREATE TABLE `user` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`enabled` boolean,
`created` date,
PRIMARY KEY (`id`)
);

Volume

To add a volume to the MySQL container, you can add the following lines to the docker-compose.yml file:

1
2
volumes:
- ./data:/var/lib/mysql

volume ./data will be mounted to /var/lib/mysql in the container. This is useful when you want to persist the data even after the container is removed.