Module 5 – How to Build Docker Images

Docker Image එකක් Build කරන විදිහ

හැඳින්වීම

Docker භාවිතා කරන විට application එක container එකක් තුළ run කරන්න Docker Image එකක් අවශ්‍ය වෙනවා. Docker Image කියන්නේ container එකක් create කරන්න භාවිතා කරන template එකක්. Docker Image එක build කිරීම සඳහා සාමාන්‍යයෙන් Dockerfile කියන configuration file එක භාවිතා කරනවා. Dockerfile එක තුළ application එක run වෙන්න අවශ්‍ය instructions define කරනවා.


Dockerfile කියන්නේ මොකක්ද?

Dockerfile කියන්නේ Docker image build කිරීම සඳහා භාවිතා කරන text file එකක්.

මෙම file එක තුළ define කරනවා:

  • Base image
  • Application files
  • Dependencies
  • Application run කරන command

Docker build process එකේදී Dockerfile එක read කරලා Docker Image එක create වෙනවා.


Simple Dockerfile Example

Node.js application එකක් සඳහා simple Dockerfile එකක් මෙහෙම ලියන්න පුළුවන්.


                    FROM node:18

                    WORKDIR /app

                    COPY . .

                    RUN npm install

                    CMD ["node", "app.js"]
                

මෙහි:

  • FROM → base image එක select කරනවා<
  • WORKDIR → working directory define කරනවා
  • COPY → application files copy කරනවා
  • RUN → dependencies install කරනවා
  • CMD → application run කරන command එක define කරනවා

Docker Image Build කරන Command

Dockerfile create කරලා ඉවර වුනාට පස්සේ Docker image build කරන්න පහත command එක භාවිතා කරනවා.

docker build -t my-app-image .

මෙහි:

  • build → image build කරන command එක
  • -t → image name (tag) define කරනවා
  • . → current directory එක build context ලෙස භාවිතා වෙනවා

Build process එක complete වුනාම Docker image එක system එකේ store වෙනවා.


Build කරපු Images බලන විදිහ

Build කරපු Docker images list එක බලන්න මේ command එක භාවිතා කරන්න පුළුවන්.

docker images

මෙම command එකෙන් system එකේ තිබෙන Docker images list එක display වෙනවා.

Image එකෙන් Container Run කිරීම

Build කරපු image එකෙන් container එක run කරන්න පහත command එක භාවිතා කරනවා.

docker run -p 3000:3000 my-app-image

මෙහි application එක container එක තුළ run වෙනවා.