微服务之——DockerFile 编写
一、自定义镜像有两种方法:
1、docker commit
启动一个容器,增删改查,安装软件,修改配置文件等 ; 另存为一个新镜像
docker run -it docker.io/centos 启动一个容器
yum install -y vim net-tools
ctrl+p+q 退出容器
docker ps 查看容器ID
docker commit 容器id 镜像名:标签名
_____________________________________________________________________________________
2、编写dockerfile 文件
Dockerfile 语法格式:
FROM : 基础镜像
MAINTAINER: 镜像创建者信息
EXPOSE: 开放端口
ENV: 设置变量 (有些服务软件安装需要环境变量)
ADD: 复制文件到镜像,也可以使用wget功能
COPY: 类似于add,只不过他不支持wget
RUN: 制作镜像时执行的命令,可以有多个
WORKDIR: 定义容器默认工作目录
CMD: 容器启动执行时的命令,仅可以有一条CMD
ENTRYPOINT : 类似于cmd ,cmd 命令可以被docker run 覆盖,而他不会被覆盖,而且要比CMD或者 docker run 指定的命令要靠前执行
-----------------Dockerfile(1)--USER--WORKDIR指令----------------
[root@localhost docker]# mkdir /data/docker/dockerfile
[root@localhost docker]# cd /data/docker/dockerfile
[root@localhost docker]# vim dockerfile
FROM feixiangkeji974907/nginx:v1.12.2
USER nginx
WORKDIR /usr/share/nginx/html
--------------------构建镜像--------------------------
docker build -t 镜像名:标签 Dockerfile所在路径
eg :
[root@localhost dockerfile]#docker build -t feixiangkeji974907/nginx:v1 .
-------------------查看镜像--------------------------------
[root@localhost dockerfile]# docker images
启动容器
[root@localhost dockerfile]# docker run --rm -it --name
nginx_with_nginx_workdir feixiangkeji974907/nginx:v1 /bin/bash
[nginx@ab5a49eef0cd /usr/share/nginx/html]$ whoami --USER 指令,查看当前登录的用户是谁
nginx
[nginx@ab5a49eef0cd /usr/share/nginx/html]$ pwd ---WORKDIR指令,定义容器默认工作目录
/usr/share/nginx/html
------------------Dockerfile(2)---ADD-EXPOSE指令-------------------------
[root@localhost dockerfile]# cp /root/html/index.html /data/docker/dockerfile
[root@localhost dockerfile]# ls
dockerfile index.html
[root@localhost dockerfile]# vim dockerfile
FROM feixiangkeji974907/nginx:v1.12.2
ADD /root/html/index.html /usr/share/nginx/html/index.html ----复制文件到镜像
EXPOSE 80 ----指定容器内使用端口
构建镜像:
[root@localhost dockerfile]# docker build -t feixiangkeji974907/nginx:v1.12.2_with_add_expose \
/data/docker/dockerfile
[root@localhost dockerfile]# docker images
启动容器:
[root@localhost dockerfile]# docker run -d --name nginx_expose -P feixiangkeji974907/nginx:v1.12.2_with_add_expose
####说明####
-P 大写 (Docker 会随机映射一个随机的端口到内部容器开放的网络端口)
-p 小写 (指定要映射的IP和端口,但是在一个指定端口上只可以绑定一个容器。支持的格式有 hostPort:containerPort
ip:hostPort:containerPort、 ip::containerPort )
[root@localhost dockerfile]# curl 127.0.0.1:32773
------------------Dockerfile(3)---RUN-ENV指令-------------------------
先用 yum list bind --show-duplicates 查看bind 的版本
[root@localhost dockerfile]# yum list bind --show-duplicates
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
bind.x86_6432:9.11.4-9.P2.el7- base
当前版本是 9.11.4
编写 /data/dock/dockerfile
FROM centos
ENV VER 9.11.4 #设置环境变量
RUN yum install bind-$VER -y # 制作镜像时执行的命令,可以有多个
构建镜像:
[root@localhost dockerfile]# docker build -t feixiangkeji974907/bind:v9.11.4_with_env_run .
[root@localhost dockerfile]# docker images
启动容器:
[root@localhost dockerfile]# docker run -it --name centos_bind feixiangkeji974907/bind:v9.11.4_with_env_run /bin/bash
####可以看到容器里的环境变为也存在VER=9.11.4 ;bind软件也已经安装好了
-----------------Dockerfile(4)---CMD指令--------------------
编写 /data/dock/dockerfile
FROM centos:7
RUN yum install httpd -y
CMD ["httpd", "-D", "FOREGROUND"]
### CMD 指令支持 shell 和 exec 两种格式 ,推荐使用exec格式
shell 格式的话如下:
CMD echo "hello"
exec 格式的话如下:
CMD ["echo","hello"]
其中 exec 格式必须用双引号,中间用逗号隔开,执行命令 和命令后参数都是用双引号引起来,每个都要用逗号隔开。
shell 格式和 exec 格式主要区别在于:
shell 格式的话,实际的命令会被包装为 sh -c 的参数的形式进行执行。比如:
CMD echo $HOME
在实际执行中,会将其变更为:
CMD [ "sh", "-c", "echo $HOME" ]
构建镜像
[root@localhost dockerfile]# docker build . -t feixiangkeji974907/centos:7_httpd
[root@localhost dockerfile]# docker images
启动容器:
[root@localhost dockerfile]# docker run -d --name myhttpd -p 8888:80 feixiangkeji974907/centos:7_httpd
访问测试:
-----------------Dockerfile(5)---ENTRYPOINT指令--------------------
编写 /data/dock/dockerfile
FROM centos:7
RUN yum install epel-release -q -y && yum install nginx -y
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd #命令不会被docker-run 命令覆盖,ENTRYPOINT/CMD中最后的一个命令必须要是无限执行的命令
构建镜像
[root@localhost dockerfile]# docker build -t feixiangkeji/centos:nginx .
启动容器
[root@localhost dockerfile]# docker run -d --name centos8_nginx -p 88:80 feixiangkeji/centos:nginx
访问测试: