Docker入门3------手动编辑自定义镜像

手动编辑自定义镜像

  • 查看本地现有镜像:

  • 基于centos创建一个,会自动下载centos最新原始镜像
    docker run -it --name=web centos /bin/bash

  • 然后在容器内安装redis服务:

[root@b11c52af3a09 /]# mkdir /main
[root@b11c52af3a09 /]# cd /main/
[root@b11c52af3a09 main]# yum install -y  wget vim 
[root@b11c52af3a09 main]# wget http://download.redis.io/releases/redis-5.0.3.tar.gz
[root@b11c52af3a09 main]# tar -zvxf redis-5.0.3.tar.gz
# 编译环境
[root@b11c52af3a09 main]# yum install -y gcc make
[root@b11c52af3a09 main]# cd redis-5.0.3
# 编译
[root@b11c52af3a09 main]# make
# 修改配置文件(修改监听,密码,端口等设置后保存退出)
[root@b11c52af3a09 redis-5.0.3]# vim redis.conf
# 启动测试
[root@b11c52af3a09 src]# /main/redis-5.0.3/src/redis-server 
4221:C 21 Feb 2019 08:20:23.477 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4221:C 21 Feb 2019 08:20:23.477 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=4221, just started
4221:C 21 Feb 2019 08:20:23.477 # Warning: no config file specified, using the default config. In order to specify a config file use /main/redis-5.0.3/src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4221
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

继续在docker容器内部使用客户端测试也ok,可以退出容器了.

  • 查看最近创建的这个容器,名字是WEB

  • 查看这个容器发生的变化docker diff b11c52af3a09 会输出居多无比的信息

  • 提交容器修改

[root@localhost main]# 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)
  • 提交修改为一个镜像,指定提交内容,指定提交那个容器(web或者容器id),指定镜像的名称和版本号
[root@localhost main]# docker commit -a "sam@demo.com" -m "copile redis on centos" web sam/myredis:v1
sha256:e1cb69f9e47d78f20757f2770cdf5acede29e47ca61a194dbbaaee9babb64f6a
  • 可以发现自己的镜像生成了!

  • 查看修改记录 docker history sam/myredis:v1

  • 使用本地的镜像提供redis服务

[root@localhost main]#  docker run -p6379:6379 -d sam/myredis:v1 /main/redis-5.0.3/src/redis-server 
11c4d73c5e14329c5ca7c582f8bbd0cc668665a8d7f631601e456a16bed2b2cc

要想再次进入运行的容器内部: exec

docker exec -it 11c4d73c5e14 /bin/bash

而使用ctrl+p+q可以将该容器置于后台,而不是马上exited

于是整个手动构建就成功了

原文地址:https://www.cnblogs.com/radio/p/10413186.html