標籤:redis ubuntu docker putty linux
Ubuntu下用docker安裝redis鏡像和使用redis容器分享
1. 安裝Ubuntu2. 用Putty登入Ubuntu
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-40-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Wed Dec 31 06:41:16 UTC 2014
System load: 0.01 Processes: 228
Usage of /: 6.0% of 28.80GB Users logged in: 0
Memory usage: 11% IP address for eth0: 10.205.178.22
Swap usage: 0% IP address for docker0: 172.17.42.1
Graph this data and manage this system at:
https://landscape.canonical.com/
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
3. 拉取redis鏡像
[email protected]:~# sudo docker pull redis:latest
redis:latest: The image you are pulling has been verified
eee46bd361c1: Pull complete
ff8650f588b2: Pull complete
130985f77ca0: Pull complete
4d81fff38a25: Pull complete
e6d98faa32e2: Pull complete
95d3849978c3: Pull complete
263f96794544: Pull complete
1ed9b7611cf5: Pull complete
451742990a7f: Pull complete
511136ea3c5a: Already exists
f10807909bc5: Already exists
f6fab3b798be: Already exists
1e6ac0ffed3b: Already exists
62ff5003ac9a: Already exists
e49d349e8a75: Already exists
61213f5a1710: Already exists
9feca322d1c7: Already exists
1aa8ce669b93: Already exists
Status: Downloaded newer image for redis:latest
[email protected]:~# sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
redis latest 451742990a7f 11 days ago 111.2 MB
4. 啟動redis容器
[email protected]:~# sudo docker run -t -i redis:latest
[1] 31 Dec 02:56:57.870 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
_._
_.-``__ ‘‘-._
_.-`` `. `_. ‘‘-._ Redis 2.8.19 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ‘‘-._
( ‘ , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|‘` _.-‘| Port: 6379
| `-._ `._ / _.-‘ | PID: 1
`-._ `-._ `-./ _.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ | http://redis.io
`-._ `-._`-.__.-‘_.-‘ _.-‘
|`-._`-._ `-.__.-‘ _.-‘_.-‘|
| `-._`-._ _.-‘_.-‘ |
`-._ `-._`-.__.-‘_.-‘ _.-‘
`-._ `-.__.-‘ _.-‘
`-._ _.-‘
`-.__.-‘
[1] 31 Dec 02:56:57.890 # Server started, Redis version 2.8.19
[1] 31 Dec 02:56:57.890 # 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] 31 Dec 02:56:57.891 # 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] 31 Dec 02:56:57.891 # 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] 31 Dec 02:56:57.891 * The server is now ready to accept connections on port 6379
5.Redis容器啟動了,那接下來怎麼辦?
辦法:把putty關了,然後從新進來putty
6. 怎麼進入容器呢?
先安裝NSenter:
cd /tmp; curl http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/pub/linux/utils/util-linux/v2.25/util-linux-2.25.tar.gz | tar -zxf-; cd util-linux-2.25;
sudo apt-get install build-essential
sudo apt-get make
./configure --without-ncurses
make nsenter && sudo cp nsenter /usr/local/bin
方法一:
PID=$(docker inspect --format "{{ .State.Pid }}" <container>)
nsenter --target $PID --mount --uts --ipc --net --pid
方法二:
安裝指令碼(指令碼參照:https://github.com/jpetazzo/nsenter/blob/master/docker-enter)
wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker;
echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc
最後,調用docker-enter進入容器:
[email protected]:/tmp# docker-enter b430d6f4ff00 ls
dirname: invalid option -- ‘s‘
Try ‘dirname --help‘ for more information.
bin boot data dev entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin selinux srv sys tmp usr var
[email protected]:~#
註:這裡大家看到有錯誤,是因為指令碼中用到$(dirname "$0""), 大家可以直接echo "$0",我這邊顯示的是-su
7. 測試redis命令
進入容器後可以用redis-cli ping測試可以串連上本機剛才啟動的redis-server不,返回PONG說明串連成功。
直接按redis-cli進入redis 命令測試下可以用不:
[email protected]:~# redis-cli ping
PONG
[email protected]:~# redis-cli
127.0.0.1:6379> set myname jumping
OK
127.0.0.1:6379> get myname
"jumping"
127.0.0.1:6379>
8. 這樣就簡單的把redis啟動起來了,要用起來大家還要把連接埠影射到宿主機器上,然後可以通過用戶端來調用Redis:http://redis.io/clients
Ubuntu下用docker安裝redis鏡像和使用redis容器分享