Linux|Docker镜像|以centos7为基础镜像制作一个目标镜像
Posted 和呵呵和
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux|Docker镜像|以centos7为基础镜像制作一个目标镜像相关的知识,希望对你有一定的参考价值。
1. 以centos7为基础镜像
2. 在里面安装好IP,vim,ping命令
3. 编译安装nginx,使用指定的配置文件nginx.conf
4. 启动容器的时候,就启动nginx
5.网页代码上传到容器里:
1.直接做到镜像里
2.使用数据卷挂载使用
如下是具体实现操作:
1.准备环境,新建目录
[root@yihelinux myimage]# mkdir nginx
[root@yihelinux myimage]# cd nginx/
[root@yihelinux nginx]# pwd
/lianxi/myimage/nginx
[root@yihelinux nginx]#
-
首先我们新建一个文件夹,用于存放制作镜像的dockerfile
-
下载nginx源码文件可以直接使用
curl -O 指定版本的nginx包
这里我提前下载好了放在和Dockerfile同一级目录下
- 然后编写一个编译安装nginx的脚本文件
install_nginx.sh脚本文件内容如下:
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
#download nginx
mkdir -p /nginx
cd /nginx
#解压 下载的nginx的源码包
tar xf nginx-1.21.1.tar.gz
cd nginx-1.21.1
#生成编译前配置工作-->Makefile
./configure --prefix=/usr/local/nginx1 --with-threads --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-file-aio --with-http_stub_status_module --with-stream
#编译
make
#编译安装--》将编译好的二进制程序安装指定目录/usr/local/nginx1
make install
~
最后目录结构如下
[root@yihelinux nginx]# tree ./
./
├── Dockerfile
├── install_nginx.sh
└── nginx-1.21.1.tar.gz
0 directories, 3 files
2.编写Dockerfile
文件内容如下
FROM centos:7
ENV NGINX_VERSION 1.21.1
ENV AUTHOR yihe
LABEL maintainer="yihe<2508022907@qq.com>"
WORKDIR /nginx
COPY . /nginx
RUN set -ex;\\
bash install_nginx.sh;\\
yum install vim iputils net-tools iproute -y
EXPOSE 80
ENV PATH=/usr/local/nginx1/sbin:$PATH
STOPSIGNAL SIGQUIT
CMD ["nginx","-g","daemon off;"]
~
- dockerfile内容解答:
- 1
q:为什么有的镜像启动需要接-it,有的不需要?
a:centos7或者ubuntu的基础镜像,在启动的时候,运行的程序是bash
一运行完就马上退出了,使用-it可以保证一直有一个前台进程运行,保证容器不挂掉
-
2
CMD [“nginx”,"-g",“daemon off;”]#让nginx在前台运行,-g daemon off 将off值赋值给daemon这个变量,告诉ngixn不要在后台启动,在前台启动;daemon是守护进程,默认在后台运行 -
3
可以使用如下命令查看某个命令来自哪个包
[root@yihelinux nginx]# yum provides ping
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* epel: mirror.sjtu.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.cn99.com
iputils-20160308-10.el7.x86_64 : Network monitoring tools including ping
Repo : base
Matched from:
Filename : /usr/bin/ping
iputils-20160308-10.el7.x86_64 : Network monitoring tools including ping
Repo : @anaconda
Matched from:
Filename : /usr/bin/ping
或者用如下命令:
[root@yihelinux nginx]# which ping
/usr/bin/ping
[root@yihelinux nginx]# rpm -qf /usr/bin/ping
iputils-20160308-10.el7.x86_64
[root@yihelinux nginx]#
3生成镜像images
[root@yihelinux nginx]# ls
Dockerfile install_nginx.sh nginx-1.21.1.tar.gz
[root@yihelinux nginx]# docker build -t yihe-nginx:1.0 .
Sending build context to Docker daemon 1.069MB
Step 1/11 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pulling fs layer
- 最后出现如下说明制作成功
Step 9/11 : ENV PATH=/usr/local/nginx1/sbin:$PATH
---> Running in c74287833a75
Removing intermediate container c74287833a75
---> 152c94eac420
Step 10/11 : STOPSIGNAL SIGQUIT
---> Running in 3c14415891fe
Removing intermediate container 3c14415891fe
---> 795ac7054c28
Step 11/11 : CMD ["nginx","-g","daemon off;"]
---> Running in 118a10de0ac2
Removing intermediate container 118a10de0ac2
---> 9c6248a3878d
Successfully built 9c6248a3878d
Successfully tagged yihe-nginx:1.0
- 查看镜像
[root@yihelinux nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yihe-nginx 1.0 9c6248a3878d About a minute ago 543MB
yihello_1 latest 258686a933a8 9 days ago 159MB
<none> <none> 210c72dbc6ba 9 days ago 148MB
redis latest aa4d65e670d6 4 weeks ago 105MB
nginx latest 08b152afcfae 5 weeks ago 133MB
mysql latest c60d96bd2b77 5 weeks ago 514MB
hello-world latest d1165f221234 5 months ago 13.3kB
daocloud.io/centos latest 300e315adb2f 8 months ago 209MB
centos 7 8652b9f0cb4c 9 months ago 204MB
python 2.7-slim eeb27ee6b893 16 months ago 148MB
[root@yihelinux nginx]#
第二个就是我们制作的镜像
4.运行制作好的镜像
[root@yihelinux nginx]# docker run -dp 3390:80 --name yihe-nginx-1 yihe-nginx:1.0
4c13ff29b94a869814057944f7f9f8d507fe46a64726f1e5bfd46f2a76fcdf78
- 没有报错说明运行成功,查看一下容器
[root@yihelinux nginx]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c13ff29b94a yihe-nginx:1.0 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:3390->80/tcp, :::3390->80/tcp yihe-nginx-1
[root@yihelinux nginx]#
5.测试访问
进入容器,查看vim,ping等命令能否使用
[root@yihelinux nginx]# docker exec -it yihe-nginx-1 /bin/bash
[root@4c13ff29b94a nginx]# vim
[root@4c13ff29b94a nginx]# ping 192.168.10.15 -c 1
PING 192.168.10.15 (192.168.10.15) 56(84) bytes of data.
64 bytes from 192.168.10.15: icmp_seq=1 ttl=64 time=0.112 ms
--- 192.168.10.15 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.112/0.112/0.112/0.000 ms
[root@4c13ff29b94a nginx]# ip route
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2
[root@4c13ff29b94a nginx]#
使用windows本机访问容器宿主机3390端口,看是否运行
nice!
以上是关于Linux|Docker镜像|以centos7为基础镜像制作一个目标镜像的主要内容,如果未能解决你的问题,请参考以下文章