Docker - Basics
Basic Concept
What is Docker?
- A tool to create and manage containers
What is Container?
- A independent and standardized unit of software.
- A application package of code and its dependencies.
- Example: NodeJs code and its runtime, same version.
- Encapsulate App code and environment.
- Same container always yield the exact same application and execution behaviour
- Good for distribution and rebuilding
Why Docker instead of Virtual Machine (VM)
- Virtual machines has an OS (Windows, Mac or Linux)
- Is like a standalone computer. Machine
- Using VM is wasting alot space and thus high overheads
- Duplicated OS, redudant
What is Image?
Common Commands
Usage
Create Docker image
The ’-t’ option allows you to define the name of your image.
Run the Docker Image
List your image
Delete a specific image.
Delete all existing images.
List all existing containers (running and not running).
Stop a specific container.
Stop all running containers.
Delete a specific container (only if stopped).
Delete all containers (only if stopped).
Display logs of a container.
CMD vs ENTRYPOINT
The CMD command specifies the instruction that is to be executed when a Docker container starts.
- This CMD command is not really necessary for the container to work, as the echo command can be called in a RUN statement as well.
- The main purpose of the CMD command is to launch the software required in a container.
- For example, the user may need to run an executable .exe file or a Bash terminal as soon as the container starts
- Then the CMD command can be used to handle such requests.
One CMD in a Dockerfile
In principle, there should only be one CMD command in your Dockerfile. When CMD is used multiple times, only the last instance is executed.
Overidding CMD
A CMD command can be overridden by providing the executable and its parameters in the docker run command. For example:
ENTRYPOINT
- ENTRYPOINT cannot be overridden by docker run.
- Instead, whatever is specified in docker run will be appended to ENTRYPOINT – this is not the case with CMD.