1.创建Dockerfile文件
新建一个目录,在里面新建一个dockerfile文件(新建一个的目录,主要是为了和以防和其它dockerfile混乱 )
[[email protected] myfiles]# mkdir centos6.8_dockerfile [[email protected] myfiles]# [[email protected] myfiles]# cd centos6.8_dockerfile/
创建dockerfile文件(vim Dockerfile)
# The dockerfile has Change add sshd services on Centos7.0 #centos7:latest image FROM centos:6.8 MAINTAINER xxx,http://www.cnblogs.com/xxx #Install sshd net-tools RUN yum install openssh-server net-tools -y RUN mkdir /var/run/sshd #Set password for root RUN echo ‘root:xxxxxxx‘ | chpasswd RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/‘ /etc/ssh/sshd_config #Set history record ENV HISTTIMEFORMAT "%F %T " #Fix sshd service:Read from socket failed: Connection reset by peer? RUN ssh-keygen -A #Change timezone CST RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #Open 22 port EXPOSE 22 #Auto running sshd service CMD ["/usr/sbin/sshd","-D"]
上述文件内容就是一个dockerfile 常见的命令组合。开头带#号的为注释
文件解释: FROM: 必不可少的命令,从某个镜像作为基。如 FROM <image_name> ,或者 FROM <image_name>:<tag>. 如果不加tag,默认为latest。先从本地镜像仓库去搜索基镜像,如过本地没有,在去网上docker registry去寻找。 MAINTAINER:标明该Dockerfile作者及联系方式,可忽略不写 RUN:建立新的镜像时,可以执行在系统里的命令,如安装特定的软件以及设置环境变量。 ENV:设置系统环境变量(注意:写在/etc/profile里的命令在dockerfile这里会不生效,所以为改成ENV的方式) EXPOSE:开放容器内的端口,但不和宿主机进行映射。方便在宿主机上进行开发测试。(如需映射到宿主机端口,可在运行容器时使用 -p host_port:container_port) CMD:设置执行的命令,经常用于容器启动时指定的某个操作。如执行自定义脚本服务,或者是执行系统命令。CMD 只能存在一条,如在Dockerfile中有多条CMD的话,只有最后一条CMD生效!
2.执行build 创建镜像
使用docker build命令来创建镜像
[[email protected] centos6.8_dockerfile]# docker build -t centos_ssh_1_6.8 . Sending build context to Docker daemon 15.36 kB Step 1/10 : FROM centos:6.8 ---> 0cd976dc0a98 Step 2/10 : MAINTAINER xxx,http://www.cnblogs.com/xxx ---> Using cache ---> 025bdf5382de Step 3/10 : RUN yum install openssh-server net-tools -y ---> Using cache ---> f970cedf0dc6 Step 4/10 : RUN mkdir /var/run/sshd ---> Using cache ---> 620754832e8c Step 5/10 : RUN echo ‘root:xxxxxxx‘ | chpasswd ---> Using cache ---> 8bcd6f4ae32b Step 6/10 : RUN sed -i ‘s/PermitRootLogin prohibit-password/PermitRootLogin yes/‘ /etc/ssh/sshd_config ---> Using cache ---> b46214833685 Step 7/10 : ENV HISTTIMEFORMAT "%F %T " ---> Using cache ---> c18c2102a92e Step 8/10 : RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Running in 77698b6cfce0 ---> 0e755b7f8c4a Removing intermediate container 77698b6cfce0 Step 9/10 : EXPOSE 22 ---> Running in 29ab858378f7 ---> 3c8c969057c4 Removing intermediate container 29ab858378f7 Step 10/10 : CMD /usr/sbin/sshd -D ---> Running in 8ebd12375b64 ---> ff004247b678 Removing intermediate container 8ebd12375b64 Successfully built ff004247b678 [[email protected] centos6.8_dockerfile]#
-t 选项来docker build新的镜像以便于标记构建的镜像,. 表示当前目录,也可以指定dockerfile 文件所在目录。
3.查看镜像列表,并创建容器
[[email protected] centos6.8_dockerfile]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_ssh_1_6.8 latest ff004247b678 About a minute ago 291 MB nginx_diy latest e24d0b283207 About an hour ago 109 MB nginx latest 34678a414b84 About an hour ago 109 MB docker01.lo:5000/nginx latest 958a7ae9e569 3 weeks ago 109 MB nginx <none> 958a7ae9e569 3 weeks ago 109 MB registry latest 9d0c4eabab4d 6 weeks ago 33.2 MB swarm latest 36b1e23becab 5 months ago 15.9 MB centos 6.8 0cd976dc0a98 9 months ago 195 MB
4.创建和查看容器
[[email protected] centos6.8_dockerfile]# docker run -d -it --name mycentos centos_ssh_1_6.8 813171a387c2cc2a2c91ce96815e300c70bf1d42b6bd9e5c83f2d4608a2a2e6b [[email protected] centos6.8_dockerfile]# docker exec mycentos hostname -I 172.17.0.3 [[email protected] centos6.8_dockerfile]# ssh [email protected]172.17.0.3 date Read from socket failed: Connection reset by peer [[email protected] centos6.8_dockerfile]# docker exec mycentos netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1/sshd tcp 0 0 :::22 :::* LISTEN 1/sshd
原文链接:http://www.cnblogs.com/hanyifeng/p/6116067.html
时间: 2024-12-29 11:33:33