Docker: 创建保存镜像

镜像的创建和保存

  • 基于现有父镜像开发并commit
  • 使用Dockerfile文件创建

一.基于现有父镜像开发并commit

  1. pull and start a new container
docker pull ubuntu:18.04
docker run -it ubuntu:18.04 bash
  1. deploy app and edit /run.sh

  2. exit and commit this container

// docker commit ID SW_NAME:VERSION
// example:
root@13336c183077:/# exit
exit
bear@k40 ~% docker commit 13336c183077 ubuntu.18.04:v1
  1. run the new container by run.sh
docker run ubuntu.18.04:v1 /run.sh

二.使用Dockerfile文件创建

  1. touch a new Dockerfile and edit:
FROM nginx
RUN echo 'I'm nginx' > /usr/share/nginx/html/index.html
  • FROM: IMAGE, base 父镜像
  • RUN:["可执行文件", "参数1", "参数2"], like RUN ["./test.php", "dev", "offline"] == RUN ./test.php dev offline
    注意:
FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz
以上执行会创建 3 层镜像。可简化为以下格式:
FROM centos
RUN yum install wget \
    && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
    && tar -xvf redis.tar.gz
  1. build new image

当前目录.上下文路径创建镜像:

docker build -t myimage:v1 .
posted @ 2020-12-11 15:10  kumata  阅读(337)  评论(0编辑  收藏  举报