1 root@docker-lab:~/redis# ll
2 total 16
3 drwxr-xr-x 2 root root 4096 Feb 26 16:00 ./
4 drwx------ 11 root root 4096 Feb 26 15:59 ../
5 -rw-r--r-- 1 root root 521 Feb 26 15:59 Dockerfile
6 -rw-r--r-- 1 root root 343 Feb 26 15:59 start.sh
7 root@docker-lab:~/redis# cat Dockerfile
8 # Create By www1707@163.com
9
10 FROM centos:7
11
12 COPY start.sh /
13
14 RUN yum clean all && \
15 yum update -y && \
16 yum install -y wget make gcc && \
17 wget http://download.redis.io/releases/redis-5.0.3.tar.gz && \
18 tar xzf redis-5.0.3.tar.gz && \
19 cd redis-5.0.3 && \
20 make && make install && \
21 cp redis.conf /etc/ && \
22 sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /etc/redis.conf && \
23 sed -i 's/protected-mode yes/protected-mode no/' /etc/redis.conf && \
24 cd / && rm -rf redis-5.0.3* && \
25 chmod 755 /start.sh
26
27 CMD ["/start.sh"]
28
29 root@docker-lab:~/redis# cat start.sh
30 #!/bin/bash
31
32 # 修改系统内核参数,在启动容器时必须使用 docker run --privileged ,否则修改的系统内核参数不会生效
33
34 echo 1 > /proc/sys/vm/overcommit_memory && \
35 echo 511 > /proc/sys/net/core/somaxconn && \
36 echo never > /sys/kernel/mm/transparent_hugepage/enabled
37
38 /usr/local/bin/redis-server /etc/redis.conf
39
40 root@docker-lab:~/redis# docker build -t myredis .
41
42 过程省略...
43
44 root@docker-lab:~/redis# docker images
45 REPOSITORY TAG IMAGE ID CREATED SIZE
46 myredis latest faf5b0d8ad98 7 seconds ago 428MB
47 centos 7 1e1148e4cc2c 2 months ago 202MB
48
49 root@docker-lab:~/redis# docker run --privileged --name redis-server -d -p 6379:6379 myredis
50 d1a84ea20ba7f01391d110beccf1f9c8b601516a6a9c9d929cad92ff2499c0e4
51
52 root@docker-lab:~/redis# docker run --name redis-client -it -d myredis
53 f61f87c244c1492f82c91ae9d6ec1e45aadcb73e2a5ab4cc9977c9b1898c3a88
54
55 root@docker-lab:~/redis# docker ps
56 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57 f61f87c244c1 myredis "/start.sh" 6 seconds ago Up 5 seconds redis-client
58 d1a84ea20ba7 myredis "/start.sh" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp redis-server
59
60 root@docker-lab:~/redis# docker inspect redis-server -f '{{.NetworkSettings.IPAddress}}'
61 172.17.0.2
62
63 root@docker-lab:~/redis# docker inspect redis-client -f '{{.NetworkSettings.IPAddress}}'
64 172.17.0.3
65
66 root@docker-lab:~/redis# docker logs redis-server
67 8:C 26 Feb 2019 08:03:04.128 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
68 8:C 26 Feb 2019 08:03:04.128 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8, just started
69 8:C 26 Feb 2019 08:03:04.128 # Configuration loaded
70 _._
71 _.-``__ ''-._
72 _.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
73 .-`` .-```. ```\/ _.,_ ''-._
74 ( ' , .-` | `, ) Running in standalone mode
75 |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
76 | `-._ `._ / _.-' | PID: 8
77 `-._ `-._ `-./ _.-' _.-'
78 |`-._`-._ `-.__.-' _.-'_.-'|
79 | `-._`-._ _.-'_.-' | http://redis.io
80 `-._ `-._`-.__.-'_.-' _.-'
81 |`-._`-._ `-.__.-' _.-'_.-'|
82 | `-._`-._ _.-'_.-' |
83 `-._ `-._`-.__.-'_.-' _.-'
84 `-._ `-.__.-' _.-'
85 `-._ _.-'
86 `-.__.-'
87
88
89
90
91 8:M 26 Feb 2019 08:03:04.129 # Server initialized
92 8:M 26 Feb 2019 08:03:04.129 * Ready to accept connections
93
94 root@docker-lab:~/redis# docker exec -it redis-client /bin/bash
95 [root@f61f87c244c1 /]# redis-cli -h 172.17.0.2
96 172.17.0.2:6379> set foo bar
97 OK
98 172.17.0.2:6379> get foo
99 "bar"
100 172.17.0.2:6379> exit
101 [root@f61f87c244c1 /]# exit
102 exit
1 root@docker-lab:~/mongodb# cat Dockerfile
2 FROM centos:7
3
4 RUN echo '[mongodb-org]' >>/etc/yum.repos.d/mongodb.repo && \
5 echo 'name=MongoDB Repository' >>/etc/yum.repos.d/mongodb.repo && \
6 echo 'baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el$releasever/' >>/etc/yum.repos.d/mongodb.repo && \
7 echo 'gpgcheck=0' >>/etc/yum.repos.d/mongodb.repo && \
8 echo 'enabled=1' >>/etc/yum.repos.d/mongodb.repo && \
9 \
10 yum clean all && \
11 yum install -y initscripts mongodb-org && \
12 yum clean all && \
13 \
14 echo 'mongod soft nofile 64000' >> /etc/security/limits.conf && \
15 echo 'mongod hard nofile 64000' >> /etc/security/limits.conf && \
16 echo 'mongod soft nproc 32000' >> /etc/security/limits.conf && \
17 echo 'mongod hard nproc 32000' >> /etc/security/limits.conf && \
18 \
19 sed -i 's/ bindIp: 127.0.0.1/# bindIp: 127.0.0.1/' /etc/mongod.conf && \
20 \
21 echo '#!/bin/bash' >> /start.sh && \
22 echo 'echo "never" > /sys/kernel/mm/transparent_hugepage/enabled' >> /start.sh && \
23 echo 'echo "never" > /sys/kernel/mm/transparent_hugepage/defrag' >> /start.sh && \
24 echo '/etc/init.d/mongod start ' >> /start.sh && \
25 echo ' ' >>/start.sh && \
26 echo 'while [ -e /var/run/mongodb/mongod.pid ]' >> /start.sh && \
27 echo 'do' >> /start.sh && \
28 echo ' sleep 1' >> /start.sh && \
29 echo 'done' >> /start.sh && \
30 chmod 755 /start.sh
31
32 CMD ["/start.sh"]
33
34 root@docker-lab:~/mongodb# docker build -t mymongo .
35
36 过程省略...
37
38 root@docker-lab:~/mongodb# docker run --name mongo_server -d --privileged -p 27017:27017 mymongo
39 4620a598a3457452d37446e2eeeaf292bbc3d0eca5a2f926695caf5c6da1c01f
40
41 root@docker-lab:~/mongodb# docker run --name mongo_client -d mymongo
42 017d7a8ad77929b939ad72090ad318d0607a590927a156ae4468bfbf99b21499
43
44 root@docker-lab:~/mongodb# docker inspect mongo_server -f '{{.NetworkSettings.IPAddress}}'
45 172.17.0.2
46 root@docker-lab:~/mongodb# docker inspect mongo_client -f '{{.NetworkSettings.IPAddress}}'
47 172.17.0.3
48 root@docker-lab:~/mongodb# docker logs mongo_server
49 Starting mongod: [ OK ]
50 root@docker-lab:~/mongodb# docker exec -it mongo_client /bin/bash
51 [root@017d7a8ad779 /]# mongo --host 172.17.0.2
52 MongoDB shell version: 3.2.22
53 connecting to: 172.17.0.2:27017/test
54 Welcome to the MongoDB shell.
55 For interactive help, type "help".
56 For more comprehensive documentation, see
57 http://docs.mongodb.org/
58 Questions? Try the support group
59 http://groups.google.com/group/mongodb-user
60 > show dbs
61 local 0.000GB
62 > exit
63 bye
64 [root@017d7a8ad779 /]#