Docker学习实践(二)

docker镜像的构建

一 基础构建

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

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

    • a. 拉取静态的镜像文件
      docker pull karthequian/helloworld:latest
    • 创建并运行容器
      docker run -p 80:80/tcp "karthequian/helloworld:latest"
    • 第三步: 访问服务,安装docker环境主机的ip:80
  • 方式2: 编辑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"

二 、 由容器构建镜像

  • 使用docker-compose.yml根据基础镜像创建一个基础容器
    docker-compose.yml
    version: "2.0"
    services:
    web:
        container_name: base_build
        image: ubuntu:16.04
        tty: True
    
    docker-compose up -d
    
  • 进入容器内部并做出修改
    docker exec -it base_build bash
    
  • 提交修改创建新的镜像 my_image:1.0
    docker commit -m "安装了supervisor服务" base_build my_image:1.0
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值