Dockfile定制镜像

Posted walkinginthesun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dockfile定制镜像相关的知识,希望对你有一定的参考价值。

Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。

1. 定制redis镜像

1.1 Dockerfile文件

mkdir redis
cd redis
touch Dockerfile

Dockerfile文件

FROM ubuntu:latest

RUN apt-get update     && apt-get install -y redis     && mkdir -p /home/ld

COPY ./testfile.txt /home/ld

VOLUME /data

CMD [ "redis-server" ]

1.2 构建镜像命令

[[email protected] ]# docker build -t redis:1.0.0 .
Sending build context to Docker daemon  2.56 kB
Step 1/5 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/5 : RUN apt-get update     && apt-get install -y redis     && mkdir -p /home/ld
 ---> Using cache
 ---> f16df9849ef0
Step 3/5 : COPY ./testfile.txt /home/ld
 ---> Using cache
 ---> 0f15de0e812f
Step 4/5 : VOLUME /data
 ---> Using cache
 ---> 5363daff78a5
Step 5/5 : CMD redis-server
 ---> Using cache
 ---> 614b878bc14e
Successfully built 614b878bc14e
[[email protected] ]# 

1.3 查看镜像

[[email protected] ]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               1.0.0               614b878bc14e        12 days ago         125 MB
registry            latest              b2b03e9146e1        4 weeks ago         33.3 MB
golang              latest              4e611157870f        5 weeks ago         794 MB
redis               latest              71a81cb279e3        5 weeks ago         83.4 MB
httpd               latest              2a7d646dbba8        5 weeks ago         177 MB
ubuntu              latest              113a43faa138        8 weeks ago         81.1 MB
google/cadvisor     latest              75f88e3ec333        7 months ago        62.2 MB
[[email protected] ]# 

2. 测试定制的redis镜像

2. 启动redis-server

[[email protected] ]# docker run -it --name redis -p 6379:6379 redis:1.0.0
1:C 04 Aug 15:57:25.901 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 04 Aug 15:57:25.901 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 04 Aug 15:57:25.901 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ‘‘-._                                   
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 1
  `-._    `-._  `-./  _.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |                                  
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
      `-._    `-.__.-‘    _.-‘                                       
          `-._        _.-‘                                           
              `-.__.-‘                                               

1:M 04 Aug 15:57:25.905 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 04 Aug 15:57:25.905 # Server initialized
1:M 04 Aug 15:57:25.905 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1‘ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1‘ for this to take effect.
1:M 04 Aug 15:57:25.905 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 04 Aug 15:57:25.906 * Ready to accept connections

2.2 查看容器

[[email protected] ]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f7fbb103e28c        redis:1.0.0         "redis-server"           23 seconds ago      Up 22 seconds       0.0.0.0:6379->6379/tcp   redis
5dd13df73ef1        httpd:latest        "httpd-foreground"       4 days ago          Up 4 days           0.0.0.0:32771->80/tcp    httpd
457815a05858        google/cadvisor     "/usr/bin/cadvisor..."   8 days ago          Up 8 days           0.0.0.0:8080->8080/tcp   vigilant_galileo
7afd18cf46b8        registry            "/entrypoint.sh /e..."   3 weeks ago         Up 3 weeks          0.0.0.0:5000->5000/tcp   registry
[[email protected] ]# 

2.3 启动redis-cli

[[email protected] liudong]# docker exec -it f7fbb103e28c redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name dog
OK
127.0.0.1:6379> get dog
(nil)
127.0.0.1:6379> get name
"dog"
127.0.0.1:6379> 

以上是关于Dockfile定制镜像的主要内容,如果未能解决你的问题,请参考以下文章

Dockfile的详解

Dockfile打包带tdengine驱动的tomcat自定义镜像

docker安装nginx及使用dockfile创建镜像和使用

使用 Dockerfile 构建镜像

如何使用Dockerfile构建镜像

docker学习--Dockfile详解