Create a docker folder directly under the project, and create a db_data folder (may have a different name) and docker-compose.yml directly under it.
concentratio #Project root directory
├── config 
│   └── ...
│   
├── docker 
│   ├── db_data #For data persistence
│   └── docker-compose.yml
└── ...
docker/docker-compose.yml
version: '2'
services:
  db:
    image: mariadb:latest
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
    environment:
      - MYSQL_ROOT_USER=root
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=concentratio
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user
    volumes:
      - db_data:/var/lib/mysql # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
      - ./db_data:/docker-entrypoint-initdb.d # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
      - ./db_data:/etc/mysql/conf.d # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
    ports:
      - '3333:3306'
volumes:
  db_data:
    driver: local
Since the db_data folder is mounted in /docker-entrypoint-initdb.d, if you put an arbitrary SQL file in the db_data folder, it will be started when the container is started (first time?). It will execute the SQL file. </ font>
Move to the docker directory with the cd command and then start the MySQL container with docker-compose up -d.
docker$ docker-compose up -d
Creating docker_db_1 ... done
By the way
Stop is docker-compose stop (or docker stop container ID or container name)
Delete is docker-copose down -v (or docker rm container ID or container name)
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
03dd318a2ad7        mariadb:latest      "docker-entrypoint.s…"   29 seconds ago      Up 29 seconds       0.0.0.0:3333->3306/tcp   docker_db_1

It looks like I was able to connect
pip3 install django-environ mysqlclient
concentratio #Project root directory
├── config 
│   └── ...
│   
├── docker 
│   └── ...
│
├──.env
│
└── ...
 .env.default
DEBUG=True
DATABASE_URL=mysql://user:[email protected]:3333/concentratio
config/settings.py
.
..
...
import environ #add to(django-Import environ)
.
..
...
#add to
ENV_FILE = os.path.join(BASE_DIR, '.env') # .env file path
ENV = environ.Env()
ENV.read_env(ENV_FILE) # django-in environ.Read env file
...
..
.
#add to
DATABASES = {
    'default': ENV.db()
}
DATABASES['default']['ATOMIC_REQUESTS'] = True # ATOMIC_When REQUESTS is set to True, the entire view becomes a transaction (if an exception occurs during view processing, the previous DB operation is rolled back).
...
..
.
python3 manage.py makemigrations
python3 manage.py migrate
The migrate command is okay as long as various OKs are given.
$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
A django admin table has been created.
DB migration is complete.

You can create a superuser with python3 manage.py createsuperuser.
$ python3 manage.py createsuperuser
Username : admin #Set appropriately (this time, admin)
Email address: [email protected] #Set appropriately (admin this [email protected])
Password: #Set appropriately (this time, admin)
Password (again): #Set appropriately (this time, admin)
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y #I get a warning that the password is easy, but it doesn't matter if it's "y"
Superuser created successfully.

The user has been created. that's all.
Recommended Posts