Skip to content

Instantly share code, notes, and snippets.

@poudelmadhav
Last active March 14, 2024 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poudelmadhav/ea5108859a06d38c520ff10af1e6f583 to your computer and use it in GitHub Desktop.
Save poudelmadhav/ea5108859a06d38c520ff10af1e6f583 to your computer and use it in GitHub Desktop.
Starting docker in rails 7.1 with a separate MySQL container

Setting Up MySQL

  1. Pull the MySQL image:

    docker pull mysql/mysql-server
  2. Run the MySQL container in a persistent volume:

    docker run --rm -d \
      --name=mysql_container \
      -p 3307:3306 \
      -e MYSQL_ROOT_PASSWORD=password \
      -e MYSQL_ROOT_HOST=% \
      -v mysql-data:/var/lib/mysql \
      mysql/mysql-server
    
    • Explanation:
      • --rm: Remove the container when it stops.
      • -d: Run the container in the background.
      • --name=mysql_container: Name the container for easier management.
      • -p 3307:3306: Map the container port 3306 (MySQL) to host port 3307.
      • -e MYSQL_ROOT_PASSWORD=password: Set the root password for the MySQL user.
      • -e MYSQL_ROOT_HOST=%: Allow connections from any host.
      • -v mysql-data:/var/lib/mysql: Mounts the volume (mysql-data) to the container's /var/lib/mysql directory (where MySQL stores data).
      • mysql/mysql-server: Specifies the Docker image for the MySQL server.
  3. Get the container's IP Address:

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql_container

    This will display the container's IP address, which will be used later.

  4. Set environment variables:

    export DATABASE_URL='mysql2://root:password@<container_ip>/<db_name>:3306?encoding=utf8mb4'
    export RAILS_MASTER_KEY=<rails_master_key>
    • Replace <container_ip> with the IP address obtained in the previous step.
    • Replace <db_name> with the name of the database you want to use.
    • Replace <rails_master_key> with the master key from config/master.key.

Running project

  1. Build the project image:

    docker-compose build -t <rails_app> .
    • Replace <rails_app> with the name you want to give to the image.
    • This will build the image using the Dockerfile.
  2. Run the project container:

    docker run --rm -it -p 3000:3000 --name=<rails_app_container> \
      -e DATABASE_URL=$DATABASE_URL \
      -e RAILS_MASTER_KEY=$RAILS_MASTER_KEY \
      <rails_app>
    • Replace <rails_app_container> with the name you want to give to the container.
    • Replace <rails_app> with the name of the image you built in the previous step.
    • Explanation:
      • --rm: Remove the container when it stops.
      • -it: Run in interactive.
      • -p 3000:3000: Map the container port 3000 (Rails) to host port 3000.
      • -e DATABASE_URL=$DATABASE_URL: Set the database URL environment variable.
      • -e RAILS_MASTER_KEY=$RAILS_MASTER_KEY: Set the Rails master key environment variable.

Debugging

  • View MySQL container logs:

    docker logs mysql_container
  • Enter the MySQL container:

    docker exec -it mysql_container bash
    • This will open a bash shell inside the container.
    • You can then run mysql -u root -p to enter the MySQL command line.
    • Use exit to leave the container.

Controlling the MySQL container

  • Stop the container:

    docker stop mysql_container
  • Start the container:

    docker start mysql_container
  • Remove the container:

    docker rm mysql_container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment