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
Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide informationUsage
Create Docker image
The ’-t’ option allows you to define the name of your image.
docker build -t python-test .Run the Docker Image
docker run python-testList your image
docker image lsDelete a specific image.
docker image rm [image name]Delete all existing images.
docker image rm $(docker images -a -q)List all existing containers (running and not running).
docker ps -aStop a specific container.
docker stop [container name]Stop all running containers.
docker stop $(docker ps -a -q)Delete a specific container (only if stopped).
docker rm [container name]Delete all containers (only if stopped).
docker rm $(docker ps -a -q)Display logs of a container.
docker logs [container name]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.
CMD ["executable", "parameter1", "parameter2"]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:
docker run executable parametersENTRYPOINT
- 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.