Docker——dockerfile

一、dockerfile

  • The dockerfile is a text file that contains all the commands we manually execute in order to build the docker image. Docker can read instructions from the dockerfile to automatically build a mirror. We can use the docker build command to create an automatic build

1. Create a letter directory

2, save Dockerfile

3. The files needed to build the Dockerfile

4. The path where the Dockerfile file is located is also called the context

Two, Dockerfile instructions

1,FROM
指定一个基础镜像,后续指令将在此镜像上运行

FROM 镜像名
2,USER
指定一个用户,后续RUN CMD以及,ENTRYPOINT指令都会使用该用户去执行,该用户必须存在

USER 用户名
3,WORKDIR
可指定工作目录,RUN,CMD,COPY,ADD指令将在指定工作目录汇总执行,命令执行时的当前目录

WORKDIR 目录
4,RUN、CMD、ENTRYPORINT
(1)RUN,两种形式


RUN <conmand>,使用shell 去执行指定的指令command,一般默认的shell为 /bin/sh -c		
例如:
	CMD echo "hello world"
		
		
RUN ["executable","paraml","param2",...],使用可执行的文件或程序executable,给予相应的参数param。	
例如:
	CMD ["ls","-l"]
5. COPY and ADD
COPY和ADD都用于将文件,目录等复制到镜像中

ADD <src>... <dest>			ADD可以添加远程路径,<src>可识别压缩格式,会自动解压为目录
ADD ["<SRC>",... "<dest>"]

COPY <src>... <dest>
COPY ["<src>",... "<dest>"]
6,ENV
用于设置环境变量

ENV <key> <value>
ENV <key>=<value> <key>=<value>...
7,VOLUME
  • The specified mount directory will be created, and the corresponding anonymous volume will be created when the container is running:
    VOLUME mount directory

  • For example:
    VOLUME /data1 /data2 When the container is running, create two anonymous volumes and mount them to the
    /data1/data2 directory of the container

8,EXPOSE
  • Specify the network port to be monitored when the container is running. It is different from the -p parameter of the docker run command. It does not actually map the port, but exposes the port to allow external or other containers to access

  • To expose the container port, you need to use the -p or -pubulish parameter with the docker run command. If you use -p to map ports randomly, DOcker will map all EXPOSE ports declared in the Dockerfile.

Three, Dockerfile example

1. Build an ssh service
# 指定基础镜像
FROM ubuntu:14.04

# 安装软件
RUN apt-get update && apt-get install -y openssh-server && mkdir /var/run/sshd

# 添加用户 shiyanlou 及设定密码
RUN useradd -g root -G sudo shiyanlou && echo "shiyanlou:123456" | chpasswd shiyanlou

# 暴露 SSH 端口
EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]
2. Create a dockerfile
  • For example:
    docker build -t sshd:test.
    Docker build -t tag name path
3. Run the mirror
  • docker run -itd -p 10001:22 sshd:test maps port 10001 to port 22 of the container

ps:docker build detailed commands: https://www.runoob.com/docker/docker-build-command.html

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/114580278