2

I managed to dockerize my existing Rails application running on a Mysql database. But I wonder if it is possible to setup docker-compose to create the test database in the same container?

Here is my docker-compose.yml and it wirks fine with the mysql for developing

version: '2'

volumes:
  db-data:

services:
  db:
    image: mysql:5.5
    restart: always
  ports:
    - "3307:3306"
  environment:
    MYSQL_ROOT_PASSWORD: verysecret
    MYSQL_USER: appdb
    MYSQL_PASSWORD: secret
    MYSQL_DATABASE: appdb
  volumes:
    - db-data:/var/lib/mysql

  web:
    build: .
    command: bundle exec rails s -p 3000
    volumes:
    - .:/app
  ports:
    - "3000:3000"
  links:
    - db
  depends_on:
    - db

Can I add a darabase more in environment part somehow?

2 Answers 2

1

You can create only one DB per Mysql container with docker compose. In your case, I think you should create a second DB container for the second database (isolated from the "real" DB, which is a good practice). If you really want to have the 2 databases in the same container, you will have to create a Dockerfile based on the Mysql image, and add your command lines (RUN) to create the second DB.

HTH

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can, but it won't necessarily be as a single container, but a manager for coupling containers. If this is okay, I've added some steps that will help you configure your project. If not, I've added how to run a single image from a docker-compose file

You will want to start off by creating a docker-compose.yml file in the source directory for your project.

Then you'll want to add something like this inside your yml file.(this was taken from Docker's quick-start documentation. Modified to show mysql instead of postgres)

version: '3'
services:
  db:
    image: mysql
  web:
    build: .
    command: bundle exec rails s -p 3306 -b '0.0.0.0'

volumes:
  - .:/myapp
ports:
  - "3000:3000"
depends_on:
  - db

Detail on how they create one can be found here: https://docs.docker.com/compose/rails/#define-the-project.

Things to note:

  • web is the details about the rails container. So you will want to add an image property if you have already created your rails image.
  • Also, build: . is expecting your Dockerfile to be in the same location as your project. So if you create this docker-compose.yml somewhere else, you'll have to provide the path.
  • depends_on allows your app to build the DB before running rails

Once you finished creating the docker-compose.yml file run: docker-compose build followed by: docker-compose up

If separating the containers, isn't what you are looking for: then you might want to look into creating a single image running both applications. Then use something like this to run from docker-compose.

version: '3'
services:
  app:
    image: {your-app-image}
    build: .
volumes:
  - .:/myapp
ports:
  - "3000:3000"
  - "3306:3306"

Note: somethings might vary on how you create your image from the Dockerfile.

2 Comments

Thanks but as stated it already works with my docker and docker-compose. The question is if i can have compose to create to databases ob the same container
Np. I sort of referenced how you could solve that issue. I think if you're trying to create the entire app inside one container, you might want to try adding more layers in your Dockerfile to include the MySQL app, and dependencies - then expose the additional port. I don't know if you can merge containers directly from docker-compose. Maybe someone can answer it and teach us both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.