Docker/Docker Compose

Docker/Docker Compose
  • Docker?
    • Docker is a set of PaaS products that deliver software in packages, which are isolated from one another and bundle their 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 env, isolated from each other, run anywhere, more granular control over resources, improved efficiency and better utilization of your compute resources.
  • 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 understanding(In OO programming):

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

Docker focuses more on the system level, which can be used to control the environment in the same version(dependencies).

  • users can download an image from Docker Hub, and then initialize it to an instance that has the same environment as an image.

  • this container can be reversed to an image(U can modify it) that will be passed to other users to share the same editing and environment. good version control and reproduced the results.

  • sudo service docker start 启动 docker 服务器

  • docker build -t <imageName> .

    • build an IMAGE from docker file
    • -t add tag name for this image
    • -f: 指定 dockerfile 路径
  • docker image rm <imageName>

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

    • run a CONTAINER based on an IMAGE in the front of the terminal
  • docker run -d —rm -p 80:5000 —name <containerName> <imageName>

    • run a CONTAINER based on an IMAGE and run it in the background, and clean itself after closing this CONTAINER
    • -p: port mapping local: docker
    • -d: detach, run the container in the background and print container ID
    • —rm: rm, if u close this container
    • —name: container name
  • docker stop <containerName>

    • 正常关机,可 resume
  • docker kill <containerName>

    • 拔电源
  • docker ps -a

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

    • restart a container

Dockerfile: text docs that can assemble an image, which can create an automated build

  • FROM: initialize a new build stage with base IMAGE (from the local repo or public repo)
  • ENV: key - value pair
  • WORKDIR: working directory for any [RUN, CMD, ENTRYPOINT, COPY and ADD instructions],指定登录终端后的落脚点
  • COPY: COPY <src> <dest>
  • ADD: 相当于 COPY + 自动处理 url 和 解压 tar 压缩包
  • RUN: run commands in the current image (Linux) (其中,有<命令行命令> 和 <exec 格式> with String arr[’’])
  • EXPOSE: the container expose a specific port to listen
  • ENTRYPOINT: 不会被 docker run 后面的命令覆盖(CMD 再给 entrypoint 传参)→ <ENTRYPOINT> <CMD>
  • CMD:指定容器启动后要做的事情;在指定了 entrypoint 指令后,用 CMD 指定具体的参数;虽支持多个指令,但仅最后一个生效(会被 docker run 之后的参数 override,相当于 默认值)

PS: CMD 在 docker run 时运行, RUN 在 docker build 时运行

  • docker exec -it <containerName> /bin/bash
    • 在 container 中操作已经部署的系统
    • -it: run the command in the container with bash, keep interactive
  • docker container rm -f $(docker ps -aq)’
    • delete all the docker container
    • -q → quiet: only display container ID
    • -a → all
    • -f → force
  • docker ps -q | xargs docker kill
    • rm all the containers
    • -q: 只显示容器编号
    • -a all
    • xargs: get params from pre pipeline processing
  • docker images -qa | xargs docker rmi -f
    • list all the images with only 容器编号,并强制删除
  • docker system prune
    • 清理不常用的数据 (-a, -f 有更深度清理,请移步文档)
  • 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 可以视为版本号,不填写默认为 latest

Docker-compose.yml:对 docker 容器集群的快速编排 - service: 一个个应用容器实例 eg. 订单微服务,库存微服务,mysql 容器 (一个应用的容器,可以包括若干运行相同的容器实例。每个服务都有自己的名字,使用镜像,依赖 XXX) - project: 有一组关联的容器组成的一个完整业务单元。1 project = 多个服务(容器应用实例) - 先写好 dockerfile,定义各个微服务并构建出对应的镜像 - 用 docker-compose 定义一个完整的 project,安排好整体应用中的各个容器,顺序,交互 - docker-compose up 一键部署

  • docker-compose build: build all the docker files
  • docker-compose up: run all the containers with dependencies
  • docker-compose down: stop

https://www.cnblogs.com/hsyw/p/13198448.html#:~:text=Compose 项目是 Docker 官方,可以启动多个容器!

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
  • dockerfile 中 volume
;