Ubuntu Docker (Ubuntu 16.04 64位)

前天安装好了Ubuntu 16.04, 感觉还不错。 于是就想安装个docker版的mysql, 搭配python2.7+django+mysql或者python2.7+flask+mysql开发一个demo web。于是就有了本文。

Docker

摘至百度百科
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Ubuntu 16.04下安装Docker

前提条件

Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker。
通过 uname -r 命令查看你当前的内核版本

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

安装完成后, 会出现以下提示:

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时,需要执行 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

参数解释:

  • docker: Docker 的二进制执行文件。
  • run:与前面的 docker 组合来运行一个容器。
  • ubuntu:16.04 指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
  • /bin/echo "Hello world": 在启动的容器里执行的命令

以上命令完整的意思可以解释为:Docker 以 ubuntu15.10 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。

运行交互式容器

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:/# 


参数解释:

  • -t:在新容器内指定一个伪终端或终端。
  • -i:允许你对容器内的标准输入 (STDIN) 进行交互。

此时我们已进入一个 ubuntu16.04系统的容器
我们尝试在容器中运行命令 cat /proc/version和ls分别查看当前系统的版本信息和当前目录下的文件列表

运行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来查看对应的容器发生了什么。

确认容器是否在运行

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:自动分配的容器名称

查看容器内的标准输出:docker logs

在容器内使用docker logs命令,查看容器内的标准输出.
docker logs CONTAINER IDNAMES

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 IDNAMES

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

运行前, 先创建三个文件夹: conf data logs 和conf/my.cnf配置文件

  • data目录将映射为mysql容器配置的数据文件存放路径
  • 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后的默认的配置, 大家可根据需要自行优化。

#
# 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的运行状态

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在宿主机器中的内网ip地址。
使用ifconfig可以看到宿主机器上多了一张docker0的网卡。

jameszhang@jameszhang-SVF15324YCW:/usr/local/mysql$ ifconfig
docker0   Link encap:以太网  硬件地址 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  跃点数:1
          接收数据包:97 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:168 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:0 
          接收字节:7343 (7.3 KB)  发送字节:15563 (15.5 KB)

enp3s0    Link encap:以太网  硬件地址 3c:07:71:65:6a:d3  
          UP BROADCAST MULTICAST  MTU:1500  跃点数:1
          接收数据包:0 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:0 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:0 (0.0 B)  发送字节:0 (0.0 B)

lo        Link encap:本地环回  
          inet 地址:127.0.0.1  掩码:255.0.0.0
          inet6 地址: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  跃点数:1
          接收数据包:9124 错误:0 丢弃:0 过载:0 帧数:0
          发送数据包:9124 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1 
          接收字节:862964 (862.9 KB)  发送字节:862964 (862.9 KB)

wlp2s0    Link encap:以太网  硬件地址 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  跃点数:1
          接收数据包:50950 错误:0 丢弃:0 过载:0 帧数:86029
          发送数据包:52409 错误:0 丢弃:0 过载:0 载波:0
          碰撞:0 发送队列长度:1000 
          接收字节:34877141 (34.8 MB)  发送字节: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

准备

首先,创建目录mongo,用于存放后面的相关东西。

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数据存储目录

使用

使用mongo镜像执行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的驱动程序
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

准备

首先,创建目录redis,用于存放后面的相关东西。

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 :在容器执行redis-server启动命令,并打开redis持久化配置

使用

使用redis镜像执行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驱动程序
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中文资源

其它资源

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • 0. 前言 docker是什么?docker是用GO语言开发的应用容器引擎,基于容器化,沙箱机制的应用部署技术。可...
    sessionboy阅读 3,795评论 2 49
  • 转载自 http://blog.opskumu.com/docker.html 一、Docker 简介 Docke...
    极客圈阅读 10,404评论 0 120
  • Docker — 云时代的程序分发方式 要说最近一年云计算业界有什么大事件?Google Compute Engi...
    ahohoho阅读 15,418评论 15 147
  • 一,小王对于容器的困惑 小王刚开始学习Docker的时候,找资料在网上看到最多的是Docker的好处。比如: 1、...
    架构师小秘圈阅读 8,378评论 0 24
  • 2017年的除夕过得五味杂陈,关于孩子的教育问题,一家人倒是分了诸多派系,以我和孩子爸,孩子爷爷和奶奶这两...
    酉时七若阅读 291评论 1 1