Code Monkey home page Code Monkey logo

docker-demo's Introduction

Deploy a Rails app with Docker Compose

Cài đặt Docker và Docker Compose

# cài docker bằng lệnh curl
curl -fsSL https://get.docker.com/ | sh

# Cài đặt docker-compose
COMPOSE_VERSION=`git ls-remote https://github.com/docker/compose | grep refs/tags | grep -oP "[0-9]+\.[0-9][0-9]+\.[0-9]+$" | tail -n 1`
sudo sh -c "curl -L https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose"
sudo chmod +x /usr/local/bin/docker-compose
sudo sh -c "curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VERSION}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose"

# Thêm current user vào docker group để khỏi dùng sudo
sudo usermod -aG docker $USER
sudo reboot

Tổ chức các file Docker

Ý tưởng:

  • Lưu Dockerfile, các file scripts không thay đổi giữa các môi trường vào thư mục /docker.
    • Mỗi image cần build nên đặt ở trong từng folder riêng, bao gồm Dockerfile và các file scripts liên quan.
    • Những image sử dụng trực tiếp từ offical image thì có thể khai báo thẳng trong docker-compose.
    • Những file scripts dùng chung thì để trong folder /common
  • File config docker-compose sẽ khác nhau giữa các môi trường và đặt tên theo kiểu: docker-compose.[env].yml
  • Khi thực hiện các lệnh với docker-compose thì thêm cờ -f trỏ tới file config. Ví du: docker-compose -f docker-compose.dev.yml up. Vì đoạn docker-compose -f docker-compose.dev.yml là giống nhau trong 1 môi trường nên có thể đặt alias cho ngắn gọn.

Build Dockerfile và config Docker Compose cơ bản

Các file liên quan:

  • docker/app/entrypoint.sh: Tập hợp các task của app chạy mỗi lần deploy
  • docker/common/wait-for-it.sh: Script yêu cầu đợi. Cụ thể container app sẽ phải đợi db khởi tạo xong mới được chạy, tránh bị lỗi không connect được

Thực hiện build và deploy

  1. Đặt alias để giản lược câu lệnh của docker-compose
alias dc="docker-compose -f docker-compose.dev.yml"
  1. Cấp quyền thực thi cho các file .sh:
  • Có 2 cách cấp quyền thực thi cho các file .sh.
    • Một là cấp quyền ngay trong Dockerfile, ưu điểm là không bị miss, thực hiện tự động. Tuy nhiên nhược điểm là mỗi lần có thay đổi trong những file .sh này thì sẽ phải build lại images
    • Hai là thực hiện cấp quyền ngay bên ngoài host rồi mount vào container. Ưu điểm là thay đổi file này chỉ cần build lại container thay vì build lại toàn bộ images, nhược điểm là dễ miss file, phải thực hiện thủ công
chmod +x docker/*/*.sh
  1. Create network
docker network create demo_docker
  1. Build các images
dc build
  1. Build container
dc up
hoặc
dc up -d # chạy ở chế độ daemon
  1. Stop and remove container
dc down

Một số lưu ý

  • depends_on: Start service theo thứ tự trong list depends_on trước. Tuy nhiên, nó chỉ đợi start service chứ không đợi service "ready", vì vậy những service phụ thuộc cần phải đợi "ready" (ví dụ app phải đợi DB khởi tạo thành công mới migrate được) thì nên sử dụng scripts docker/common/wait-fot-it.sh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.