Project Icon

Fast-Docker

Docker容器化技术学习与实践指南

Fast-Docker是一个全面的Docker学习资源,涵盖从基础概念到高级应用。项目通过实践实验和命令指南,帮助开发者快速掌握Dockerfile、镜像、容器、Docker Compose、网络和Swarm等核心技术。内容适合各层级开发者,提供容器化解决方案的实际应用指导和最佳实践参考。

Fast-Docker

This repo aims to cover Docker details (Dockerfile, Image, Container, Commands, Volumes, Docker-Compose, Networks, Swarm, Stack) quickly, and possible example usage scenarios (HowTo: LABs) in a nutshell. Possible usage scenarios are aimed to update over time.

Keywords: Docker-Image, Dockerfile, Containerization, Docker-Compose, Docker-Volume, Docker-Network, Docker-Swarm, Service, Cheatsheet.

Quick Look (HowTo: LABs)

Table of Contents

Motivation

Why should we use Docker? "Docker changed the way applications used to build and ship. It has completely revolutionized the containerization world." (Ref:ItNext)

Needs

  • Installing all dependencies, setting up a new environment for SW (time-consuming every time to install environment for testing )
  • We want to run our apps on different platforms (Ubuntu, Windows, Raspberry Pi).
    • Question in our mind: What if, it does not run on a different OS?
  • CI/CD Integration Testing: We can handle unit testing, component testing with Jenkins. What if integration testing?
    • Extending Chain: Jenkins- Docker Image - Docker Container - Automatic testing
  • Are our SW products portable to carry on different PC easily? (especially in the development & testing phase)
  • Developing, testing, maintenance of code as one Monolithic App could be problematic when the app needs more features/services. It is required to convert one big monolithic app into microservices.

Benefits

  • NOT needed to install dependencies/SWs again & again
  • Enables to run on different OS, different platforms
  • Enables a consistent environment
  • Enables more efficient use of system resources
  • Easy to use and maintain
  • Efficient use of the system resources
  • Isolate SW components
  • Enables faster software delivery cycles
  • Containers give us instant application portability.
  • Enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container
  • Microservice Architecture (Monolithic Apps to MicroService Architecture, e.g. Cloud Native App)

(Ref: Infoworld)

Problems Docker does not solve

  • Docker does NOT fix your security issues
  • Docker does NOT turn applications magically into microservices
  • Docker isn’t a substitute for virtual machines

(Ref: Infoworld)

What is Docker?

  • Docker is a tool that reduces the gap between the Development/Deployment phase of a software development cycle.
  • Docker is like a VM but it has more features than VMs (no kernel, only small app and file systems, portable)
    • On Linux Kernel (2000s) two features are added (these features support Docker):
      • Namespaces: Isolate process.
      • Control Groups: Resource usage (CPU, Memory) isolation and limitation for each process.
  • Without Docker, each VM consumes 30% of resources (Memory, CPU)

image (Ref: Docker.com)

Architecture

image (Ref: docs.docker.com)

Installation

Docker Engine (Deamon, REST API, CLI)

  • There are mainly 3 components in the Docker Engine:
    • Server is the docker daemon named docker daemon. Creates and manages docker images, containers, networks, etc.
    • Rest API instructs docker daemon what to do.
    • Command Line Interface (CLI) is the client used to enter docker commands.

image (Ref: Docker.com)

Docker Registry and Docker Hub

image

App: Running Docker Free Local Registry, Tagging Container, Pushing to Local Registry, Pulling From Local Registry and Deleting Images from Local Registry

Docker Command Structure

  • docker [ManagementCommand] [Command]
docker container ls -a
docker image ls
docker volume ls
docker network ls
docker container rm -f [containerName or containerID]

image

image

Docker Container

image (Ref: docker-handbook-borosan)

  • When we create the container from the image, in every container, there is an application that is set to run by default app.
    • When this app runs, the container runs.
    • When this default app finishes/stops, the container stops.
  • There could be more than one app in docker image (such as: sh, ls, basic commands)
  • When the Docker container is started, it is allowed that a single application is configured to run automatically.
docker container run --name mywebserver -d -p 80:80 -v test:/usr/share/nginx/html nginx
docker container ls -a
docker image pull alpine
docker image push alpine
docker image build -t hello . (run this command where “Dockerfile” is)
(PS: image file name MUST be “Dockerfile”, no extension)
docker save -o hello.tar test/hello
docker load -i <path to docker image tar file>
docker load -i .\hello.tar

Goto: App: Creating First Docker Image and Container using Docker File

Docker Container: Life Cycle

image (Ref: life-cycle-medium)

e.g. [imageName]=alpine, busybox, nginx, ubuntu, etc.
docker image pull [imageName]
docker container run [imageName]
docker container start [containerId or containerName]
docker container stop [containerId or containerName]
docker container pause [containerId or containerName]
docker container unpause [containerId or containerName]

Docker Container: Union File System

  • Images are read only (R/O).
  • When containers are created, new read-write (R/W) thin layer is created.

image (Ref: docs.docker.com)

Docker Volumes: Why Volumes needed?

  • Containers do not save the changes/logs when erased if there is not any binding to volume/mount.
  • For persistence, volumes/mounts MUST be used.
  • e.g. Creating a log file in the container. When the container is deleted, the log file also deleted with the container. So volumes/binding mounts MUST be used to provide persistence!

image (Ref: udemy-course:adan-zye-docker)

Docker Volumes/Bind Mounts

  • Volumes and binding mounts must be used for saving logs, output files, and input files.
  • When volumes bind to the directory in the container, this directory and volume are synchronized.
docker volume create [volumeName]
docker volume create test
docker container run --name [containerName] -v [volumeName]:[pathInContainer] [imageName]
docker container run --name c1 -v test:/app alpine

Goto: App: Binding Volume to the Different Containers

Bind Mount

docker container run --name [containerName] -v [pathInHost]:[pathInContainer] [imageName]
docker container run --name c1 -v C:\test:/app alpine

image (Ref: Docker.com)

Goto: App: Binding Mount to Container Goto: App: Transferring Content between Host PC and Docker Container

Docker Network

  • Docker containers work like VMs.
  • Every Docker container has network connections
  • Docker Network Drivers:
    • None
    • Bridge
    • Host
    • Macvlan
    • Overlay

Docker Network: Bridge

  • Default Network Driver: Bridge (--net bridge)
docker network create [networkName]
docker network create bridge1
docker container run --name [containerName] --net [networkName] [imageName] 
docker container run --name c1 --net bridge1 alpine sh
docker network inspect bridge1
docker container run --name c2 --net bridge1 alpine sh
docker network connect bridge1 c2
docker network inspect bridge1
docker network disconnect bridge1 c2
  • Creating a new network using customized network parameters:
docker network create --driver=bridge --subnet=10.10.0.0/16 --ip-range=10.10.10.0/24 --gateway=10.10.10.10 newbridge

image (Ref: Docker.com)

Docker Network: Host

  • Containers reach host network interfaces (--net host)
docker container run --name [containerName] --net [networkName] [imageName] 
docker container run --name c1 --net host alpine sh

image (Ref: Docker.com)

Docker Network: MacVlan

  • Each Container has its own MAC interface (--net macvlan)

image (Ref: Docker.com)

Docker Network: Overlay

  • Containers that work on different PCs/hosts can work as the same network (--net overlay)

image (Ref: Docker.com)

Port Mapping/Publish:

  • Mapping Host PC's port to container port:
-p [hostPort]:[containerPort], --publish [hostPort]:[containerPort] e.g. -p 8080:80, -p 80:80
docker container run --name mywebserver -d -p 80:80 nginx

Docker Log

  • Docker Logs show /dev/stdout, /dev/stderror
docker logs --details [containerName]

image

Docker Stats/Memory-CPU Limitations

image

image

image

Docker Environment Variables

image

Docker File

image

Goto: [App: Creating Docker Container using Dockerfile to Build C++ on

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

白日梦AI

白日梦AI提供专注于AI视频生成的多样化功能,包括文生视频、动态画面和形象生成等,帮助用户快速上手,创造专业级内容。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

讯飞绘镜

讯飞绘镜是一个支持从创意到完整视频创作的智能平台,用户可以快速生成视频素材并创作独特的音乐视频和故事。平台提供多样化的主题和精选作品,帮助用户探索创意灵感。

Project Cover

讯飞文书

讯飞文书依托讯飞星火大模型,为文书写作者提供从素材筹备到稿件撰写及审稿的全程支持。通过录音智记和以稿写稿等功能,满足事务性工作的高频需求,帮助撰稿人节省精力,提高效率,优化工作与生活。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号