← all posts

Docker/Docker Compose

October 26, 2022
Docker/Docker Compose
  • Docker:
    • Docker is a set of platform-as-a-service products that deliver software in isolated packages. Each package bundles its own software, libraries, and configuration files.
  • Why not a virtual machine?
    • Unlike virtual machines, containers do not have a high overhead and hence enable more efficient usage of the underlying system and resources.
  • Container:
    • Containers offer a logical packaging mechanism in which applications can be abstracted from the environment in which they actually run. This decoupling allows container-based applications to be deployed easily and consistently, regardless of whether the target environment is a private data centre, the public cloud, or even a developer's personal laptop.
    • Benefits: predictable environments, isolation between applications, portability, more granular resource control, improved efficiency, and better compute utilization.
  • Docker Image: The basis of a docker container. Represents a full application.
  • Docker Container: The standard unit in which the application service resides and executes.
  • Docker Engine: Creates, ships and runs Docker containers deployable on a physical or virtual, host locally, in a data centre or cloud service provider.
  • Repo (Docker Hub(Public) or Docker Trusted private): Cloud or server-based storage.

My mental model from object-oriented programming:

  • Java class == image
  • new instances == container
  • GitHub == Docker Hub (repository)

Docker works at the system level and helps keep runtime environments and dependency versions consistent.

  • Users can download an image from Docker Hub and start a container with the same environment defined by that image.

  • A modified container can be committed back into an image and shared with other users, which improves version control and reproducibility.

  • sudo service docker start — start the Docker service

  • docker build -t <imageName> .

    • Build an image from a Dockerfile.
    • -t: add a tag name for the image.
    • -f: specify the Dockerfile path.
  • docker image rm <imageName>

    • delete an IMAGE from the local image store
  • docker run -p 80:5000 —name <containerName> <imageName>

    • Run a container from an image in the foreground.
  • docker run -d —rm -p 80:5000 —name <containerName> <imageName>

    • Run a container from an image in the background and remove it automatically when it stops.
    • -p: map a local port to a container port.
    • -d: detach, run the container in the background, and print the container ID.
    • —rm: remove the container when it stops.
    • —name: set the container name.
  • docker stop <containerName>

    • graceful shutdown, can be resumed
  • docker kill <containerName>

    • force kill (like pulling the power cord)
  • docker ps -a

    • list all the running containers including stopped containers
  • docker restart <containerName> or <containerID>

    • restart a container

Dockerfile: a text document that defines how to assemble an image through an automated build.

  • FROM: initialize a new build stage with base IMAGE (from the local repository or public repository)
  • ENV: key - value pair
  • WORKDIR: working directory for any [RUN, CMD, ENTRYPOINT, COPY and ADD instructions] — sets the landing directory after logging into the terminal
  • COPY: COPY <src> <dest>
  • ADD: equivalent to COPY + automatically handles URLs and extracts tar archives
  • RUN: run commands in the current image (Linux) — supports both shell command format and exec format with String array
  • EXPOSE: the container expose a specific port to listen
  • ENTRYPOINT: cannot be overridden by commands after docker run (CMD provides additional arguments to ENTRYPOINT) → <ENTRYPOINT> <CMD>
  • CMD: specifies what to do after the container starts; when ENTRYPOINT is specified, CMD provides specific parameters; supports multiple instructions but only the last one takes effect (can be overridden by arguments after docker run, acts as default value)

PS: CMD runs at docker run time, RUN runs at docker build time

  • docker exec -it <containerName> /bin/bash
    • operate inside an already-deployed container
    • -it: run the command in the container with bash, keep interactive
  • docker container rm -f $(docker ps -aq)'
    • Delete all Docker containers.
    • -q → quiet: only display container ID
    • -a → all
    • -f → force
  • docker ps -q | xargs docker kill
    • kill all running containers
    • -q: only show container IDs
    • -a all
    • xargs: pass piped output as command arguments.
  • docker images -qa | xargs docker rmi -f
    • list all images with only container IDs and force delete them
  • docker system prune
    • clean up unused data (-a, -f for deeper cleanup, see docs)
  • docker inspect demo
    • inspect the container
  • docker cp your_file demo:/
    • copy a file into the container
  • docker <command> your_docker_account_name/image_name:<tag>
    • tag can be treated as a version number; defaults to latest if not specified

docker-compose.yml: a file for quickly orchestrating groups of Docker containers.

  • service: individual application container instances, e.g. order microservice, inventory microservice, mysql container (one application container can include multiple running instances of the same container; each service has its own name, uses an image, and depends on other services)

  • project: a complete business unit composed of a group of related containers. 1 project = multiple services (container application instances)

  • Write the Dockerfile first, define each microservice and build the corresponding images

  • Use docker-compose to define a complete project, arranging all containers in the application, their order, and interactions

  • docker-compose up deploys everything in one command

  • docker-compose build: build all the Docker files

  • docker-compose up: run all the containers with dependencies

  • docker-compose down: stop

DevOps Docker Volume

  • docker build -f Dockerfile-check -t
  • docker volume create sortlog-back
  • docker volume inspect sortlog-back
  • docker run -d -p 4000:4000 -v "$(pwd)/docker-output:/app/node_modules" vol_try
  • docker run -d -p 4000:4000 -v sortlog-back:/app/node_modules vol_try
  • volume definition in Dockerfile
;