基于CentOS7创建Docker镜像,Redis、NGINX、PHP容器。(如果创建失败,重启虚拟机重新运行就可以)
一、Docker安装
1. 卸载老版本
yum -y remove docker docker-common docker-selinux docker-engine
2. 安装需要的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
3. 设置国内yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4. 查看docker版本
yum list docker-ce --showduplicates|sort -r
5. 安装
yum install docker-ce-18.03.1.ce -y
6. 配置docker镜像源
vi /etc/docker/daemon.json
{"registry-mirrors": ["http://hub-mirror.c.163.com"]}
7.启动
systemctl start docker
8.加入开机自启
systemctl enable docker
二、生成镜像
1、生成个centos 7镜像试试
[root@localhost ~]# docker pull centos:centos7
centos7: Pulling from library/centos
524b0c1e57f8: Pull complete
Digest: sha256:e9ce0b76f29f942502facd849f3e468232492b259b9d9f076f71b392293f1582
Status: Downloaded newer image for centos:centos7
使用命令 docker images 查看
2、通过docker构建nginx+php+redis环境(用Dockerfile生成)
(1)创建Dockerfile文件
Redis:
FROM centos:centos7
RUN groupadd -r redis && useradd -r -g redis redis
RUN mkdir data ;\ yum update -y ; \ yum -y install gcc automake autoconf libtool make wget epel-release gcc-c++;
COPY ./redis-5.0.7.tar.gz redis-5.0.7.tar.gz
RUN mkdir -p /usr/src/redis; \
tar -zxvf redis-5.0.7.tar.gz -C /usr/src/redis; \
rm -rf redis-5.0.7.tar.gz; \
cd /usr/src/redis/redis-5.0.7 && make ; \
cd /usr/src/redis/redis-5.0.7 && make install
COPY ./conf/redis.conf /usr/src/redis/redis-5.0.7/redis.conf
EXPOSE 6379
ENTRYPOINT ["redis-server", "/usr/src/redis/redis-5.0.7/redis.conf"]
NGINX:
FROM centos:centos7
RUN groupadd -r nginx && useradd -r -g nginx nginx
#添加centos源(先下载wget)
COPY ./epel-7.repo /etc/yum.repos.d/epel.repo
RUN mkdir /data \
&& mkdir /conf \
&& yum update -y \
&& yum clean all \
&& yum makecache \
&& yum -y install gcc gcc-c++ autoconf automake make zlib zlib-devel net-tools openssl* pcre* wget \
&& yum clean all && rm -rf /var/cache/yum/*
#声明匿名卷
VOLUME /data
COPY ./nginx-1.14.1.tar.gz /data/nginx-1.14.1.tar.gz
RUN cd /data \
&& tar -zxvf nginx-1.14.1.tar.gz \
&& cd nginx-1.14.1 \
&& ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
&& make && make install && rm -rf /data/nginx-1.14.1.tar.gz
&& rm -rf /data/nginx-1.14. \ && ln -s /usr/local/nginx/sbin/* /usr/local/sbin
COPY ./conf/nginx.conf /conf
#进入容器时默认打开的目录
WORKDIR /conf
#声明端口
EXPOSE 80
#容器启动的时候执行,在docker run过程当中是会被其他指令替代
#CMD ["/usr/local/nginx/sbin/nginx","-c","/conf/nginx.conf","-g","daemon off;"]
#执行一条指
ENTRYPOINT ["/usr/local/nginx/sbin/nginx","-c","/conf/nginx.conf","-g","daemon off;"]
PHP:
FROM php:7.3-fpm-alpine
# Version
ENV PHPREDIS_VERSION 4.0.0
# Libs
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk add \
curl \
vim \
wget \
git \
openssl-dev\
zip \
unzip \
g++ make autoconf
# docker方式安装PDO extension
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install pcntl \
&& docker-php-ext-install sysvmsg
# Redis extension
RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
&& docker-php-ext-install pdo_mysql \
&& rm -rf /tmp/redis.tar.tgz \
&& docker-php-ext-enable redis
# 修改php.ini的文件 extension=redis.so
EXPOSE 9000
#设置工作目录
WORKDIR /www
(2)执行duckerfile文件生成镜像
到各自文件目录下运行以下命令
docker build -t php_srm_2007 .
docker build -t nginx_srm_2007 .
docker build -t redis_srm_2007 .
注意:生成命令后面带上“.”
最后生成完成