Dockerfile主体结构分为四部分:
基础镜像信息
维护者信息
镜像操作指令
容器启动时执行指令
Dockfile文件指令说明


创建镜像指令
docker build -t 镜像名 .
参数选项


.dockerignore文件
使用.dockerignore文件可以忽略无关文件,构建镜像时不对其操作,常用的有:
vim .dockerignore
*/temp*
*/*temp*
tmp?
~*
Dockerfile
!README.md
TIPS:
*代表任意多个字符
?代表单个字符
!表示不匹配
多步骤创建
使用多步骤创建,可以保证最终生成的运行环境镜像保持精简,降低维护复杂度
对于需要进行编译的应用来说,通常情况下至少需要准备两个环境的镜像(编译环境镜像,运行环境镜像)
实例
1.使用golang镜像编译应用,使用alpine运行应用,这样可以精简化镜像



2.构建具有ssh服务的ubuntu镜像
创建ssh密钥
cd ~/.ssh
ssh-keygen -t rsa
cat id_rsa.pub > /root/test/authorized_keys
创建启动脚本
vim run.sh
#!/bin/bash
/usr/sbin/sshd -D
创建Dockerfile
vim Dockerfile
FROM ubuntu:18.04
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse \n\
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse \n'\
> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y openssh-server \
&& mkdir -p /var/run/sshd \
&& mkdir -p /root/.ssh \
&& sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 755 /run.sh
EXPOSE 22
CMD ["/run.sh"]
构建镜像
docker build -t sshd:test .
查看镜像

使用镜像
docker run -d -p 8888:22 sshd:test
连接服务
ssh 127.0.0.1 -p 8888
