Ubuntu Docker (Ubuntu 16.04 64位)

前端之家收集整理的这篇文章主要介绍了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,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$

查看容器内的标准输出: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配置文件

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,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,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,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资源

Docker官方英文资源

Docker中文资源

其它资源

原文链接:https://www.f2er.com/ubuntu/351876.html

猜你在找的Ubuntu相关文章