一、Dockerfile指令简介
1、FROM指定来源于哪个镜像
例如:FROM centos FROM centos:latest
2、MAINTAINER指定作者信息
例如:MAINTAINER wyu wyu.com
3、RUN镜像操作指令
例如:RUN yum install httpd
RUN [“/bin/bash”,”-c”,”echo hello”]
4、CMD
5、EXPOSE
例如:EXPOSE 22 80 8443用来指定要映射出去的端口,比如容器内部启动了sshd和nginx,所以要将22和80端口暴露出去,需要配合-P,在启动容器时加参数-P自动分配,-p指定具体端口。
6、ENV为后续的RUN提供环境变量
例如:ENV PATH /usr/local/musql/bin:$PATH
ENV MYSQL_version 5.6
7、ADD将本地的一个文件或目录拷贝到某个目录里
格式为 ADD <src><dest>,其中src为Dockerfile所有目录的相对路径,也可以是一个url,例如ADD <conf/vhosts></usr/local/nginx/conf>
8、COPY,格式同ADD,不支持url
9、ENTRYPOINT格式类似于CMD
10、VOLUME创建一个可以从本地主机或其他容器挂载的挂载点
例如 VOLUME [“/data”]
11、USER指定运行容器的用户
例如:USER wyu
12、WORKDIR为后续RUN,CMD,ENTRYPOINT指定工作目录
例如:WORKDIR /path/to/workdir
二、Dockerfile创建镜像
1、下载nginx配置文件
wget http://www.apelearn.com/study_v2/.nginx_conf
2、编辑Dockerfile,加入以下内容
[root@oracle2 docker]# cat Dockerfile
##set the base image to CentOS
FROM centos
# File Author / Maintainer
MAINTAINER wyu wyu.com
# Install necessary tools
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
# Install Nginx
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar zxvf nginx-1.8.0.tar.gz
RUN mkdir -p /usr/local/nginx
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -fv /usr/local/nginx/conf/nginx.conf
COPY .nginx_conf /usr/local/nginx/conf/nginx.conf
# Expose ports
EXPOSE 80
# Set the default command to execute when creating a new container
ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
3、创建镜像
[root@oracle2 docker]# docker build -t test1_nginx .
注意语句最后有个 . 意思为从当前目录寻找Dockerfile文件
4、查看创建的镜像
5、打开指定镜像
[root@oracle2 docker]# docker run -itd -p 8099:80 test1_nginx bash
三、使用Docker compose部署服务
1\docker compose可以方便我们快捷高效的管理容器的启动,停止,重启等操作,类似于Linux下的shell脚本,基于yaml语法,在该文件里可以描述应用的架构,比如使用镜像,数据卷,网络模式,监听端口等。
2、安装compose
curl -L
https://github.com/docker/compose/releases/download/1.17.0-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
3、修改权限
chomd 755 /usr/local/bin/docker-compose
4、查看版本信息
5、部署实际服务
(1)编辑docker-compose.yml文件
[root@oracle2 ~]# cat docker-compose.yml
version: "2"
services:
app1:
image: centos_nginx
ports:
- "8080:80"
networks:
- "net1"
volumes:
- /data/:/data
app2:
image:wyu_net
networks:
- "net2"
volumes:
- /data/:/data1
entrypoint: tail -f /etc/passwd
networks:
net1:
driver: bridge
net2:
driver: bridge
(2)启动两个容器