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

Posted 公号运维家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了06-docker系列-使用dockerfile构建nginxredis镜像相关的知识,希望对你有一定的参考价值。

声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。

06-docker系列-使用dockerfile构建nginx、redis镜像_redis


主旨

本文使用上一篇文章中说到的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镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
yunweijia nginx e46b589abaed About a minute ago 461MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[yunweijia@localhost nginx]$


    测试nginx镜像:


















[yunweijia@localhost nginx]$ sudo docker run -d yunweijia:nginx
cde16676029bf114bb4226c6aeeed8b3cd1f0b45c90b5a3ca5c488cf6315635a
[yunweijia@localhost nginx]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cde16676029b yunweijia:nginx "nginx" 4 seconds ago Up 4 seconds beautiful_ganguly
[yunweijia@localhost nginx]$
[yunweijia@localhost nginx]$ sudo docker exec -it cde16676029b /bin/bash
[root@cde16676029b /]# ps -ef | grep nginx
root 1 0 0 14:23 ? 00:00:00 /bin/bash /usr/bin/nginx
root 7 1 0 14:23 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -g daemon off;
nobody 8 7 0 14:23 ? 00:00:00 nginx: worker process
root 24 9 0 14:23 pts/0 00: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
REPOSITORY TAG IMAGE ID CREATED SIZE
yunweijia redis 117c9de3eb27 20 seconds ago 509MB
yunweijia nginx 553baf668d3f 7 minutes ago 461MB
centos 7 eeb6ee3f44bd 4 months ago 204MB
[yunweijia@localhost redis]$


测试redis镜像:
















[yunweijia@localhost redis]$ sudo docker run -d yunweijia:redis
4d73e5af06132f5d477b1e3ba55bbba7e6390fea66952daefc355509a4366511
[yunweijia@localhost redis]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d73e5af0613 yunweijia:redis "redis" 7 seconds ago Up 6 seconds focused_swirles
[yunweijia@localhost redis]$ sudo docker exec -it 4d73e5af0613 /bin/bash
[root@4d73e5af0613 /]# ps -ef | grep redis
root 1 0 0 14:30 ? 00:00:00 /bin/bash /usr/bin/redis
root 7 1 0 14:30 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 26 11 0 14:30 pts/0 00: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镜像。

以上是关于06-docker系列-使用dockerfile构建nginxredis镜像的主要内容,如果未能解决你的问题,请参考以下文章

Docker系列-第六篇DockerFile解析

系列2使用Dockerfile创建带sshd服务的Centos Docker镜像

Dockerflie概述

Docker虚拟化技术系列之-DockerFile配置

Docker入门与应用系列Dockerfile

07-docker系列-使用dockerfile构建pythonjenkins镜像