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
- a. 拉取静态的镜像文件
-
方式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.ymlversion: "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