It is troublesome to create docker and docker-compose once while referring to other people's ones, and then start over when you want to create an application with the same settings. So, I will upload the image I made to docker-hub so that I can easily duplicate it. This time, I will use the image created by rails 6.0.3 and mysql8.0.2. (Reference: https://qiita.com/shima-zu/items/b825c5a47b3582ef99cc)
First, check the container ID used on docker-compose, and create an image to push from that container ID to docker-hub.
Enter this command in the terminal to retrieve the state of the container.
macbook-pro rails_app % docker ps -a
Output result
51334eee76eb rails_app_web "entrypoint.sh bash …" 2 days ago Up 31 minutes 0.0.0.0:3000->3000/tcp rails_app_web_1
40f92b2d8ed5 mysql:8.0.20 "docker-entrypoint.s…" 2 days ago Up 31 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp rails_app_db_1
Two containers are used, ID = 51334eee76eb
Name = rails_app_web
, ID = 40f92b2d8ed5
, Name = mysql: 8.0.20
. By the way, mysql: 8.0.20
is equivalent to tag.
To push to docker hub, the image name must include the user name, so create a new image.
tag
For mysql, enter tag = `8.0.20`.
If tag is not specified, latest will be set automatically.
And finally push the image to docker hub.
`docker push Username/ImageName:tag`
You can push with.
Now, pull the image from here, create a container, and go to the point where you issue Yay! You ’re on Rails !.
First, set the working directory and move it.
`mkdir /docker-rails
cd docker-rails`
Pull the image.
`docker pull Username/rails_app_web`
`docker pull Username/mysql`
Create the required files.
` touch { docker-compose.yml, Gemfile, Gemfile.lock, entrypoint.sh, .env }`
If you do not delete the build of docker-compose.yml and change the port number, you will get an error saying `port is already taken`, so be careful.
`docker-compose run --rm web rails new . --force --no-deps --database=mysql --skip-turbolinks`
Create rails-app in the folder and install webpacker again.
`docker-compose run --rm web bin/rails webpacker:install`
Paste database.yml in the same way to create the database.
`docker-compose run --rm web bin/rails db:create`
Finally, launch the container in the background and you're done.
`docker-compose up -d`
Recommended Posts