At the Docker study session, I thought it would be a waste to fill the created repository as it is, so I wrote it as an article. I hope it will be helpful to someone.
https://github.com/yodev21/docker_tutorial_rails
Create the following file
Dockerfile
docker-compose.yml
Gemfile
Gemfile.lock
#Ruby in the image name(Ver2.6.5)Specify the image of the execution environment of
FROM ruby:2.6.5
#Update the list of packages and install the packages required to build the rails environment
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
#Create a directory for your project
RUN mkdir /myapp
#Set to working directory
WORKDIR /myapp
#Copy to project directory
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
#run bundle install
RUN bundle install
#Copy all the contents of the build context to myapp
COPY . /myapp
docker-compose.yml
version: '3'
services:
db:
#Get image of postgres
image: postgres
environment:
POSTGRES_USER: 'postgresql'
POSTGRES_PASSWORD: 'postgresql-pass'
restart: always
volumes:
- pgdatavol:/var/lib/postgresql/data
web:
#Build and use images from Dockerfile
build: .
#Executed when container starts
command: bundle exec rails s -p 3000 -b '0.0.0.0'
#Current directory/Bind mount to myapp
volumes:
- .:/myapp
#Publish at 3000 and transfer to 3000 in container
ports:
- "3000:3000"
#Start the db service before starting the web service
depends_on:
- db
#Create a pgdatabol volume for data persistence and mount the postgresql data area
volumes:
pgdatavol:
source 'https://rubygems.org'
gem 'rails', '5.2.4.2'
Gemfile.lock
docker-compose run web rails new . --force --database=postgresql
Fixed database config file used for rails project
database.yml
default: &default
adapter: postgresql
encoding: unicode
# --------add to--------
host: db
username: postgresql
password: postgresql-pass
# --------So far--------
docker-compose up -d
docker-compose build --no-cache
docker-compose run web rails db:create
docker-compose run web bin/rails g scaffold User name:string
docker-compose run web bin/rails db:migrate
http://localhost:3000/users
docker-compose stop
docker-compose down
docker-compose run web bash
Directory move
cd doc/golang/
FROM golang:1.9
RUN mkdir /echo
COPY main.go /echo
CMD ["go", "run", "/echo/main.go"]
docker image build -t example/echo:latest .
docker image ls
docker container run -d -p 9000:8080 example/echo:latest
curl http://localhost:9000
Recommended Posts