06-docker系列-使用dockerfile构建nginxredis镜像

万事须己运,他得非我贤。这篇文章主要讲述06-docker系列-使用dockerfile构建nginxredis镜像相关的知识,希望能为你提供帮助。
声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。



主旨
本文使用上一篇文章中说到的dockerfile方式,分别构建一个nginx,一个redis镜像。
环境



linux环境
docker环境



nginx镜像构建
创建目录,并切换至对应目录:




[yunweijia@localhost ~]$ mkdir -pv docker/nginx
mkdir: 已创建目录 "docker"
mkdir: 已创建目录 "docker/nginx"
[yunweijia@localhost ~]$ cd docker/nginx/



nginx安装脚本:











[yunweijia@localhost nginx]$ pwd
/home/yunweijia/docker/nginx
[yunweijia@localhost nginx]$ vim install.s
yum install -y wget tar gcc gcc-c++ make pcre pcre-devel zlib zlib-devel
cd /usr/local/src
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx & & make & & make install
\\rm -rf /usr/local/src/*
[yunweijia@localhost nginx]$



nginx启动脚本,记得添加可执行权限:







[yunweijia@localhost nginx]$ pwd
/home/yunweijia/docker/nginx
[yunweijia@localhost nginx]$ vim nginx
#!/bin/bash
/usr/local/nginx/sbin/nginx -g "daemon off; "
[yunweijia@localhost nginx]$ chmod +x nginx
[yunweijia@localhost nginx]$



dockerflie文件:









[yunweijia@localhost nginx]$ pwd
/home/yunweijia/docker/nginx
[yunweijia@localhost nginx]$ vim Dockerfile
FROM centos:7
COPY install.sh /tmp/install.sh
RUN sh /tmp/install.sh
COPY nginx /usr/bin/nginx
ENTRYPOINT ["nginx"]
[yunweijia@localhost nginx]$



        构建nginx镜像:









[yunweijia@localhost nginx]$ sudo docker build -t yunweijia:nginx /home/yunweijia/docker/nginx/
# 直至出现如下提示
Successfully built e46b589abaed
Successfully tagged yunweijia:nginx
[yunweijia@localhost nginx]$ sudo docker images# 新建了一个nginx镜像
REPOSITORYTAGIMAGE IDCREATEDSIZE
yunweijianginxe46b589abaedAbout a minute ago461MB
centos7eeb6ee3f44bd4 months ago204MB
[yunweijia@localhost nginx]$



【06-docker系列-使用dockerfile构建nginxredis镜像】    测试nginx镜像:

















[yunweijia@localhost nginx]$ sudo docker run -d yunweijia:nginx
cde16676029bf114bb4226c6aeeed8b3cd1f0b45c90b5a3ca5c488cf6315635a
[yunweijia@localhost nginx]$ sudo docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
cde16676029byunweijia:nginx"nginx"4 seconds agoUp 4 secondsbeautiful_ganguly
[yunweijia@localhost nginx]$
[yunweijia@localhost nginx]$ sudo docker exec -it cde16676029b /bin/bash
[root@cde16676029b /]# ps -ef | grep nginx
root100 14:23 ?00:00:00 /bin/bash /usr/bin/nginx
root710 14:23 ?00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
nobody870 14:23 ?00:00:00 nginx: worker process
root2490 14:23 pts/000:00:00 grep --color=auto nginx
[root@cde16676029b /]# exit
[yunweijia@localhost nginx]$
[yunweijia@localhost nginx]$ sudo docker stop cde16676029b
cde16676029b
[yunweijia@localhost nginx]$



redis镜像构建
创建目录,并切换至对应目录:






[yunweijia@localhost ~]$ pwd
/home/yunweijia
[yunweijia@localhost ~]$ mkdir -pv docker/redis
mkdir: 已创建目录 "docker/redis"
[yunweijia@localhost ~]$ cd docker/redis/
[yunweijia@localhost redis]$



redis安装脚本:













[yunweijia@localhost redis]$ pwd
/home/yunweijia/docker/redis
[yunweijia@localhost redis]$ vim install.sh
yum install -y wget gcc gcc-c++ make tar openssl openssl-devel cmake
cd /usr/local/src
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar -zxf redis-4.0.9.tar.gz
cd redis-4.0.9
make & & make PREFIX=/usr/local/redis install
mkdir -pv /usr/local/redis/conf/
cp redis.conf /usr/local/redis/conf/
\\rm -rf /usr/local/src/*
[yunweijia@localhost redis]$



redis启动脚本,记得添加可执行权限:







[yunweijia@localhost redis]$ pwd
/home/yunweijia/docker/redis
[yunweijia@localhost redis]$ vim redis
#!/bin/bash
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
[yunweijia@localhost redis]$
[yunweijia@localhost redis]$ chmod +x redis



Dockerfile文件:









[yunweijia@localhost redis]$ pwd
/home/yunweijia/docker/redis
[yunweijia@localhost redis]$ vim Dockerfile
FROM centos:7
COPY install.sh /tmp/install.sh
RUN sh /tmp/install.sh
COPY redis /usr/bin/redis
ENTRYPOINT ["redis"]
[yunweijia@localhost redis]$



构建redis镜像:










[yunweijia@localhost redis]$ sudo docker build -t yunweijia:redis /home/yunweijia/docker/redis/
# 直至出现如下提示
Successfully built 117c9de3eb27
Successfully tagged yunweijia:redis
[yunweijia@localhost redis]$ sudo docker images# 新建了一个redis
REPOSITORYTAGIMAGE IDCREATEDSIZE
yunweijiaredis117c9de3eb2720 seconds ago509MB
yunweijianginx553baf668d3f7 minutes ago461MB
centos7eeb6ee3f44bd4 months ago204MB
[yunweijia@localhost redis]$



测试redis镜像:















[yunweijia@localhost redis]$ sudo docker run -d yunweijia:redis
4d73e5af06132f5d477b1e3ba55bbba7e6390fea66952daefc355509a4366511
[yunweijia@localhost redis]$ sudo docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
4d73e5af0613yunweijia:redis"redis"7 seconds agoUp 6 secondsfocused_swirles
[yunweijia@localhost redis]$ sudo docker exec -it 4d73e5af0613 /bin/bash
[root@4d73e5af0613 /]# ps -ef | grep redis
root100 14:30 ?00:00:00 /bin/bash /usr/bin/redis
root710 14:30 ?00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root26110 14:30 pts/000:00:00 grep --color=auto redis
[root@4d73e5af0613 /]# exit
exit
[yunweijia@localhost redis]$ sudo docker stop 4d73e5af0613
4d73e5af0613
[yunweijia@localhost redis]$



        至此,使用dockerfile构建nginx、redis镜像完毕。下一篇我们将介绍下使用dockerfile构建python、jenkins镜像。

    推荐阅读