Terminus by Warp
Launch MySQL Using Docker Compose

Launch MySQL Using Docker Compose

Emmanuel Oyibo
Emmanuel Oyibo

The short answer

To set up a MySQL database container in Docker Compose, you can start by creating a [.inline-code] compose.yaml[.inline-code]  file within your project's directory with the following content:

version: '3'

services:
 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: <root_password>
     MYSQL_DATABASE: <database_name>
     MYSQL_USER: <user_name>
     MYSQL_PASSWORD: <user_password>
   ports:
     - 3306:3306

Where:

  • [.inline-code] root_password[.inline-code]  is the required password for the MySQL root account.
  • [.inline-code] database_name[.inline-code]  is the name for the default database that the new container creates when it starts.
  • [.inline-code] user_name[.inline-code]  and [.inline-code] user_password[.inline-code]  are optional environment variables that you can use altogether to create a new user with superuser privileges for the database specified in [.inline-code] database_name[.inline-code] .

Then run the following [.inline-code] docker compose[.inline-code]  command to start the MySQL container:

$ docker compose -f compose.yaml up -d

Where:

  • The [.inline-code] -f[.inline-code]  option specifies the Compose file’s path.
  • The [.inline-code] -d[.inline-code]  option launches the services in detached mode (i.e. in the background).

If you want to learn how to do the same thing with PostgreSQL, you can read our other article on how to launch PostgreSQL using Docker Compose.

[#easily-recall-syntax-with-ai] Easily retrieve this syntax using Warp AI [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this syntax using the Warp AI feature:

For example, entering [.inline-code] docker compose mysql[.inline-code]  in the AI question input will prompt a human-readable step by step guide including code snippets.

[#persist-the-database-data] Persisting the container's data with named volumes [#persist-the-database-data]

By default, all the data generated in the MySQL container will be lost upon termination.

To prevent this data loss, you can use the top-level [.inline-code] volumes[.inline-code]  property in your [.inline-code] compose.yaml[.inline-code]  file to create a named volume, which is a persistent storage mechanism provided by Docker:

version: '3'

services:
 database:
   image: mysql:latest
   environment:
     MYSQL_ROOT_PASSWORD: <root_password>
     MYSQL_DATABASE: <database_name>
     MYSQL_USER: <user_name>
     MYSQL_PASSWORD: <user_password>
   ports:
     - 3306:3306
   volumes:
     - <volume_name>:<volume_location>

volumes:
 <volume_name>:

Where:

  • [.inline-code] <volume_name>[.inline-code]  is the name of the volume the container's data will be persisted on.
  • [.inline-code] <volume_location>[.inline-code]  is the path where the volume will be mounted within the container. By default, [.inline-code] /var/lib/mysql[.inline-code] .

For example:

version: '3'

services:
 database:
   image: mysql:latest
   environment:
     MYSQL_ROOT_PASSWORD: password102
     MYSQL_DATABASE: my-database
     MYSQL_USER: emminex
     MYSQL_PASSWORD: password104
   ports:
     - 3306:3306
   volumes:
     - mysql_data:/var/lib/mysql

volumes:
 mysql_data:

You can learn more about persisting data in Docker Compose by reading the official documentation page on named volumes and our other article on how to create a bind mount in Docker Compose.

[#initialize-the-database-with-scripts] Initializing the database using scripts [#initialize-the-database-with-scripts]

When running a new database instance, it is sometimes necessary to initialize it by automating the creation of tables, populating it with a default dataset, or customizing its configuration before other applications can connect to it.

To do so, you can add all your initialization scripts (i.e., [.inline-code] .sql[.inline-code] , [.inline-code] .sh[.inline-code] , or [.inline-code] .sql.gz[.inline-code] ) in a directory on your local machine and bind it to the MySQL container's [.inline-code] /docker-entrypoint-initdb.d[.inline-code]  directory through the [.inline-code] volumes[.inline-code]  property as follows:

version: '3'

services:
 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: <root_password>
     MYSQL_DATABASE: <database_name>
     MYSQL_USER: <user_name>
     MYSQL_PASSWORD: <user_password>
   ports:
     - 3306:3306
   volumes:
     - <scripts>:/docker-entrypoint-initdb.d

Where:

  • [.inline-code] scripts[.inline-code]  is the path to the directory on your local machine containing the initialization scripts.

Upon startup, all the scripts located in the [.inline-code] docker-entrypoint-initdb.d[.inline-code]  directory will automatically be executed by the container.

[#restart-the-database-on-failure] Automatically restarting the database on failure [#restart-the-database-on-failure]

To automatically restart your MySQL container in case of failure or unexpected behavior, you can set the [.inline-code] restart[.inline-code]  property to [.inline-code] always[.inline-code] , which will ensure that your container keep running forever until it is manually stopped.

For example:

version: '3'

services:
 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
     MYSQL_DATABASE: ${MYSQL_DATABASE}
     MYSQL_USER: ${MYSQL_USER}
     MYSQL_PASSWORD: ${MYSQL_PASSWORD}
   ports:
     - 3306:3306
   restart: always

You can learn more about restart policies in Compose by reading our other article on how to restart containers in Docker Compose https://www.warp.dev/terminus/docker-compose-restart.

[#create-a-dependency-between-services] Expressing a dependency between MySQL and other services [#create-a-dependency-between-services]

To ensure that the database container is up and running before the various services defined in the [.inline-code] compose.yaml[.inline-code]  file are launched, you can create a dependency between them using the [.inline-code] depends_on[.inline-code]  property.

In this example, Compose will first launch the [.inline-code] database[.inline-code]  service, then wait for it to be up and running before launching the [.inline-code] website[.inline-code]  service:

version: '3'

services:
 website:
   image: nginx
   depends_on:
     - db

 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: password102
     MYSQL_DATABASE: my-database
     MYSQL_USER: emminex
     MYSQL_PASSWORD: password104
   ports:
     - 3306:3306

[#ensure-a-healthy-database-connection] Ensuring a healthy database connection [#ensure-a-healthy-database-connection]

Although the [.inline-code] depends_on[.inline-code]  property tells Compose to start the MySQL container first, it doesn’t guarantee that the database is fully ready to handle requests.

To solve this issue, you can use the [.inline-code] healthcheck[.inline-code]  property to define a [.inline-code] condition[.inline-code]  that will determine whether it is safe for other services to attempt to connect to the database.

In this example, Compose will first launch the [.inline-code] database[.inline-code]  service, then execute the command defined in the [.inline-code] healthcheck.test[.inline-code]  property every 10 seconds to verify the service's readiness, and finally launch the [.inline-code] website[.inline-code]  service once the command is deemed successful:

version: '3'

services:
 website:
   image: nginx
   depends_on:
     database:
       condition: service_healthy

 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: password102
     MYSQL_DATABASE: my-database
     MYSQL_USER: emminex
     MYSQL_PASSWORD: password104
   ports:
     - 3306:3306
   healthcheck:
     test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
     interval: 10s
     timeout: 5s
     retries: 5

You can learn more startup conditions in Compose by reading our other article on how to perform a health check in Docker Compose.

[#keep-the-database-credentials-safe] Keeping the database credentials safe with [.inline-code] .env[.inline-code] files [#keep-the-database-credentials-safe]

To prevent the accidental leak of sensitive information such as database credentials, it is considered good practice to keep them outside of your Compose configuration by placing them in an environment file that is not pushed to your repository.

To do so, you can first create a new [.inline-code] .env[.inline-code]  file within the same directory as your [.inline-code] compose.yaml[.inline-code]  file containing your MySQL credentials:

MYSQL_ROOT_PASSWORD=password102
MYSQL_DATABASE=my-database
MYSQL_USER=emminex
MYSQL_PASSWORD=password104

Then replace the values of the environment variables in your [.inline-code] compose.yaml[.inline-code]  file using the variable interpolation syntax:

version: '3'

services:
 database:
   image: mysql
   environment:
     MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
     MYSQL_DATABASE: ${MYSQL_DATABASE}
     MYSQL_USER: ${MYSQL_USER}
     MYSQL_PASSWORD: ${MYSQL_PASSWORD}
   ports:
     - 3306:3306

When running the [.inline-code] docker compose up[.inline-code]  command, Compose will automatically replace those variables at runtime.

You can learn more about using environment variables in Compose by reading our other article on how to use an .env file in Docker Compose.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.