安装源ISO CentOS-7-x86_64-DVD-1810
最小化安装系统后先更新
[root@Server ~]# yum update -y
[root@Server ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
[root@Server ~]# uname -r 3.10.0-957.21.3.el7.x86_64
关闭防火墙 systemctl stop firewalld.service
关闭防火墙开机启动 systemctl disable firewalld.service
关闭SELINUX (重启生效) sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
更换阿里源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
生成cache yum makecache
docker安装 yum install -y docker
启动docker systemctl start docker
加入开机启动 systemctl enable docker
查看运行状态 systemctl status docker
查看docker版本 [root@localhost ~]# docker -v Docker version 1.13.1, build 64e9980/1.13.1
镜像加速器 解决docker hub访问慢的问题,提升下载速度,推荐阿里云镜像加速地址 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["加速地址"] } EOF
重启服务 sudo systemctl daemon-reload sudo systemctl restart docker
安装docker 可视化web管理工具portainer [root@localhost ~]# docker pull docker.io/portainer/portainer
运行单机版 通过9000端口访问 docker run -d -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true --name portainer docker.io/portainer/portainer
验证: 浏览访问 http://ip:9000 初次访问,设置管理员密码,选择local即可
docker 常用命令
强制删除本地镜像 docker rmi -f [REPOSITORY:TAG] 或 [IMAGE ID]
停止容器 & 删除容器 docker stop $(docker ps -q) & docker rm $(docker ps -aq)
设置容器为自启动 docker container update --restart=always [容器id1] [容器id2]
取消容器自启动 单个[容器ID] 全部 [ $(docker ps -q)] docker update --restart=no $(docker ps -q)
从容器创建新镜像 docker commit -a "作者名" -m "修改描述" 容器ID [REPOSITORY[:TAG]
docker快速搭建LNMP环境
拉镜像 docker pull nginx docker pull php:7.1-fpm docker pull mariadb docker pull phpmyadmin docker pull redis
创建文件夹 mkdir -p nginx/conf.d /nginx/www /nginx/mysql
编写default.conf [root@localpc conf.d]# cat /nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
启动php docker run --name myphp -d -v /nginx/www:/var/www/html docker.io/php:7.1-fpm
关联启动 nginx docker run --name mynginx -p 80:80 -d -v /nginx/www:/usr/share/nginx/html -v /nginx/conf.d:/etc/nginx/conf.d:ro --link myphp:php docker.io/nginx
启动mariadb docker run --name mydb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin123 -v /nginx/mysql:/var/lib/mysql -d --link myphp:php docker.io/mariadb:10.5.6
启动phpmyadmin docker run --name myadmin -d -p 81:80 --link mydb:db docker.io/phpmyadmin
启动redis docker run --name myredis -p 6379:6379 -d docker.io/redis:latest
php 安装mysqli pdo_mysql扩展 docker-php-ext-install pdo pdo_mysql docker-php-ext-install mysqli
php安装gd扩展 apt update apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev docker-php-source extract 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
php安装redis扩展 curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.3.tar.gz cd /tmp tar xfz /tmp/redis.tar.gz mv phpredis-3.1.3 /usr/src/php/ext/redis docker-php-ext-install redis