This post provides another basic docker-compose.yml .This time is to create a MySQL container with initial user accounts.
Tested with:
- Docker for Windows. Please install this before proceeding.
- MySQL 8. This is the version of MySQL that our Docker image has.
- Windows 10 64-bit
docker-compose.yml: A YAML File
When we use this to create a Docker container, the MySQL instance will have an initial user with turreta username. Moreover, it will enable the root user and create a database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | version: "2.3" services: demo-container-mysq-db: image: mysql:8 #restart: always environment: MYSQL_USER: turreta MYSQL_PASSWORD: turreta+pwd! MYSQL_ROOT_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc MYSQL_DATABASE: turretadb ports: - "3306:3306" networks: - turreta_network networks: turreta_network: driver: bridge |
How to Use the docker-compose.yml For MySQL
Next, open a Windows Command Line prompt and cd to the directory where the docker-compose.yml file is. Then, run the following commands.
1. Pull the image from Docker Hub to the local machine.
To pull the image from Docker Hub, run the following command on the command-line window. If we look at our docker-compose.yml again, we see mysql:8. This means docker-compose will put an image of mysql:8 from Docker Hub.
1 | docker-compose pull |
2. Create and Run a MariaDB container
After pulling the image, we can now create a Docker container for our image. Although it took two steps to start up a container, we don’t have to do it all the time. We can use the following command to pull an image from Dock Hub and start a Docker container in one command.
1 | docker-compose up demo-container-mysql-db |
Once we successfully started the MySQL Docker container, we can already connect our applications to the database.
This post is part of the Docker For Developers tutorial.
Changed
docker-compose up demo-container-db
todocker-compose up demo-container-mysql-db
.