Docker File to Create a MySql container
Osama Nasir
Senior Dot Net Developer | Back-End Developer | .NET | ASP.NET CORE | ASP.NET MVC | React | Entity Framework | Angular | SQL | Azure | AWS
Docker Compose File Code#
### Docker Compose File
version: '3.9'
name: 'mysql-db'
services:
db:
image: mysql:latest
container_name: mysql-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USER: user
MYSQL_PASSWORD: user
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
- c:/aaa:/aaa
volumes:
db_data:
### Components Explained
1. Version:
version: '3.9'
- Specifies the version of the Docker Compose file format. Version 3.9 is suitable for deploying applications on Docker Swarm and supports various features for configuring services.
2. Name:
name: 'mysql-db'
- This sets a name for the Docker Compose project. It helps in identifying the project when managing multiple Docker Compose applications.
3. Services:
The services section contains definitions for the containers that will be created. Here, there is a single service named db.
#### 3.1. Database Service Configuration
db:
image: mysql:latest
container_name: mysql-db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USER: user
MYSQL_PASSWORD: user
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
- c:/aaa:/aaa
- db:
- This is the identifier for the service, and it is the name you will use to refer to this specific container.
- image:
```yaml
image: mysql:latest
领英推荐
- Specifies the Docker image to use for this service. Here, it uses the latest version of the official MySQL image from Docker Hub.
- container_name:
```yaml
container_name: mysql-db
- Names the container mysql-db. This makes it easier to manage and reference the container directly via Docker commands.
- environment:
This section sets environment variables that configure the MySQL server.
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USER: user
MYSQL_PASSWORD: user
- MYSQL_ROOT_PASSWORD: Sets the password for the root user of MySQL to root.
- MYSQL_DATABASE: Creates a default database named test upon initialization.
- MYSQL_USER: Creates a new user named user.
- MYSQL_PASSWORD: Sets the password for the new user user to user.
- ports:
ports:
- "3306:3306"
- Maps port 3306 on the host to port 3306 in the container. This allows external applications to connect to the MySQL server.
- volumes:
This section defines how data is stored and managed.
volumes:
- db_data:/var/lib/mysql
- c:/aaa:/aaa
- db_data:/var/lib/mysql:
- Uses a named volume called db_data to persist MySQL data. This ensures that the database files are stored separately from the container, preventing data loss when the container is stopped or removed.
- c:/aaa:/aaa:
- Mounts a directory from the host (`c:/aaa`) to the container at /aaa. This allows for file sharing between the host and the container.
### Volumes
volumes:
db_data:
- This section defines a named volume called db_data. Named volumes are managed by Docker and can be reused across containers. They are useful for persisting data and ensuring that it remains available even if the container is deleted.
### Summary
This Docker Compose file sets up a MySQL database service with the following features:
- Utilizes the latest MySQL image.
- Configures initial settings for the database, root user, and a regular user with passwords.
- Exposes the MySQL service on the standard port 3306.
- Persists MySQL data using a named volume (`db_data`).
- Optionally mounts a specified directory from the host to the container for additional file access.
### Usage
To start the services defined in this file, you would use:
docker-compose up -d
This command runs the service in detached mode, allowing you to continue using the terminal.
To stop and remove the containers, you can use:
docker-compose down