创建镜像
创建镜像有两种方式:
1、从已经创建的容器中更新镜像,并且提交这个镜像
2、使用 Dockerfile 指令来创建一个新的镜像
从原有镜像创建
废话不多,下面开始操作:
- 使用vi创建一个index.html文件,文件的内容为:
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
Hello World!
</body>
</html>
2. 使用cp命令将index.html复制到容器中,cp命令使用说明:
$ sudo docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
注意提前要启动容器:
$ sudo docker ps -a
$ sudo docker start 9fe0e4b5b4a3
执行效果:
然后在执行cp命令:
$ docker cp index.html 9fe0e4b5b4a3:/usr/share/nginx/html/
/usr/share/nginx/html/
是web服务的文件目录。
然后执行commit
命令,命令说明:
$ docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
执行命令:
$ docker commit -a "Li XiaoBai" -m "Modify index.html" 9fe0e4b5b4a3 nginx/hello:hello
使用docker images
就可以看到新添加的镜像了。
执行效果:
使用run
命令运行新添加的镜像:
sudo docker run --name nginx-hello1 -p 8080:80 -d d5002b6f7260
然后打开浏览器,输入127.0.0.1:8080
就可以看到我们添加的网页了:
第一步制作镜像成功。^_^
使用 Dockerfile制作镜像
先来看一下build
命令的使用说明:
$ sudo docker build --help
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])
首先再本地创建一个Dockerfile文件,文件内容如下:
FROM ubuntu
MAINTAINER Fisher "Li XiaoBai"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd xiaobai
RUN /bin/echo 'xiaobai:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令,安装了什么
执行创建命令:
$docker build -t xiaobai/ubuntu .
执行结果:
使用images
查看下生成的镜像:
大功告成。^_^