Photo by Unsplash

Rails 6: Running on Docker with PHPMyAdmin

One day, I was using my Windows computer. Since the arriving of WSL 2 I wanted to play around with a Rails application. Just for the fun of practicing my Docker skills. The only big requirement for me? Use the Alpine Linux image, well known for the small image size they generate.
After a couple of hours of pain and suffering, I made it. My docker containers were working as expected and it was ready for work. Here, I present all the files and workarounds that I used.

🐳 Dockerfile

First. I created the Dockerfile for the Ruby container. The route was .docker/Dockerfile
1
FROM ruby:2.7.2-alpine
2
RUN \
3
# update packages
4
apk update && apk upgrade && \
5
apk --no-cache add make gcc libc-dev && \
6
apk add --update --no-cache \
7
build-base curl-dev git libxslt-dev libxml2-dev ruby-rdoc mysql-dev \
8
yaml-dev zlib-dev nodejs yarn tzdata
9
10
WORKDIR /app
11
12
# Required gems
13
RUN gem install nokogiri -- --use-system-libraries
14
RUN gem install bundler
15
16
COPY package.json yarn.lock ./
17
RUN yarn install --check-files
18
19
COPY Gemfile Gemfile.lock ./
20
RUN bundle install
21
22
COPY . .
23
24
# clear after installation
25
RUN rm -rf /var/cache/apk/*
26
27
ENV PATH=./bin:$PATH
28
29
EXPOSE 3000
30
CMD ["rails", "console"]
Do you see that tzdata library? That was giving me errors when I tried bundle install. In short, I just wanted to install all my dependencies without any issues. Inside the container, the app will be located in the /app folder and it comes with the MySQL libraries for a DB integration.
Plus, I added nodejs and yarn as Alpine Linux libraries, we will need them as tools for local development use, specially since Rails relies on Webpacker for frontend development.

📦 Docker compose

After that, we can create our docker-compose.yml file. For this case, we are using a Ruby + MySQL 8.0 + PHPMyAdmin combo. Probably you are wondering, why PHPMyAdmin? That’s because I don’t have any SQL UI tool in my Linux computer and I don’t want to use the command line right now.
1
version: '3'
2
services:
3
app:
4
container_name: rails_project_backend
5
command: sh -c "bundle install && yarn install --check-files && bundle exec rails s -p 3000 -b '0.0.0.0'"
6
depends_on:
7
- db
8
build:
9
context: .
10
dockerfile: .docker/Dockerfile
11
ports:
12
- "3000:3000"
13
restart: always
14
volumes:
15
- ".:/app"
16
- "node-modules:/app/node_modules"
17
environment:
18
RAILS_ENV: development
19
DATABASE_HOST: db
20
DATABASE_USER: rails_db_user
21
DATABASE_PASSWORD: rails_db_password
22
DATABASE_NAME: rails_db
23
db:
24
container_name: rails_project_db
25
image: "mysql:8.0"
26
environment:
27
MYSQL_ROOT_PASSWORD: password
28
MYSQL_DATABASE: rails_db
29
MYSQL_USER: rails_db_user
30
MYSQL_PASSWORD: rails_db_password
31
volumes:
32
- './dbdocker:/var/lib/mysql'
33
- './dbdocker_init:/docker-entrypoint-initdb.d'
34
ports:
35
- "3306:3306"
36
phpmyadmin:
37
depends_on:
38
- db
39
image: phpmyadmin/phpmyadmin
40
restart: always
41
ports:
42
- '3500:80'
43
environment:
44
PMA_HOST: db
45
PMA_PORT: 3306
46
MYSQL_USER: root
47
MYSQL_ROOT_PASSWORD: password
48
volumes:
49
node-modules:
In the PHPMyAdmin container we need the root password, because we want to execute “powerful” operations over our databases.
The dbdocker and dbdocker_init are extra folders that you can add to your project if you want to execute extra commands into your MySQL container. Remember once you added it to ignore all the content of dbdocker, where the database files will remain. It’s a good practice to create an empty .gitkeep or .keep file in the folder and then ignoring it. Here is the rule that I’d use in my .gitignore
1
/dockerdb/*
2
!/dockerdb/.gitkeep
Remember that you can use a .env file and then add env_file: - .env to your docker compose file (removing the environment section per container first), if you don’t want to commit your env values or if you change them often.
My posts are not AI generated, they might be only AI corrected. The first draft is always my creation

Author

Written by Helmer Davila