Docker Study Notes

Docker is a Linux-based container virtualization technology. Its goal is “Build, Ship and Run Any App, Anywhere”—to build, ship, and run any application anywhere. It is one of the key technologies in cloud-native architectures.

Contents

Docker

Introduction to Docker

Docker is an open-source cloud project implemented in Go.

Docker’s main goal is “Build, Ship and Run Any App, Anywhere.” In other words, by managing the entire lifecycle of application components—including packaging, distribution, deployment, and execution—users can ensure that their apps (whether web applications, database applications, or otherwise) and their runtime environments can “run anywhere from a single image.”

The emergence of Linux container technology solved this problem, and Docker developed from that foundation. Applications are packaged into images, which are used to create instances that run in Docker containers. Docker containers behave consistently across operating systems, enabling cross-platform and cross-server deployment. Once the environment is configured, deploying it on another machine takes just one click, greatly simplifying operations.

Software containers that solve runtime environment and configuration problems, making continuous integration easier and facilitating unified releases through container virtualization technology.

Docker’s Basic Components

  • Image: We package an application and its configuration dependencies into a deliverable runtime environment. This packaged runtime environment is an image file.
  • Container: A container is a running instance created from an image. It is the runtime entity of an image. A container provides the image with a standard, isolated runtime environment. It can be started, stopped, and deleted. Each container is isolated from the others, ensuring a secure platform.
  • Repository: A place where image files are centrally stored. Repositories come in two forms: public and private. The largest public repository is Docker Hub, which stores a vast number of images for users to download. Public repositories in China include Alibaba Cloud and NetEase Cloud.

The earlier notes got lost, so I’ll just make something up.

The earlier notes got lost, so I’ll just make something up.

The earlier notes got lost, so I’ll just make something up.

The main content starts here ↓

Installing Docker

https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.3e221b11MyHd3k

# 一键安装
bash <(curl -sSL https://linuxmirrors.cn/docker.sh)
# 官方
wget http://get.docker.com -o docker_install.sh

Common Docker Commands

pull

docker pull redis
Pull an image from a remote repository to the local machine

run

docker run hello-world
docker run -it -name ubuntu-container ubuntu:latest /bin/bash
Start a container from an image. Images are searched for in this order: local -> remote repository

Parameters

  • --name Specify the container name
  • -d Run in the background and return the container ID
  • -i Run the container in interactive mode, usually used together with -t
  • -t Allocate a pseudo-TTY for the container, that is, start an interactive container
  • -p Host port:container port Port mapping
  • -P Random port mapping

exec

docker exec -it [id | container name] [command]
Run an additional command in the container. The container will not exit when the command finishes; this is equivalent to starting a new thread

attach

docker attach ubuntu
Attach to the terminal of a running container. If you run a command that ends the session inside the container, the container will also exit

logs

-f Follow the log stream and continuously display new entries

View log records generated inside the container

system df

Used to view system resources occupied by Docker containers, images, and so on, similar to df -h in Linux

images

View the image list

Related commands

  • rmi Remove an image

ps

View the container status list. By default, only running containers are shown

Parameters

  • -q Return IDs only
  • -a View all containers

cp

docker cp container-id:container-path host-path

Copy files from a container to the host machine

export

docker export container-id > file-name

[ save ]

Package a container's state into an image archive file

import

docker import file author/image-name:tag

[ load ]

Import a packaged image file into the local image list

Key Concepts

Dangling images: Images with no name or tag, only an ID and size. They are generally created when an image build encounters a problem; when you find them, it is generally best to delete them

Docker Layered File System

Docker is lightweight because it uses a union file system (union fs). Its greatest feature is that files are stored in layers.

A container roughly consists of bootfs > rootfs > software...

bootfs is shared, so image files do not need to include it, greatly reducing image size.

The layered file system also allows each layer to be reused. This means the same layer only needs to be downloaded once and can be reused multiple times, saving local storage space and making images more lightweight.

Images are read-only, while containers are read-write. When a container is exported as an image, all operations on the container are converted into a new read-only "operation record" layer added to the original image. And so on, layer upon layer.

<img src="/api/file/dbe7a52130454012a7a6ef21d584f0d3.png" alt="image-20230806110515044" style="zoom: 50%;" />

Alibaba Cloud Remote Repository

1. Log in to the Alibaba Cloud Docker Registry

$ docker login --username=wds824 registry.cn-hangzhou.aliyuncs.com

2. Pulling an Image from the Registry

$ docker pull registry.cn-hangzhou.aliyuncs.com/wds824/myubuntu:[镜像版本号]

3. Push the Image to the Registry

$ docker login --username=wds824 registry.cn-hangzhou.aliyuncs.com$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/wds824/myubuntu:[镜像版本号]$ docker push registry.cn-hangzhou.aliyuncs.com/wds824/myubuntu:[镜像版本号]

Docker Private Registry

  • Install the private registry
 docker run -d -p 5000:5000 -v /temp/myregistry/:/tmp/registry --privileged=true registry 
  • Use the private registry

Before using it, configure Docker to trust the private registry (not required if using HTTPS).

Edit /etc/docker/deamon.conf

{
  "registry-mirrors": ["https://7n97q08r.mirror.aliyuncs.com"],
   //信任的服务器
  "insecure-registries":["192.168.6.101:5000"] 
}

Docker Data Volume

-v /root/app:/usr/local/app:rw --privilege=true

-v host path: mount path: permissions (default is rw, read and write; optional ro --read only) Use privileged mode (the host file system may prevent container operations for security reasons; use this parameter to allow them)

Mount a directory from the host inside the container to share files.

The mounted directory is separate from the container’s union file system, so it will not be packaged into the image, and the same directory can be mounted into multiple containers at the same time.

  • Volume inheritance

--volumes-from another container

Inherit (copy) the mount paths from another container

Command Note

MySQL Database:

Remember to change the data volume container name to prevent conflicts when creating it again.

Version 8: Uses the utf8mb4 character set by default. On Linux, table names are case-sensitive by default.

Version 5.7: Uses the Latin1 character set by default and requires manual changes.

⚠️ When allowing public internet access, be sure to avoid weak passwords and default ports to prevent brute-force attacks.

# 8  
docker run --name mysql8 \
--privileged=true \
--restart always \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-v mysql-log:/var/log/mysql \
-v mysql-data:/var/lib/mysql \
-v myql-conf:/etc/mysql/conf.d \
-d mysql:8 \
--lower_case_table_names=1 

# 5.7
docker run --name mysql57 \
  --privileged=true \
  --restart always \
  -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -e TZ=Asia/Shanghai \
  -v /etc/localtime:/etc/localtime:ro \
  -v /storage/docker/data/mysql/log:/var/log/mysql \
  -v /storage/docker/data/mysql/data:/var/lib/mysql \
  -v /storage/docker/data/mysql/conf:/etc/mysql/conf.d \
  -d mysql:5.7 \
  --character-set-server=utf8mb4 \
  --collation-server=utf8mb4_unicode_ci

Modify the character set through the cnf configuration file

  • my.cnf example
[client]
default_character_set=utf8mb4

[mysqld]
collation_server=utf8mb4_general_ci
character_set_server=utf8mb4
  • View encoding
show variables like 'character%'

Encoding Comparison

Encoding What can it store? Emoji Recommended?
latin1 Western European text only (English, French, etc.) ❌ Not supported ⚠️ Almost never used
utf8 Most text, but not all Partially supported (legacy) ❌ Not recommended
utf8mb4 All text (all languages) ✅ Fully supported ? Strongly recommended

Redis Cache:

⚠️ Redis is accessible from the public internet. Set a password, or attackers could exploit your server.

# 有配置文件
docker run \
--name redis \
-v redis-conf:/usr/local/etc/redis \
-v redis-data:/data \
--privileged=true \
--restart always  \
-p 6379:6379  \
-itd redis \
redis-server /usr/local/etc/redis/redis.conf

# 无配置文件
docker run \
--name redis \
-v redis-data:/data \
--privileged=true \
--restart always  \
-p 6379:6379  \
-itd redis \

# 测试
docker exec -it redis redis-cli 

RabbitMQ

docker run \
 -e RABBITMQ_DEFAULT_USER=account \
 -e RABBITMQ_DEFAULT_PASS=password \
 --name mq \
 --hostname mq1 \
 -p 15672:15672 \
 -p 5672:5672 \
 -d rabbitmq:3.13.0-management

ElasticSearch

docker network create  es-net

docker run -d --name es --net es-net --privileged -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v es-data:/usr/share/elasticsearch/data -v es-config:/usr/share/elasticsearch/config -v es-plugins:/usr/share/elasticsearch/plugins elasticsearch:7.12.1 
# conf中的jvm配置,可以抑制内存占用
docker run -d --name kibana -v kibana-conf:/usr/share/kibana/config --net es-net -p 5601:5601 kibana:7.12.1
# 在配置文件中修改es的地址

# 安装ik 分词插件
elasticsearch-plugin install https://github.com/infinilabs/analysis-ik/releases/download/v7.12.1/elasticsearch-analysis-ik-7.12.1.zip
# 重启容器
docker restart es

MC-Server

Minecraft Server

docker volume create mc
docker run 
    -itd \
    -e EULA=TRUE \
    -e VERSION=1.20.4 \
    -e ONLINE_MODE=false \
    -e ALLOW_CHEATS=true \
    -v mc:/data \
    -p 19132:19132/udp \
    -p 25565:25565 \
    --name mc itzg/minecraft-server

RustDesk

Remote desktop relay node

The key is visible at startup

# docker-compose.yml

networks:
  rustdesk-net:
    external: false

services:
  hbbs:
    container_name: hbbs
    ports:
      - 21115:21115
      - 21116:21116 # 自定义 hbbs 映射端口
      - 21116:21116/udp # 自定义 hbbs 映射端口
    image: rustdesk/rustdesk-server
    command: hbbs 
    volumes:
      - /root/rustDesk/nbbs:/root # 自定义挂载目录
    networks:
      - rustdesk-net
    depends_on:
      - hbbr
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 64M 

  hbbr:
    container_name: hbbr
    ports:
      - 21117:21117 # 自定义 hbbr 映射端口
    image: rustdesk/rustdesk-server
    command: hbbr
    volumes:
      - /root/rustDesk/hbbr:/root # 自定义挂载目录
    networks:
      - rustdesk-net
    restart: unless-stopped
    deploy:
      resources:
        limits:
          memory: 64M 

Minio

File storage server

docker stop minio && docker rm minio

docker run \
    -itd \
    --restart always \
    --name minio \
    -p 9000:9000 \
    -p 9001:9001 \
    -v /root/minio/data:/data \
    -e "MINIO_ROOT_USER=account" \
    -e "MINIO_ROOT_PASSWORD=password" \
    minio/minio \
    server /data --console-address ":9001"