I used Docker for my portfolio as a beginner, so I hope that even 1mm will be helpful to someone.

Finally, the portfolio has been completed and deployed. I would like to output hard.

** Created app ToDoGame ** Image 2020-10-27 at 10.45.17 AM.png github

** Technology used ** Image 2020-11-01 at 3.32.12 PM.png

Since I finally incorporated Docker, I would like to introduce articles and videos that were helpful with gratitude.

I learned the basics of Docker in this video

Docker course to catch up from now on! Aim for container master with AWS ECS and Fargate! ~ Series 1st ~

"How free !!!"

Where this video was helpful ↓

I was convinced that I wanted to embed Docker

Docker is growing in demand and will become an essential technology for engineers.

I understood what Docker is

A container for storing things inside. The container contains the application.

It's quite convenient, such as carrying your home application to work in a portable container.

About Dockerfile

[What is Docker? What is a Dockerfile? ](Https://qiita.com/matsugaknight/items/c77fec14ec3c281a47b4#:~:text=Dockerfile%20%E3%81%A8%E3%81%AF-,Docker%E4%B8%8A%E3%81% A7% E5% 8B% 95% E4% BD% 9C% E3% 81% 95% E3% 81% 9B% E3% 82% 8B% E3% 82% B3% E3% 83% B3% E3% 83% 86% E3% 83% 8A% E3% 81% AE% E6% A7% 8B% E6% 88% 90% E6% 83% 85% E5% A0% B1,% E3% 81% 99% E3% 82% 8B% E3 % 81% 9F% E3% 82% 81% E3% 81% AE% E3% 83% 95% E3% 82% A1% E3% 82% A4% E3% 83% AB% E3% 81% A7% E3% 81 % 99% E3% 80% 82)

It is a file to describe the configuration information of the container to be operated on Docker.

Create a Docker image with the docker build command based on the information described in the Dockerfile.

The reason for thinking about this configuration

When I read this article, I was inspired by my own respect, saying, "It's too amazing ... I don't think I'm in the same job hunting." [Part 1] WEB service created with Rails + Nuxt + MySQL + Docker is automatically tested and deployed with ECS / ECR / CircleCI and converted to terraform

My composition this time

ToDoGame
├─docker-compose.yml 
├─frontend
|   ├─Dockerfile
└─backend
    ├─Dockerfile
    ├─Gemfile
    └─Gemfile.lock

Dockerfile

backend side

#Specifying the base image(Use alpine to reduce the weight of the image)
FROM ruby:2.7.1-alpine3.10

#Download required packages
ENV RUNTIME_PACKAGES="linux-headers libxml2-dev make gcc libc-dev nodejs tzdata mysql-dev mysql-client yarn" \
    DEV_PACKAGES="build-base curl-dev" \
    HOME="/app" \
    LANG=C.UTF-8 \
    TZ=Asia/Tokyo

#Move to working directory
WORKDIR ${HOME}

#Copy the necessary files from the host (files on your computer) to Docker
ADD Gemfile ${HOME}/Gemfile
ADD Gemfile.lock ${HOME}/Gemfile.lock

RUN apk update && \
    apk upgrade && \
    apk add --update --no-cache ${RUNTIME_PACKAGES} && \
    apk add --update --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
    bundle install -j4 && \
    apk del build-dependencies && \
    rm -rf /usr/local/bundle/cache/* \
    /usr/local/share/.cache/* \
    /var/cache/* \
    /tmp/* \
    /usr/lib/mysqld* \
    /usr/bin/mysql*

#Copy the necessary files from the host (files on your computer) to Docker
ADD . ${HOME}

#Open port 3000
EXPOSE 3000

#Execute command
CMD ["bundle", "exec", "rails", "s", "puma", "-b", "0.0.0.0", "-p", "3000", "-e", "development"]

★ Use the ENV command to set a value in an environment variable

★ RUNTIME_PACKAGES are ** parts required to run programs ** "Runtime" + "Package" is "Runtime Package". When the runtime "runs a program (at runtime)"

frontend side

FROM node:12.5.0-alpine

ENV HOME="/app" \
    LANG=C.UTF-8 \
    TZ=Asia/Tokyo

ENV HOST 0.0.0.0

WORKDIR ${HOME}

RUN apk update && \
    apk upgrade && \
    npm install -g n && \
    yarn install &&\
    rm -rf /var/cache/apk/*

★ Access from outside the container is allowed by specifying 0.0.0.0 for the environment variable HOST.

What is Docker Compose?

Docker compose is a function that automates the procedure for building and executing a service consisting of multiple containers, making it easier to manage. With Docker compose, you can prepare a compose file and execute the command once to read the settings from that file and start all container services.

docker-compose.yml

docker-compose.yml


version: "3"

services:
  db:
    image: mysql:5.7
    env_file:
      - ./backend/config/environments/db.env
    restart: always
    volumes:
      - db-data:/var/lib/mysql:cached

  back:
    build: backend/
    # rm -f tmp/pids/server.Useful when you fail to erase the rails server with pid
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 
    env_file:
      - ./backend/config/environments/db.env
    volumes:
      - ./backend:/app:cached
    depends_on:
      - db
    #Host computer port: Port in Docker
    ports:
      - 3000:3000

  front:
    build: frontend/
    command: yarn run dev
    volumes:
      - ./frontend:/app:cached
    ports:
      #Host computer port: Port in Docker
      - 8080:3000
    depends_on:
      - back
volumes:
  public-data:
  tmp-data:
  log-data:
  db-data:

** Interesting because there are still many things I don't understand (finished) **

Article that taught me the setting part

Docker compose hands-on

I made an API container with Rails on Docker (alpine)

Digression

When I changed jobs from November, I had a period of time within myself, so I'm really disappointed that I couldn't introduce AWS ... But now I have no time to dent! I will do my best to change jobs.

Recommended Posts

I used Docker for my portfolio as a beginner, so I hope that even 1mm will be helpful to someone.
[Android] I want to create a ViewPager that can be used for tutorials
I made a question that can be used for a technical interview
I made a THETA API client that can be used for plug-in development
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
I introduced Docker to Rails 6, so I summarized it (beginner)
A collection of patterns that you want to be aware of so as not to complicate the code