คู่มือภาษาและกรอบงาน
วิธีสร้าง Dockerfile อย่างมืออาชีพ
1 นาที
docker เป็นเทคโนโลยีที่ช่วยให้คุณสามารถบรรจุและรันแอปพลิเคชันในคอนเทนเนอร์ back4app containers ใช้เทคโนโลยี docker และใช้คอนเทนเนอร์ docker เพื่อแยกและรันแอปพลิเคชัน ซึ่งมีข้อดีหลายประการ เช่น ประสิทธิภาพที่ดีขึ้น ความปลอดภัยที่เพิ่มขึ้น และการปรับใช้และขยายที่ง่ายขึ้น dockerfile คือสคริปต์ที่มีคำสั่งสำหรับการสร้างภาพ docker เพื่อสร้าง dockerfile สำหรับแอปพลิเคชันใด ๆ คุณต้องปฏิบัติตามขั้นตอนพื้นฐานเหล่านี้ เลือกภาพพื้นฐาน (from) ขั้นตอนแรกคือการเลือกภาพพื้นฐานสำหรับ dockerfile ของคุณ ภาพพื้นฐานควรมีระบบปฏิบัติการและการพึ่งพาที่จำเป็นสำหรับแอปพลิเคชันของคุณ คุณสามารถใช้ภาพอย่างเป็นทางการจาก docker hub หรือเลือกภาพจากแหล่งที่เชื่อถือได้ ตั้งค่าไดเรกทอรีทำงาน (workdir) ถัดไป คุณต้องตั้งค่าไดเรกทอรีทำงานในคอนเทนเนอร์ที่ไฟล์แอปพลิเคชันจะอยู่ คุณสามารถใช้คำสั่ง workdir เพื่อกำหนดไดเรกทอรีทำงาน คัดลอกไฟล์แอปพลิเคชัน (copy) ตอนนี้ คุณต้องคัดลอกไฟล์แอปพลิเคชันไปยังไดเรกทอรีทำงาน คุณสามารถใช้คำสั่ง copy เพื่อคัดลอกไฟล์ ติดตั้งการพึ่งพา (run) หากแอปพลิเคชันของคุณมีการพึ่งพาใด ๆ คุณต้องติดตั้งในคอนเทนเนอร์ คุณสามารถใช้ตัวจัดการแพ็คเกจที่เหมาะสม (เช่น apt get , yum , pip , ฯลฯ) เพื่อติดตั้งการพึ่งพา กำหนดจุดเริ่มต้น (cmd) ขั้นตอนสุดท้ายคือการกำหนดจุดเริ่มต้นสำหรับคอนเทนเนอร์ ซึ่งเป็นคำสั่งที่จะรันเมื่อคอนเทนเนอร์เริ่มทำงาน จุดเริ่มต้นควรเริ่มแอปพลิเคชัน นี่คือตัวอย่างง่าย ๆ ของ dockerfile สำหรับแอปพลิเคชันทั่วไป nodejs bashcopy code \# use the official node js image as the base image from node 18 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# install the application dependencies run npm install \# define the entry point for the container cmd \["npm", "start"] flask # use the official python image as the base image from python 3 8 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# install the application dependencies run pip install r requirements txt \# define the entry point for the container cmd \["flask", "run", " host=0 0 0 0"] reactjs # use the official node js image as the base image from node 18 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# install the application dependencies run npm install \# build the react application run npm run build \# expose port 3000 expose 3000 \# define the entry point for the container cmd \["npm", "start"] django # use the official python image as the base image from python 3 8 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# install the application dependencies run pip install r requirements txt \# define the entry point for the container cmd \["python", "manage py", "runserver", "0 0 0 0 8000"] php # use the official php image as the base image from php 7 4 apache \# copy the application files into the container copy /var/www/html \# set the working directory in the container workdir /var/www/html \# install necessary php extensions run apt get update && apt get install y \\ libicu dev \\ libzip dev \\ && docker php ext install \\ intl \\ zip \\ && a2enmod rewrite \# expose port 80 expose 80 \# define the entry point for the container cmd \["apache2 foreground"] elixir # use the official elixir image as the base image from elixir 1 12 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# install the application dependencies run mix local hex force && \\ mix local rebar force && \\ mix deps get \# build the application run mix compile \# define the entry point for the container cmd \["mix", "phx server"] laravel # use the official php image as the base image from php 7 4 apache \# copy the application files into the container copy /var/www/html \# set the working directory in the container workdir /var/www/html \# install necessary php extensions run apt get update && apt get install y \\ libicu dev \\ libzip dev \\ && docker php ext install \\ intl \\ zip \\ && a2enmod rewrite \# install composer run curl ss https //getcomposer org/installer | php install dir=/usr/local/bin filename=composer \# install laravel dependencies run composer install no dev \# expose port 80 expose 80 \# define the entry point for the container cmd \["apache2 foreground"] go # use the official go image as the base image from golang 1 15 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# build the application run go build o main \# expose port 8080 expose 8080 \# define the entry point for the container cmd \[" /main"] rust # use the official rust image as the base image from rust 1 48 \# set the working directory in the container workdir /app \# copy the application files into the working directory copy /app \# build the application run cargo build release \# expose port 8080 expose 8080 \# define the entry point for the container cmd \[" /target/release/app"] เมื่อคุณสร้าง dockerfile เสร็จแล้ว คุณต้องวางมันไว้ที่รากโปรเจกต์ของคุณหรือในโฟลเดอร์ใด ๆ (เพียงระบุสิ่งนั้นในพารามิเตอร์ root ในการตั้งค่าแอป) และสร้างหรือปรับใช้แอปพลิเคชันของคุณใหม่ back4app จะสร้างภาพ docker ตามไฟล์นี้และสร้าง container โดยใช้ภาพนี้ ด้วยวิธีนี้ คุณสามารถเรียกใช้แอปพลิเคชันของคุณใน container ของ bak4app