Docker学习实践(二)

Dockerfile的使用

熟悉linux系统中的Makefile的概念,这里Dockerfile就是类似Makefile文件的一个配置文件,用于快速构建我们所需的镜像(image)
通过例子了解:
docker-helloworld

  • 方式一:直接使用仓库中镜像

    • 第一步: 拉取静态的镜像文件
      docker pull karthequian/helloworld:latest
    • 第二步:创建并运行容器
      docker run -p 80:80/tcp "karthequian/helloworld:latest"
    • 第三步: 访问服务,安装docker环境主机的ip:80
  • 方式二: 编辑Dockerfile文件,使用docker build -t image-name .构建

    ############################################################
    # Dockerfile to build Nginx Installed Containers
    # Based on Ubuntu
    ############################################################


    # Set the base image to Ubuntu
    FROM ubuntu

    # File Author / Maintainer
    MAINTAINER Karthik Gaekwad

    # Install Nginx

    # Add application repository URL to the default sources
    # RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list

    # Update the repository
    RUN apt-get update

    # Install necessary tools
    RUN apt-get install -y vim wget dialog net-tools

    RUN apt-get install -y nginx

    # Remove the default Nginx configuration file
    RUN rm -v /etc/nginx/nginx.conf

    # Copy a configuration file from the current directory
    ADD nginx.conf /etc/nginx/

    RUN mkdir /etc/nginx/logs

    # Add a sample index file
    ADD index.html /www/data/

    # Append "daemon off;" to the beginning of the configuration
    RUN echo "daemon off;" >> /etc/nginx/nginx.conf

    # Expose ports
    EXPOSE 80

    # Set the default command to execute
    # when creating a new container
    CMD ["nginx"]
  • 命令介绍:
    FROM
    它表示新的镜像是从什么基础镜像开始构建的
    MAINTAINER
    指定该镜像的创建者
    ENV
    设置环境变量
    RUN
    运行shell命令,如果多条命令可以使用”&&”连接
    COPY
    将编译机本地的文件拷贝到镜像文件系统中
    EXPOSE
    指定监听的窗口
    ENTRYPOINT
    这个关键字不在构建镜像时执行,容器启动后才执行
  • 第一步:构建自己的镜像
    docker build -t karthequian/helloworld:latest .
  • 第二步:创建并运行自己的容器
    docker run -p 80:80/tcp "karthequian/helloworld:latest"

猜你喜欢

转载自blog.csdn.net/yzlll/article/details/80734946
今日推荐