前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker镜像创建

Docker镜像创建

作者头像
李小白是一只喵
发布2020-04-23 15:17:54
1.1K0
发布2020-04-23 15:17:54
举报
文章被收录于专栏:算法微时光算法微时光

创建镜像

创建镜像有两种方式: 1、从已经创建的容器中更新镜像,并且提交这个镜像 2、使用 Dockerfile 指令来创建一个新的镜像

从原有镜像创建

废话不多,下面开始操作:

  1. 使用vi创建一个index.html文件,文件的内容为:
代码语言:javascript
复制
<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

</head>

<body>

Hello World!

</body>

</html>

2. 使用cp命令将index.html复制到容器中,cp命令使用说明:

代码语言:javascript
复制
$ 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

注意提前要启动容器:

代码语言:javascript
复制
$ sudo docker ps -a
$ sudo docker start 9fe0e4b5b4a3

执行效果:

然后在执行cp命令:

代码语言:javascript
复制
$ docker cp index.html 9fe0e4b5b4a3:/usr/share/nginx/html/

/usr/share/nginx/html/是web服务的文件目录。 然后执行commit命令,命令说明:

代码语言:javascript
复制
$  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)

执行命令:

代码语言:javascript
复制
$ docker commit -a "Li XiaoBai" -m "Modify index.html" 9fe0e4b5b4a3 nginx/hello:hello

使用docker images就可以看到新添加的镜像了。 执行效果:

使用run命令运行新添加的镜像:

代码语言:javascript
复制
sudo docker run --name nginx-hello1 -p 8080:80 -d d5002b6f7260

然后打开浏览器,输入127.0.0.1:8080就可以看到我们添加的网页了:

第一步制作镜像成功。^_^

使用 Dockerfile制作镜像

先来看一下build命令的使用说明:

代码语言:javascript
复制
$ 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文件,文件内容如下:

代码语言:javascript
复制
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 在镜像内执行命令,安装了什么

执行创建命令:

代码语言:javascript
复制
$docker build -t xiaobai/ubuntu .

执行结果:

使用images查看下生成的镜像:

大功告成。

参考

Docker 镜像使用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建镜像
  • 从原有镜像创建
  • 使用 Dockerfile制作镜像
  • 参考
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档