[RAILS] [Note] Dockerfile

at first

In my repository, I have a quick start that allows you to set up a rails development environment in the Docker environment. This time, I will summarize the Dockerfile placed there. https://github.com/naokikubo2/quickstart/blob/master/Dockerfile

What is Dockerfile

Coded command for creating docker image Official documentation

Important commands in Dockerfile are

FROM Specifying the base image https://docs.docker.com/engine/reference/builder/#from RUN Command execution https://docs.docker.com/engine/reference/builder/#run COPY Transfer files from local to docker image https://docs.docker.com/engine/reference/builder/#copy

Loading the base image

FROM ruby:2.7

The FROM line is loading the base image of ruby: 2.7.

The> FROM instruction initializes the processing stage for the image build and sets the base image. Subsequent instructions follow. For this reason, the correct Dockerfile must start with the FROM instruction. The base image can be anything that is correct. If you want to get started easily, get her image from a public repository. docker reference

Language designation

ENV LANG C.UTF-8

Set UTF-8 here to use Japanese in rails console.

Actions to remove the warning

ENV DEBCONF_NOWARNINGS yes
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE yes
ENV XDG_CACHE_HOME /tmp
EXPOSE 3000

――When you install something, a warning like "Are you sure you want to install it?" Is displayed, and you may select "yes" each time, right? The first and second lines always select yes when a warning comes.

See below for what APT is

APT is one of the package management systems used to install, manage and remove software on Linux. Also, the command (apt) to operate it. It is used as standard in Debian-based distributions. IT Glossary

apt-key is used to manage the list of keys that apt uses to authenticate packages. ubuntu manual

--Third line: There are cases where the file generated in the mount area of ​​Docker throws an error because you do not have write permission for the cache directory. I have specified a directory with write permission in XDG_CACHE_HOME, but it seems that it does not occur when using Rails only. If there is no problem, you can remove it.

--4th line: Since it is expose, port number 3000 is exposed. In other words, it means that it accepts data from the exposed port number 3000.

Container package update and installation

RUN apt-get update -qq && apt-get install -y \
    build-essential \
    libpq-dev \
    vim \
    less \
    graphviz

--Upadate and then install, so you can use the latest version of the package. --The tools required for the build will be installed --Required for PostgreSQL connection. --Installing vim (you can manipulate the file contents in the terminal) --Added less command (one of the linux commands that can display the contents of a file) --To use the open source tool package graphviz (necessary when outputting ER diagram)

Yarn installation

yarn and npm are package managers used on the program (Node.js) used to perform server-side processing with JavaScript.

RUN apt-get install apt-transport-https
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

--Second line: pubkey is the public key for authentication. --Third line: Save the stable version information of the current yarn. Save the contents output by echo by using the tee command. --Line 4: Installation of yarn.

Set work directory

# setting work directory
RUN mkdir /app
WORKDIR /app

--Second line: Create a directory for your project --Third line: Set in work directory

Set environment variables

ENV HOME /app

For use when entering a script or container.

Create Gemfile

COPY Gemfile /app/Gemfile

Copy a local file called "Gemfile" to your project's directory.

Reference material

https://tech-lab.sios.jp/archives/19191

Recommended Posts

[Note] Dockerfile
Note
Note
Dockerfile memo
About Dockerfile
StringBuilder Note
Note: Installing SDKMAN
Rails Credentials Note
[Note] About nil
[Note] SQLite related
Firebase Authentication Note