Linux服务器安装docker
- 查看Linux服务器内核版本(docker要求CentOS系统内核版本高于3.10)
- 确保yum包更新到最新
- 卸载旧版本docker(如果已经安装过旧版本的话)
1
| yum remove docker docker-common docker-selinux docker-engine
|
- 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
1
| sudo yum install -y yum-utils device-mapper-persistent-data lvm2
|
- 设置阿里云yum源
1
| sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
|
- 安装docker
1
| yum install docker-ce docker-ce-cli containerd.io
|
如果想安装指定版本的docker-ce,可以先用命令查看版本号:
1
| sudo yum list docker-ce --showduplicates | sort -r
|
通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。如:
yum install docker-ce- docker-ce-cli- containerd.io
sudo yum install -y docker-ce-18.09.9 docker-ce-cli-18.09.9 containerd.io
yum install -y docker-ce-19.03.11 docker-ce-cli-19.03.11 containerd.io
- 启动Docker并设置开机启动
1 2
| systemctl start docker systemctl enable docker
|
- 验证是否安装成功
php容器中安装扩展
一般由仓库下在的容器都是最简版本,所有东西都要自己去取容器内部安装。如下就是案例如何进行容器内安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| docker exec -it 5849efd915e3(容器ID) /bin/bash
docker-php-source extract
cd /usr/src/php/ext/
docker-php-ext-install bcmath
docker-php-ext-install mysqli
apt update apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev cd /usr/src/php/ext/gd docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2 docker-php-ext-install gd
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev rm -rf /var/lib/apt/lists/* docker-php-ext-configure gd --with-freetype --with-webp --with-jpeg docker-php-ext-install gd
apt-get install -y libjpeg-dev rm -rf /var/lib/apt/lists/* docker-php-ext-configure gd --with-jpeg docker-php-ext-install gd
apt-get install -y zlib1g-dev && apt-get install -y libzip-dev docker-php-ext-install
pecl install redis
|
该容器安装了多个扩展,但不能每次都要安装容器再安装一大推扩展这与快速开发和使用docker初终不符。
所以我们要安装好扩展好的容器打包成镜像,打包如下:
1 2 3 4 5 6
| docker commit -a "作者名称" -m "描述" 容器id 名称:标签
docker login docker push 名称:标签
|
使用 docker-compose 安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| version: '3' networks: luocheng: driver: bridge services: nginx: container_name: luocheng-nginx image: nginx:1.23.1 #restart: always depends_on: #设置依赖关系。 - "php" volumes: - ../../basic:/basic - ./config/nginx/conf.d:/etc/nginx/conf.d - ../log/nginx:/logs networks: - luocheng ports: - "80:80" php: container_name: luocheng-php image: 963523556/php:7.2.8-fpm #restart: always working_dir: /basic volumes: - ../../basic:/basic networks: - luocheng ports: - "9000:9000" #user: "1000:1000" redis: # 自定义容器名 container_name: luocheng-redis # 镜像及版本 image: redis:7.0.4 # docker启动时,自动启动该容器 #restart: always # 挂载映射,可以让数据或配置持久化 volumes: # <本地配置文件> : <docker中的配置文件> : <ro:docker容器对该文件只读,默认是rw可读可写> - ../data/redis:/data - ./config/redis/redis.conf:/usr/local/etc/redis.conf #- ./log/redis/:/logs # docker执行的启动命令 command: redis-server /usr/local/etc/redis.conf #配置容器连接的网络,引用顶级 networks 下的条目 。 networks: - luocheng ports: # <本地端口> : <docker容器端口> - "6379:6379" mysql: container_name: luocheng-mysql image: mysql:5.7.29 #X86架构 #image: amd64/mysql:5.7.29 #arm架构 #restart: always #添加环境变量。您可以使用数组或字典、任何布尔值,布尔值需要用引号引起来,以确保 YML 解析器不会将其转换为 True 或 False。 environment: MYSQL_ROOT_PASSWORD: xxxxx volumes: - ../data/mysql5.7.29/lib:/var/lib/mysql - ./config/mysql/mysqld.cnf:/etc/mysql/my.cnf networks: - luocheng ports: - "3306:3306"
|
mysqld.cnf
1 2 3 4 5 6 7 8 9 10
| [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql log-error = /var/log/mysql/error.log # By default we only accept connections from localhost #bind-address = 127.0.0.1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 max_connections=1000
|
docker-compose.yml 如何安装?
1
| docker-compose -p xxx up -d 安装镜像
|
返回 docker 系列
php容器中安装扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| apt-get update && \ apt-get install -y --no-install-recommends libbz2-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) bz2
apt-get update && \ apt-get install -y --no-install-recommends libenchant-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) enchant
apt-get update && \ apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \ docker-php-ext-install -j$(nproc) gd
apt-get update && \ apt-get install -y --no-install-recommends libgmp-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) gmp
apt-get update && \ apt-get install -y --no-install-recommends libxml2-dev libtidy-dev libxslt1-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) soap wddx xmlrpc tidy xsl
apt-get update && \ apt-get install -y --no-install-recommends libzip-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) zip
apt-get update && \ apt-get install -y --no-install-recommends libsnmp-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) snmp
apt-get update && \ apt-get install -y --no-install-recommends libpq-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) pgsql pdo_pgsql
apt-get update && \ apt-get install -y --no-install-recommends libpspell-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) pspell
apt-get update && \ apt-get install -y --no-install-recommends librecode-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) recode
apt-get update && \ apt-get install -y --no-install-recommends firebird-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) pdo_firebird
apt-get update && \ apt-get install -y --no-install-recommends freetds-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-configure pdo_dblib --with-libdir=lib/x86_64-linux-gnu && \ docker-php-ext-install -j$(nproc) pdo_dblib
apt-get update && \ apt-get install -y --no-install-recommends libldap2-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu && \ docker-php-ext-install -j$(nproc) ldap
apt-get update && \ apt-get install -y --no-install-recommends libc-client-dev libkrb5-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \ docker-php-ext-install -j$(nproc) imap
apt-get update && \ apt-get install -y --no-install-recommends firebird-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) interbase
apt-get update && \ apt-get install -y --no-install-recommends libicu-dev && \ rm -r /var/lib/apt/lists/* && \ docker-php-ext-install -j$(nproc) intl
apt-get update && \ apt-get install -y --no-install-recommends libmcrypt-dev && \ rm -r /var/lib/apt/lists/* && \ pecl install mcrypt-1.0.1 && \ docker-php-ext-enable mcrypt
export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" && \ apt-get update && \ apt-get install -y --no-install-recommends libmagickwand-dev && \ rm -rf /var/lib/apt/lists/* && \ pecl install imagick-3.4.3 && \ docker-php-ext-enable imagick
apt-get update && \ apt-get install -y --no-install-recommends zlib1g-dev libmemcached-dev && \ rm -r /var/lib/apt/lists/* && \ pecl install memcached && \ docker-php-ext-enable memcached
pecl install redis && docker-php-ext-enable redis
docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache
|