06-使用dockerfile构建nginx、redis镜像

7edb7a58ba324275dcc206da72e1919f.jpeg

主旨

本文使用上一篇文章中说到的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镜像。

1e85c63e3fac8d74671e380429223429.jpeg

如果本文内容对你有所帮助,请关注并分享我的公众号【运维家】。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值