镜像加速地址
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.1panel.live"
]
}
daemon.json 配置文件里面
bip 配置项中可以配置docker 的网段
{
"graph": "/data/docker", #数据目录,此行可不加
"storage-driver": "overlay2", #存储引擎,此行可不加
"insecure-registries": ["registry.access.redhat.com","quary.io"], #私有仓库
"registry-mirrors": ["https://q"], #镜像加速,必加项
"bip": "172.7.5.1/16", #docker网络,必加项
"exec-opts": ["native.cgroupdriver=systemd"], #启动时候的额外参数(驱动),结合K8S使用
"live-restore": true #当docker容器引擎挂掉的时候,使用docker跑起来的容器还能运行(分离) ,必加项
}
配置tomcat镜像
如何缩小镜像体积
如何缩小镜像的体积大小?
1)尽可能的使用较小体积的基础镜像(推荐使用 alpine 镜像)
2)尽可能的减少Dockerfile中的指令数量,从而减少镜像的层数
3)在RUN指令结尾添加清空安装软件的缓存和软件包的命令
4)使用 .dockerignore 文件,添加不需要复制到镜像里的文件名或子目录名
5)容器镜像里一般只运行单个应用
6)使用多阶段构建的方法
FROM 第一构建阶段的基础镜像 as 阶段别名 #在第一阶段,安装依赖项并构建项目
....
FROM 第二构建阶段的基础镜像 #在第二阶段,复制上一阶段构建产物的文件或目录,并使用它来运行应用程序,这样最终的镜像中就不会有不必要的依赖和代码
COPY --from=第一构建阶段的别名 第一构建阶段的项目文件或目录 当前阶段的路径
....
CMD或ENTRYPOINT 指定容器启动命令
多级构建
Docker搭建LNMP与运行博客网站
1项目介绍
公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress
网站平台。然后对此服务进行相关的性能调优和管理工作
1配置nginx
nginx:172.111.0.10 docker-nginx
mysql:172.111.0.20 docker-mysql
php:172.111.0.30 docker-php
自定义网段:172.111.0.0/16
mkdir nginx mysql php
mv nginx-1.22.0.tar.gz wordpress-6.4.2-zh_CN.tar.gz /opt/nginx
mv mysql-boost-5.7.20.tar.gz /opt/mysql
vim /opt/nginx/nginx.conf
vim /opt/nginx/Dockerfile
#声明基础镜像
FROM centos:7
RUN yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
ADD nginx-1.22.0.tar.gz /usr/local/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.22.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
COPY nginx.conf /usr/local/nginx/conf/
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod -R 777 /usr/local/nginx/html
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
#创建自定义网段
docker network ls
#查看docker的自定义网段
docker network rm -f
#删除网络
ifconfig
#查看网络是否生成
docker build -t nginx:lnmp .
#创建镜像
docker run -itd --name nginx1 -p 80:80 -v /opt/nginx:/opt/nginxlogs --net mynetwork --ip 172.111.0.10 nginx:lnmp
#运行容器
curl 192.168.80.101
#测试nginx是否访问成功
2配置mysql
vim /opt/mysql/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
vim /opt/mysql/Dockerfile
FROM centos:7
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel && useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
COPY php.ini /usr/local/php/lib
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]
docker build -t mysql:lnmp .
docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp
docker exec -it mysql1 bash
cat etc/my.conf
mysql -u root -p
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
#创建系统用户wordpress可以从任意网段访问
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;
3配置php
vim /opt/php/Dockerfile
FROM centos:7
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel && useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
COPY php.ini /usr/local/php/lib/
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]
docker build -t php:lnmp
docker run -itd --name php -p 9000:9000 --volumes-from nginx1 --volumes-from mysql1 --net mynetwork --ip 172.111.0.30 php:lnmp
docker exec -it php bash
4运行博客测试