部署Docker

安装Docker

CentOS 7.9

  1. 配置yum源

    [root@ecs ~]# yum install -y yum-utils
    [root@ecs ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  2. 安装docker ce

    yum install -y docker-ce
  3. 启动docker并设置开机自启动

    [root@ecs ~]# systemctl enable --now docker
  4. 查看docker版本

    [root@ecs ~]# docker --version
    Docker version 26.1.4, build 5650f9b
  5. 安装docker compose

    [root@ecs ~]# curl -L "https://gh-proxy.com/https://github.com/docker/compose/releases/download/v2.29.7/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    [root@ecs ~]# chmod a+x /usr/local/bin/docker-compose
    [root@ecs ~]# docker-compose version
    Docker Compose version v2.29.7

Ubuntu

Docker要求内核版本不能低于3.10,且必须为64位系统。

  1. 查看Ubuntu内核版本

    root@ecs:~# uname -a
    Linux ecs 5.15.0-186-generic #196-Ubuntu SMP Sat Jun 20 16:09:34 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
  2. 更换软件源

    Ubuntu 22.4

    root@ecs:~# sed -i 's|http://[a-z0-9\.]*\.archive\.ubuntu\.com|https://mirrors.aliyun.com|g;s|http://security\.ubuntu\.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list
    root@ecs:~# apt update && apt upgrade && apt full-upgrade

    Ubuntu 24.4

    root@ecs:~# sed -i 's|http://[a-z0-9\.]*\.archive\.ubuntu\.com|https://mirrors.aliyun.com|g;s|http://security\.ubuntu\.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources
    root@ecs:~# apt update && apt upgrade && apt full-upgrade
  3. 安装Docker

    root@ecs:~# curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
    root@ecs:~# add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
    root@ecs:~# apt install docker-ce

    注:必须添加GPG key,否则会报错:

    W: GPG error: http://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8
    E: The repository 'http://mirrors.aliyun.com/docker-ce/linux/ubuntu jammy InRelease' is not signed.`。

    Ubuntu 26.04已从apt(2.9.17+)移除了apt-key,因此不再适用上面的方法,需要先使用gpg --dearmor下载并转换密钥,然后通过signed-by在软件源中指定该密钥。

    root@ecs:~# curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
    gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    root@ecs:~# echo "deb [signed-by=/etc/apt/keyrings/docker.gpg] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | \
    tee /etc/apt/sources.list.d/docker.list
    root@ecs:~# apt-get update && apt-get install docker-ce
  4. 启动docker

    root@ecs:~# systemctl start docker

镜像管理

  • 拉取镜像

    [root@ecs ~]# docker pull registry.cn-shanghai.aliyuncs.com/maisi/alpine:latest
  • 导入镜像

    [root@ecs ~]# docker load < php_8.2-fpm-amd64.tar
  • 设置标签(仓库地址)

    [root@ecs ~]# docker tag php:8.2-fpm registry.cn-shanghai.aliyuncs.com/maisi/php:8.2-fpm
  • 上传镜像

    [root@ecs ~]# docker login --username=hi30772550@aliyun.com registry.cn-shanghai.aliyuncs.com 
    [root@ecs ~]# docker push registry.cn-shanghai.aliyuncs.com/maisi/php:8.2-fpm
    [root@ecs ~]# docker rmi registry.cn-shanghai.aliyuncs.com/maisi/php:8.2-fpm
  • 加速镜像

    [root@ecs ~]# cat > /etc/docker/daemon.json << EOF
    {
        "registry-mirrors": [
            "http://mscr.o88.co",
            "https://registry.cn-shanghai.aliyuncs.com"
        ],
        "insecure-registries": [
            "mscr.o88.co"
        ]
    }
    EOF
    [root@ecs ~]# systemctl daemon-reload
    [root@ecs ~]# systemctl restart docker

    默认情况下,镜像地址必须使用https,否则会报错:Error response from daemon: Get "https://mscr.o88.co/v2/": http: server gave HTTP response to HTTPS client。将镜像地址加入insecure-registries列表则可以不使用https协议传输。

    之后便可以直接使用以下方式拉取镜像:

    [root@ecs ~]# docker pull maisi/php:8.2-fpm  

    验证镜像设置是否生效:

    [root@ecs ~]# docker info | grep -A 2 'Registry'
     Registry Mirrors:
      http://mscr.o88.co/
      https://registry.cn-shanghai.aliyuncs.com/
  • 缓存docker yum源

    [root@ecs ~]# yum install -y createrepo
    [root@ecs ~]# reposync -np /data
    [root@ecs ~]# createrepo -po /data/docker-ce-stable /data/docker-ce-stable

    createrepo -po <元数据输出目录> <RPM包目录>:创建yum软件仓库元数据。

应用

部署php

安装GD

  1. 编写Dockerfile

    FROM php:8.2-fpm
    
    ENV TZ=Asia/Shanghai
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
    && echo $TZ > /etc/timezone
    RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
    
    RUN apt-get update && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    && rm -rf /var/lib/apt/lists/*
    
    RUN docker-php-ext-configure gd \
    --with-freetype \
    --with-jpeg
    
    RUN docker-php-ext-install -j$(nproc) gd pdo_mysql mysqli
    • Debian 12废弃了/etc/apt/sources.list,需改成:/etc/apt/sources.list.d/debian.sources
    • GD(Graphics Draw)是php动态创建和处理图像的核心扩展库,强制依赖libpng-dev,安装libfreetype6-dev时会自动安装libpng-dev,如果没有安装libfreetype6-dev则必须手动安装libpng-dev
    • --with-freetype为GD启用freetype字体库支持,--with-jpeg为GD启用jpeg支持。
  2. 编译镜像

    [root@ecs ~]# docker build -t php:8.2-fpm-ext .

运行php

[root@ecs ~]# docker run --name php \
--privileged=true \
--restart=always \
-e TZ=Asia/Shanghai \
-v /data/wwwroot:/var/www \
-v /tmp:/tmp \
-p 9000:9000 \
-d php:8.2-fpm-ext

标签: none

添加新评论