Docker: 创建保存镜像
镜像的创建和保存
- 基于现有父镜像开发并commit
- 使用Dockerfile文件创建
一.基于现有父镜像开发并commit
- pull and start a new container
docker pull ubuntu:18.04
docker run -it ubuntu:18.04 bash
-
deploy app and edit /run.sh
-
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
- run the new container by run.sh
docker run ubuntu.18.04:v1 /run.sh
二.使用Dockerfile文件创建
- 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
- build new image
当前目录.
上下文路径创建镜像:
docker build -t myimage:v1 .
Github地址:https://github.com/kumataahh