前天安裝好了Ubuntu 16.04, 感覺還不錯。 于是就想安裝個docker版的mysql, 搭配python2.7+django+mysql或者python2.7+flask+mysql開發(fā)一個demo web。于是就有了本文。
Docker
摘至百度百科
Docker 是一個開源的應(yīng)用容器引擎,基于 Go 語言 并遵從Apache2.0協(xié)議開源。
Docker 可以讓開發(fā)者打包他們的應(yīng)用以及依賴包到一個輕量級、可移植的容器中,然后發(fā)布到任何流行的 Linux 機器上,也可以實現(xiàn)虛擬化。
容器是完全使用沙箱機制,相互之間不會有任何接口(類似 iPhone 的 app),更重要的是容器性能開銷極低。
Ubuntu 16.04下安裝Docker
前提條件
Docker 要求 Ubuntu 系統(tǒng)的內(nèi)核版本高于 3.10 ,查看本頁面的前提條件來驗證你的 Ubuntu 版本是否支持 Docker。
通過 uname -r 命令查看你當前的內(nèi)核版本
jameszhang@jameszhang-SVF15324YCW:~$ uname -r
4.4.0-31-generic
jameszhang@jameszhang-SVF15324YCW:~$
安裝
獲取最新版本的 Docker 安裝包
jameszhang@jameszhang-SVF15324YCW:~$ sudo wget -qO- https://get.docker.com/ | sh
安裝完成后, 會出現(xiàn)以下提示:
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker jameszhang
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
containers which can be used to obtain root privileges on the
docker host.
Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
for more information.
當要以非root用戶可以直接運行docker時,需要執(zhí)行 sudo usermod -aG docker runoob 命令,然后重新登陸,否則會有如下報錯
jameszhang@jameszhang-SVF15324YCW:~$ sudo usermod -aG docker jameszhang
jameszhang@jameszhang-SVF15324YCW:~$
啟動docker后臺
jameszhang@jameszhang-SVF15324YCW:~$ sudo service docker start
jameszhang@jameszhang-SVF15324YCW:~$ ps -ef | grep docker
root 12290 1 0 22:16 ? 00:00:00 /usr/bin/dockerd -H fd://
root 12302 12290 0 22:16 ? 00:00:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
jameszh+ 13806 3905 0 22:25 pts/4 00:00:00 grep --color=auto docker
jameszhang@jameszhang-SVF15324YCW:~$
運行docker版hello-world
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$
安裝docker版 Ubuntu 16.04
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run ubuntu:16.04 /bin/echo "hello world"
Unable to find image 'ubuntu:16.04' locally
16.04: Pulling from library/ubuntu
e0a742c2abfd: Pull complete
486cb8339a27: Pull complete
dc6f0d824617: Pull complete
4f7a5649a30e: Pull complete
672363445ad2: Pull complete
Digest: sha256:84c334414e2bfdcae99509a6add166bbb4fa4041dc3fa6af08046a66fed3005f
Status: Downloaded newer image for ubuntu:16.04
hello world
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run ubuntu:16.04 /bin/echo "hello world"
hello world
參數(shù)解釋:
- docker: Docker 的二進制執(zhí)行文件。
- run:與前面的 docker 組合來運行一個容器。
- ubuntu:16.04 指定要運行的鏡像,Docker首先從本地主機上查找鏡像是否存在,如果不存在,Docker 就會從鏡像倉庫 Docker Hub 下載公共鏡像。
- /bin/echo "Hello world": 在啟動的容器里執(zhí)行的命令
以上命令完整的意思可以解釋為:Docker 以 ubuntu15.10 鏡像創(chuàng)建一個新容器,然后在容器里執(zhí)行 bin/echo "Hello world",然后輸出結(jié)果。
運行交互式容器
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run -i -t ubuntu:16.04 /bin/bash
root@f85533efc39b:/# cat /proc/version
Linux version 4.4.0-31-generic (buildd@lgw01-16) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2.1) ) #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016
root@f85533efc39b:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@f85533efc39b:/#
參數(shù)解釋:
- -t:在新容器內(nèi)指定一個偽終端或終端。
- -i:允許你對容器內(nèi)的標準輸入 (STDIN) 進行交互。
此時我們已進入一個 ubuntu16.04系統(tǒng)的容器
我們嘗試在容器中運行命令 cat /proc/version和ls分別查看當前系統(tǒng)的版本信息和當前目錄下的文件列表
運行exit命令或者使用CTRL+D來退出容器。
啟動容器(后臺模式)
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker run -d ubuntu:16.04 /bin/sh -c "while true;do echo hello jameszhang; sleep 1; done"
1b9d242777a6a323a1ce67620e889426dc532520e4cb4e4a5bad521b3aa98778
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$
在輸出中,我們沒有看到期望的"hello world",而是一串長字符
1b9d242777a6a323a1ce67620e889426dc532520e4cb4e4a5bad521b3aa98778
這個長字符串叫做容器ID,對每個容器來說都是唯一的,我們可以通過容器ID來查看對應(yīng)的容器發(fā)生了什么。
確認容器是否在運行
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b9d242777a6 ubuntu:16.04 "/bin/sh -c 'while..." About a minute ago Up About a minute jovial_hypatia
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$
- CONTAINER ID:容器ID
- NAMES:自動分配的容器名稱
查看容器內(nèi)的標準輸出:docker logs
在容器內(nèi)使用docker logs命令,查看容器內(nèi)的標準輸出.
docker logs CONTAINER ID或NAMES
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b9d242777a6 ubuntu:16.04 "/bin/sh -c 'while..." 3 minutes ago Up 3 minutes jovial_hypatia
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker logs 1b9d242777a6
hello jameszhang
hello jameszhang
hello jameszhang
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker logs jovial_hypatia
停止容器: docker stop
使用 docker stop 命令來停止容器:
docker stop CONTAINER ID或NAMES
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b9d242777a6 ubuntu:16.04 "/bin/sh -c 'while..." 8 minutes ago Up 8 minutes jovial_hypatia
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker stop jovial_hypatia
jovial_hypatia
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
jameszhang@jameszhang-SVF15324YCW:/usr/local/bin$
安裝Mysql
查找Docker Hub上的mysql鏡像
jameszhang@jameszhang-SVF15324YCW:~$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 4758 [OK]
mariadb MariaDB is a community-developed fork of M... 1464 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 329 [OK]
percona Percona Server is a fork of the MySQL rela... 283 [OK]
hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 63
centurylink/mysql Image containing mysql. Optimized to be li... 52 [OK]
sameersbn/mysql 47 [OK]
tutum/mysql Base docker image to run a MySQL database ... 24
google/mysql MySQL server for Google Compute Engine 18 [OK]
linuxserver/mysql A Mysql container, brought to you by Linux... 10
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
bitnami/mysql Bitnami MySQL Docker Image 6 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 5
autopilotpattern/mysql Implementation of the Autopilot Pattern fo... 4
frodenas/mysql A Docker Image for MySQL 3 [OK]
kuberdock/mysql This is a fork of official MySQL image wit... 1 [OK]
circleci/mysql MySQL is a widely used, open-source relati... 1
jenkler/mysql Docker Mysql package 0
vukor/mysql Build for MySQL. Project available on http... 0 [OK]
tenstartups/mysql 0 [OK]
kardasz/mysql Debian 8, MySQL 5.7 0 [OK]
astronomerio/mysql-sink MySQL sink 0 [OK]
cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]
starkandwayne/mysql 0
jameszhang@jameszhang-SVF15324YCW:~$
拉取官方的鏡像,標簽為5.6
jameszhang@jameszhang-SVF15324YCW:~$ docker pull mysql:5.6
5.6: Pulling from library/mysql
ad74af05f5a2: Pull complete
0639788facc8: Pull complete
de70fa77eb2b: Pull complete
724179e94999: Pull complete
7a61946a7226: Pull complete
fa1f0822fe12: Pull complete
2b2e255eb8e7: Pull complete
38a8b3ee3554: Pull complete
46652a6944cf: Pull complete
0dec4ac74eab: Pull complete
0190940ca68e: Pull complete
Digest: sha256:2897982d4c086b03586a1423d0cbf33688960ef7534b7bb51b9bcfdb6c3597e7
Status: Downloaded newer image for mysql:5.6
jameszhang@jameszhang-SVF15324YCW:~$
運行 docker版mysql
運行前, 先創(chuàng)建三個文件夾: conf data logs 和conf/my.cnf配置文件
- data目錄將映射為mysql容器配置的數(shù)據(jù)文件存放路徑
- logs目錄將映射為mysql容器的日志目錄
- conf目錄里的配置文件將映射為mysql容器的配置文件
- conf/my.cnf 配置文件
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ pwd
/usr/local/mysql
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ ls
conf data logs
添加my.cnf配置
以下為安裝mysql后的默認的配置, 大家可根據(jù)需要自行優(yōu)化。
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
# Here is entries for some specific programs
# The following values assume you have at least 32M ram
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-name-resolve
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options = BACKUP
#max_connections = 100
#table_cache = 64
#thread_concurrency = 10
#
# * Query Cache Configuration
#
query_cache_limit = 1M
query_cache_size = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file = /var/log/mysql/mysql.log
#general_log = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
# other settings you may need to change.
#server-id = 1
#log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#binlog_do_db = include_database_name
#binlog_ignore_db = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
然后, 就可以運行docker版的mysql5.6
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ docker run -p 3306:3306 --name my-mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=12345 -d mysql:5.6
fe39c0d434bbec20d42de236fb252009e7d8cd93e595df6a3f2d65bd004abb23
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$
*** 命令說明 ***
- -p 3306:3306:將容器的3306端口映射到主機的3306端口
- -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:將主機當前目錄下的conf/my.cnf掛載到容器的/etc/mysql/my.cnf
- -v $PWD/logs:/logs:將主機當前目錄下的logs目錄掛載到容器的/logs
- -v $PWD/data:/mysql_data:將主機當前目錄下的data目錄掛載到容器的/mysql_data
- -e MYSQL_ROOT_PASSWORD=123456:初始化root用戶的密碼
- -d 后臺運行my-mysql容器。
查看mysql 5.6的運行狀態(tài)
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d06b9b6ed3ab mysql:5.6 "docker-entrypoint..." 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp my-mysql
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$
使用docker mysql客戶端
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ docker exec -it my-mysql bash
root@7341e5df1f6c:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
mysql>
在宿主機器中連接docker版mysql
首先, 我們得知道docker版mysql在宿主機器中的內(nèi)網(wǎng)ip地址。
使用ifconfig可以看到宿主機器上多了一張docker0的網(wǎng)卡。
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ ifconfig
docker0 Link encap:以太網(wǎng) 硬件地址 02:42:95:88:bf:8f
inet 地址:172.17.0.1 廣播:0.0.0.0 掩碼:255.255.0.0
inet6 地址: fe80::42:95ff:fe88:bf8f/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 躍點數(shù):1
接收數(shù)據(jù)包:97 錯誤:0 丟棄:0 過載:0 幀數(shù):0
發(fā)送數(shù)據(jù)包:168 錯誤:0 丟棄:0 過載:0 載波:0
碰撞:0 發(fā)送隊列長度:0
接收字節(jié):7343 (7.3 KB) 發(fā)送字節(jié):15563 (15.5 KB)
enp3s0 Link encap:以太網(wǎng) 硬件地址 3c:07:71:65:6a:d3
UP BROADCAST MULTICAST MTU:1500 躍點數(shù):1
接收數(shù)據(jù)包:0 錯誤:0 丟棄:0 過載:0 幀數(shù):0
發(fā)送數(shù)據(jù)包:0 錯誤:0 丟棄:0 過載:0 載波:0
碰撞:0 發(fā)送隊列長度:1000
接收字節(jié):0 (0.0 B) 發(fā)送字節(jié):0 (0.0 B)
lo Link encap:本地環(huán)回
inet 地址:127.0.0.1 掩碼:255.0.0.0
inet6 地址: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 躍點數(shù):1
接收數(shù)據(jù)包:9124 錯誤:0 丟棄:0 過載:0 幀數(shù):0
發(fā)送數(shù)據(jù)包:9124 錯誤:0 丟棄:0 過載:0 載波:0
碰撞:0 發(fā)送隊列長度:1
接收字節(jié):862964 (862.9 KB) 發(fā)送字節(jié):862964 (862.9 KB)
wlp2s0 Link encap:以太網(wǎng) 硬件地址 34:23:87:95:98:57
inet 地址:192.168.14.103 廣播:192.168.14.255 掩碼:255.255.255.0
inet6 地址: fe80::1280:3270:38ed:b728/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 躍點數(shù):1
接收數(shù)據(jù)包:50950 錯誤:0 丟棄:0 過載:0 幀數(shù):86029
發(fā)送數(shù)據(jù)包:52409 錯誤:0 丟棄:0 過載:0 載波:0
碰撞:0 發(fā)送隊列長度:1000
接收字節(jié):34877141 (34.8 MB) 發(fā)送字節(jié):8256355 (8.2 MB)
中斷:18
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$
進入到my-mysql容器中查看容器的IP地址:
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ docker exec -it my-mysql bash
root@f4b2d192a08c:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
27: eth0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
root@f4b2d192a08c:/#
在宿主機器上連接my-mysql容器:
jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ mysql -h172.17.0.2 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
mysql>
安裝MongoDB
準備
首先,創(chuàng)建目錄mongo,用于存放后面的相關(guān)東西。
jameszhang@jameszhang-SVF15324YCW:/usr/local$ sudo mkdir -p mongo mongo/db
jameszhang@jameszhang-SVF15324YCW:/usr/local$ ls mongo/
db
jameszhang@jameszhang-SVF15324YCW:/usr/local$
安裝
查找Docker Hub上的mongo鏡像
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker search mongo
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mongo MongoDB document databases provide high av... 3516 [OK]
mongo-express Web-based MongoDB admin interface, written... 175 [OK]
mvertes/alpine-mongo light MongoDB container 52 [OK]
dhermanns/rpi-mongo Mongo 2.6 Database for the Raspberry Pi 1/2 10
jacksoncage/mongo Instant MongoDB sharded cluster 6 [OK]
khezen/mongo MongoDB Docker image supporting RocksDB st... 4 [OK]
ekesken/mongo docker image for mongo that is configurabl... 1 [OK]
xemuliam/mongo Unofiicial MongoDB docker image on Alpine 1 [OK]
19hz/mongo-container Mongodb replicaset for coreos 1 [OK]
kobotoolbox/mongo https://github.com/kobotoolbox/kobo-docker... 1 [OK]
ackee/mongo MongoDB with fixed Bluemix permissions 1 [OK]
cescoferraro/mongo docker alpine mongo 0
circleci/mongo MongoDB document databases provide high av... 0
17media/mongo 0
appsdeck/mongo 0
mygravity/mongo A slow starting mongo instance. Allows the... 0
sscpac/mongo alpine mongo 0 [OK]
os33/go-mongo go and mongo package for testing 0
bigtruedata/mongo Image for MongoDB 0 [OK]
trollin/mongo 0
voidbridge/mongo MongoDB 0 [OK]
buddy/mongo Mongo DB service for Buddy Enterprise. 0
18fgsa/mongo 0
quilt/mongo MongoDB container for quilt.io 0 [OK]
sacashgit/mongo 0
jameszhang@jameszhang-SVF15324YCW:/usr/local$
拉取官方的鏡像,標簽為3.4
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker pull mongo:3.4
3.4: Pulling from library/mongo
5233d9aed181: Pull complete
5bbfc055e8fb: Pull complete
aaf85a329dc4: Pull complete
1360aef7d266: Pull complete
9cb9d47c5d80: Pull complete
80e12bf92c3c: Pull complete
fd3679b936e6: Pull complete
5cb080b90ae5: Pull complete
46cf38664c75: Pull complete
59693a4ecb90: Pull complete
dff9fc3b430d: Pull complete
Digest: sha256:90b78c44a58d6d927f96baabea3212d8c756017846715b630044aefcabcab2eb
Status: Downloaded newer image for mongo:3.4
運行
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker run -p 27017:27017 --name my-mongo -v $PWD/mongo/db:/data/db -d mongo:3.4
fa5ff451a30fe71acc4ddca9190af17c43dc839560dce36218ad15ea336ee512
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fa5ff451a30f mongo:3.4 "docker-entrypoint..." 3 seconds ago Up 2 seconds 0.0.0.0:27017->27017/tcp my-mongo
命令說明
- -p 27017:27017 :將容器的27017 端口映射到主機的27017 端口
- -v $PWD/db:/data/db :將主機中當前目錄下的mongo/db掛載到容器的/data/db,作為mongo數(shù)據(jù)存儲目錄
使用
使用mongo鏡像執(zhí)行mongo 命令連接到剛啟動的容器,主機IP為172.17.0.1
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker run -it mongo:3.4 mongo --host 172.17.0.1
MongoDB shell version v3.4.6
connecting to: mongodb://172.17.0.1:27017/
MongoDB server version: 3.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>
Python2.7連接mongo
使用前, 需安裝python中MongoDB的驅(qū)動程序
pip install pymongo
然后就可以在Python中連接剛剛啟動的my-mongo容器中的MongoDB了。
jameszhang@jameszhang-SVF15324YCW:/usr/local$ ipython
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
Type "copyright", "credits" or "license" for more information.
IPython 5.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: from pymongo import MongoClient
In [2]: conn = MongoClient('172.17.0.1', 27017)
In [3]: db = conn.mydb
In [4]: my_set = db.test_set
In [5]: my_set.insert_one({'name': 'jameszhang', 'age': 29})
Out[5]: <pymongo.results.InsertOneResult at 0x7f2d2c7bb140>
In [6]: my_set.find({'name': 'jameszhang'})
Out[6]: <pymongo.cursor.Cursor at 0x7f2d2c71de50>
In [7]: result = my_set.find({'name': 'jameszhang'})
In [8]: for i in result: print(i)
{u'age': 29, u'_id': ObjectId('59869b18d8fd15791e0dc629'), u'name': u'jameszhang'}
安裝Redis
準備
首先,創(chuàng)建目錄redis,用于存放后面的相關(guān)東西。
jameszhang@jameszhang-SVF15324YCW:/usr/local$ sudo mkdir -p redis redis/data
查找Docker Hub上的redis鏡像
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store th... 4062 [OK]
sameersbn/redis 55 [OK]
bitnami/redis Bitnami Redis Docker Image 52 [OK]
kubeguide/redis-master redis-master with "Hello World!" 18
joshula/redis-sentinel A container for Redis Sentinel 18
tutum/redis Base docker image to run a Redis server 8
johncosta/redis This image was built using the following b... 7
webhippie/redis Docker images for redis 7 [OK]
kubernetes/redis 6
leanlabs/redis 6 [OK]
centos/redis Redis built for CentOS 3 [OK]
gurpartap/redis Smallest redis image at 18.56MB, 5.6MB of ... 3
centurylink/redis 2 [OK]
frodenas/redis A Docker Image for Redis 1 [OK]
dynomitedb/redis Redis backend for DynomiteDB. 1 [OK]
tomesar/redis-arm Redis for ARM! 1 [OK]
whatwedo/redis 0 [OK]
benyoo/redis redis 0 [OK]
anchorfree/redis redis cache server for logging 0
starkandwayne/redis 0
iadvize/redis 0
thedutchselection/redis Redis 0 [OK]
deis/redis 0
scalingo/redis Image of the Redis instances of Scalingo PaaS 0
ajmath/fluentd-redis Use fluentd logs to send docker logs to re... 0 [OK]
安裝
這里我們拉取官方的鏡像,標簽為3.2
jameszhang@jameszhang-SVF15324YCW:/usr/local$ docker pull redis:3.2
3.2: Pulling from library/redis
5233d9aed181: Already exists
ca1b33d3f114: Pull complete
920cdc17d3c2: Pull complete
039bc0a8c4af: Pulling fs layer
039bc0a8c4af: Pull complete
39d721bfb392: Pull complete
853085e403eb: Pull complete
Digest: sha256:848b4fd76a5dacb56988af810a6e86719e313cf4e1186f3d3050384686dbc120
Status: Downloaded newer image for redis:3.2
運行
jameszhang@jameszhang-SVF15324YCW:/usr/local/redis$ docker run -p 6379:6379 -v $PWD/data:/data --name my-redis -d redis:3.2 redis-server --appendonly yes
0a3eff845fdcb85b5b28d02f12b9011ea119e95e4ac88365f2f709a905639362
jameszhang@jameszhang-SVF15324YCW:/usr/local/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0a3eff845fdc redis:3.2 "docker-entrypoint..." 4 seconds ago Up 4 seconds 0.0.0.0:6379->6379/tcp my-redis
命令說明
- -p 6379:6379 :將容器的6379端口映射到主機的6379端口
- -v $PWD/data:/data :將主機中當前目錄下的data掛載到容器的/data
- redis-server --appendonly yes :在容器執(zhí)行redis-server啟動命令,并打開redis持久化配置
使用
使用redis鏡像執(zhí)行redis-cli命令連接到剛啟動的容器,主機IP為172.17.0.1
jameszhang@jameszhang-SVF15324YCW:/usr/local/redis$ docker run -it redis:3.2 redis-cli -h 172.17.0.1
172.17.0.1:6379> info
# Server
redis_version:3.2.10
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:2c3e56d4e4f3b450
redis_mode:standalone
os:Linux 4.4.0-87-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.9.2
process_id:1
run_id:1a0456c40fb27ea69ad84885fff63c8a89666c88
tcp_port:6379
uptime_in_seconds:351
uptime_in_days:0
hz:10
Python2.7 連接Redis
需要安裝Python的Redis驅(qū)動程序
pip install redis
安裝好之后, 就可以來嘗試使用Python來連接Redis了。
jameszhang@jameszhang-SVF15324YCW:/usr/local/redis$ ipython
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
Type "copyright", "credits" or "license" for more information.
IPython 5.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import redis
In [2]: r = redis.Redis(host='172.17.0.1', port=6379, db=0)
In [3]: r.set('hello', 'world')
Out[3]: True
In [4]: r.get('hello')
Out[4]: 'world'
Docker資源
docker大全
docker菜鳥教程: http://www.runoob.com/docker/docker-command-manual.html
docker資源
Docker官方英文資源
- docker官網(wǎng):http://www.docker.com
- Docker windows入門:https://docs.docker.com/windows/
- Docker Linux 入門:https://docs.docker.com/linux/
- Docker mac 入門:https://docs.docker.com/mac/
- Docker 用戶指引:https://docs.docker.com/engine/userguide/
- Docker 官方博客:http://blog.docker.com/
- Docker Hub: https://hub.docker.com/
- Docker開源: https://www.docker.com/open-source
Docker中文資源
- Docker中文網(wǎng)站:http://www.docker.org.cn
- Docker安裝手冊:http://www.docker.org.cn/book/install.html
- 一小時Docker教程 :https://blog.csphere.cn/archives/22
- Docker 從入門到實踐:http://dockerpool.com/static/books/docker_practice/index.html
- Docker中文指南:http://www.widuu.com/chinese_docker/index.html