DEV Community

Cover image for MySQL with Docker Compose
Rodolfo Chaves
Rodolfo Chaves

Posted on

MySQL with Docker Compose

Hey Folks!

I want to share a very simple way to create a MySQL instance using Docker Compose.

Create a file named docker-compose.yml with the code bellow. You should replace the <secret_pass> to a safe password for MySQL root username.

version: '3.1'
services:

  mysql_db:
    image: mysql
    restart: always
    container_name: mysql_container
    environment:
      MYSQL_ROOT_PASSWORD: <secret_pass>
    ports:
      - "3306:3306"
    volumes:
      - mysql_db_data:/var/lib/mysql

volumes:
  mysql_db_data:

Type in your terminal docker-compose up to start and docker-compose down to stop the container.
Use docker rm mysql_container -f if you want to remove the container.

You can use MySQL Workbench to access the database using the username as root and password as <secret_pass>.

But if you want to simplify the data visualization is possible to use an Adminer container, see:

version: '3.1'
services:

  mysql_db:
    image: mysql
    restart: always
    container_name: mysql_container
    environment:
      MYSQL_ROOT_PASSWORD: <secret_pass>
    ports:
      - "3306:3306"
    volumes:
      - mysql_db_data:/var/lib/mysql
    networks:
      - mysql_network

  adminer:
    image: adminer
    ports:
      - 8006:8080
    networks:
      - mysql_network

volumes:
  mysql_db_data:

networks:
  mysql_network:
    driver: bridge

Access through http://localhost:8006 and use the credentials defined before.

Have fun!

Top comments (0)