Knowledge Bank
  • My GitBook
  • Miscellaneous
  • Project
    • Rider and Intellij
    • Code
    • Frontend
      • Condition
      • AddConditionModalDialog
    • Backend
    • e2e
      • fragments
  • JAVASCRIPT
    • Promise
    • Destructuring
    • Spread Syntax and Rest Parameters
    • Typescript
      • Examples of Types
      • React Typescript
    • This
    • Dot Notation vs Bracket Notation
    • Shallow vs Deep Clone
    • New ES Edition
  • C#
    • Project Note
    • Basic
    • Shortcut and Debugging
  • Programming Paradigms
    • SOLID Principles
    • Object Oriented Programming (OOP)
      • Evolution of OOP (Procedural to OOP)
      • Instantiation
      • 4 Pillars of OOP
      • Extra
    • Functional Programming (FP)
      • Idempotent
      • Imperative vs Declarative
      • Immutability
      • High Order Function and Closure
      • Currying
      • Partial Application
      • Memoization and Caching
      • Compose and Pipe
      • Extra
      • Example of FP
    • OOP vs FP
      • Composition vs Inheritance
  • DATA STRUCTURE
    • Big O
    • Data Structure
    • Array
    • Hash Table
    • Linked List
    • Queue and Stack
    • Tree
      • Binary Heap
      • Trie
    • Graph
      • Example of Graph
  • React-Redux
    • MobX
    • Best Practices
  • Algorithms
    • Recursion
      • Examples of Recursion
    • Sorting
    • Searching and Traversal
    • Dynamic Programming
  • REFACTORING
    • Clean Code
      • Formatting
      • Error Handling
      • Concurrency
      • Testing
      • SOLID Principles
      • Classes
      • Objects and Data Structures
      • Variables
      • Functions
    • Code Smells
      • Long Function
      • Duplicate Code
      • Loops
      • Double Negative
      • Christmas Tree Code
      • Complex Condition
      • Primitive Obsession
      • Speculative Generality
      • God Class
      • Long Parameter List
  • Junior to Senior
    • AWS
      • Lambda
    • Session + Authentication
    • Redis
    • Kubernetes
      • Networking
      • Services
      • Deployment
      • Replica Set
      • YAML
      • pod-definition.yml
      • Kubectl
      • Pods
      • Fundamentals
    • Docker
      • Operating System - Extra
      • Dockerfile - Docker Image
      • Docker Storage
      • Docker Network
      • Docker Registry
      • Docker Command
      • Docker Compose
      • Docker Compose - Postgres
    • Security
      • Logging
      • HTTPS, Cross-Site-Scripting (XSS) and Cross-Site-Request-Forgery (CSRF)
      • 3rd Party Library
      • Injection
      • Code Secret, Secure Header, Access Control, Data Management, Authentication
    • CI/CD
    • SPA vs Server-Side Rendering
    • Performance
      • Optimized Code
      • Critical Render Path
      • Backend Optimization
      • Minimized Files and Images
      • Minimized Delivery
  • SECURITY
    • Encryption
    • SSH
  • Command
  • Cheatsheet
    • NPM
    • GIT
  • Writing Template
    • Guide
    • API
    • ChangeLog
    • FAQ
  • Linux
Powered by GitBook
On this page

Was this helpful?

  1. Junior to Senior
  2. Docker

Dockerfile - Docker Image

Why you need to create your own image:

  • because you cannot find a service you want to use as part of your application

  • team decision that application will be dockerized for ease of shipping and deployment

How to create your own image:

  • Write down the instructions for setting up your application in Dockerfile such as installing dependencies, where to copy the source code from and to, and what entry point of the application, etc

FROM node:8.11.1

WORKDIR /usr/src/smart-brain-api

COPY ./ ./

RUN npm install

CMD ["/bin/bash"]
ENTRYPOINT

FROM instruction defines what the base OS should be for this container. Every docker image must be based of another image either OS or image that was created before based on OS.

WORKDIR or working directory instruction is the directory in the container that we want to work out from.

COPY is to copy whatever we want from our current directory into the container.

COPY ./ ./ -- is to copy everything in root to root

COPY package.json ./ -- is to copy only package.json to root

RUN is what type of commands should we run in the container. RUN npm install is like how you git clone and you do npm install.

CMD or command instruction tells us what to run in the container and this is to access its profile.

Entrypoint specify a command that will be run when the image is run as container

What is the difference between CMD and EntryPoint?

How do you override CMD temporarily? by attaching command in the docker run. (i.e. docker run ubuntu sleep 5)

How do you make CMD change permanently? by changing the dockerfile and rebuilding it. You can still override it using the docker run (i.e. docker run ubuntu-sleeper sleep 10). But this is not good. Ideally we want to just pass the parameter (i.e. docker run ubuntu-sleeper 10). That is when Entry Point come in.

For example, ENTRYPOINT["sleep"]. Because Entry Point is appended, you have to specifiy the number (docker run ubuntu-sleeper 10) or you will get an error (operand is missing).

To configure default value, you use both Entry point and CMD. Entrypoint will be appended to the terminal and the CMD is the default value that can be overwritten. But you have to write the entrypoint and CMD in JSON format ["sleep"] and ["5"] respectively

How to modify entry point at run time? by running, docker run --entrypoint sleep2.0 ubuntu-sleeper 10

What's the difference between RUN and CMD?

RUN is what we call an image build step. The state of the container after a run command will be committed to the docker image. So a Dockerfile can run many run commands to build the image that we want. On the other hand, CMD is something that executed by default after you launch the build image. So a Dockerfile can have only one comment and that usually comes at the end of file.

Build Image

docker build -t ${your chosen name} .

// to build docker image locally.
// t = tag; which you can name whatever you like
// . means it is to build everything
PreviousOperating System - ExtraNextDocker Storage

Last updated 4 years ago

Was this helpful?