Docker入门篇一

/ 入门docker技术 / 没有评论 / 2604浏览

alt

什么是Docker?

Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机)bare metalOpenStack 集群和其他的基础应用平台。

Docker通常用于如下场景:

关于docker入门教程

准备开始

Docker系统有两个程序:docker服务端和docker客户端。其中docker服务端是一个服务进程,管理着所有的容器。docker客户端则扮演着docker服务端的远程控制器,可以用来控制docker的服务端进程。大部分情况下,docker服务端和客户端运行在一台机器上。

  1. 注册docker账号
https://store.docker.com/signup?next=%2F%3Fref%3Dlogin
  1. 登录docker账号下载docker.dmg进行安装地址如下
https://store.docker.com/editions/community/docker-ce-desktop-mac
  1. 安装完成后验证在终端敲入一下命令进行验证
#查看版本号
docker version
#查看其它更多命令
docker

搜索可用docker镜像

使用docker最简单的方式莫过于从现有的容器镜像开始。Docker官方网站专门有一个页面来存储所有可用的镜像,网址是:index.docker.io。你可以通过浏览这个网页来查找你想要使用的镜像,或者使用命令行的工具来检索。

  1. 使用命令检索镜像
➜  ~ docker search influxdb
NAME                 DESCRIPTION                                    STARS                OFFICIAL       AUTOMATED
influxdb             InfluxDB is an open source time series datab…   536                 [OK]                
tutum/influxdb       InfluxDB image - listens in port 8083 (web) …   220                                     [OK]
telegraf             Telegraf is an agent for collecting metrics …   176                 [OK]                
  1. 学会使用docker命令来下载镜像
docker pull influxdb
➜  ~ docker pull influxdb
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/library/influxdb/manifests/latest: unauthorized: incorrect username or password
docker login
Authenticating with existing credentials...
Stored credentials invalid or expired
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (xxxxxx@gmail.com): 
Password: 
➜  ~ docker pull influxdb
Using default tag: latest
latest: Pulling from library/influxdb
55cbf04beb70: Pull complete 
1607093a898c: Pull complete 
9a8ea045c926: Pull complete 
4c8b66fe6495: Pull complete 
9f3c67b9b082: Pull complete 
864cc6881ca8: Pull complete 
c1165c5c85e6: Pull complete 
0b5bd48b7b2b: Pull complete 
Digest: sha256:c9098612611038b6d0daddf1ed89d0144f41124b0feed765c0d31844e7f32e9f
Status: Downloaded newer image for influxdb:latest
docker pull influxdb:1.6-data-alpine

启动一个镜像

docker容器可以理解为在沙盒中运行的进程。这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。

  1. 启动InfluxDB镜像
$ docker run -p 8086:8086 -v /Users/xxxxxx/Documents/dev/company/docker/influxdb/data:/var/lib/influxdb influxdb
$ docker run -p 8086:8086 -v influxdb:/var/lib/influxdb influxdb
  1. 向外暴露的端口 The following ports are important and are used by InfluxDB.
    • 8086 HTTP API port
    • 8083 Administrator interface port, if it is enabled
    • 2003 Graphite support, if it is enabled
  1. 添加配置文件 InfluxDB既能使用命令行中的环境变量来启动InfluxDB,也能通过挂载配置文件来启动

    • 生成一个配置文件
        $ docker run --rm influxdb influxd config > influxdb.conf
    
    • 启动参数添加配置文件
        docker run -d -p 8086:8086 -p 8083:8083 -m 8G \
        -v /home/zhanga/influxdb/conf/influxdb.conf:/etc/influxdb/influxdb.conf:ro \
        -v /home/zhanga/influxdb/data:/var/lib/influxdb \
        -e INFLUXDB_ADMIN_ENABLED=true \
        influxdb -config /etc/influxdb/influxdb.conf
    
  2. 使用命令行变量配置InfluxDB

For environment variables, the format is INFLUXDB_$SECTION_$NAME. All dashes (-) are replaced with underscores (_). If the variable isn’t in a section, then omit that part.

INFLUXDB_REPORTING_DISABLED=true
INFLUXDB_META_DIR=/path/to/metadir
INFLUXDB_DATA_QUERY_LOG_ENABLED=false
  1. 配置InfluxDB Administrator Interface The administrator interface is deprecated as of 1.1.0 and will be removed in 1.3.0. It is disabled by default. If needed, it can still be enabled by setting an environment variable like below:
docker run -p 8086:8086 -p 8083:8083 \
    -e INFLUXDB_ADMIN_ENABLED=true \
    influxdb
  1. HTTP API 可参考InfluxDB相关文档 Creating a DB named mydb:
$ curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"

Inserting into the DB: Write a point to the database mydb with a timestamp in seconds

$ curl -i -XPOST "http://localhost:8086/write?db=mydb&precision=s" --data-binary 'mymeas,mytag=1 myfield=90 1463683075'

➜  curl -i -XPOST "http://localhost:8086/write?db=mydb&precision=s" --data-binary 'mymeas,mytag=1 myfield=90 1463683075'
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: e4c96103-a528-11e8-8003-000000000000
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.6.1
X-Request-Id: e4c96103-a528-11e8-8003-000000000000
Date: Tue, 21 Aug 2018 09:59:29 GMT

Query Data: Query data with a SELECT statement

$ curl -G 'http://localhost:8086/query?db=mydb' --data-urlencode 'q=SELECT * FROM "mymeas"'
curl -G 'http://192.168.20.232:8086/query?db=mydb' --data-urlencode 'q=SELECT * FROM "mymeas"'

返回结果

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "mymeas",
                    "columns": [
                        "time",
                        "myfield",
                        "mytag"
                    ],
                    "values": [
                        [
                            "2016-05-19T18:37:55Z",
                            90,
                            "1"
                        ],
                        [
                            "2016-05-19T18:37:56Z",
                            91,
                            "2"
                        ]
                    ]
                }
            ]
        }
    ]
}

Docker 参数使用

docker ps

查看当前运行的容器,作用类似于Linux中的ps

docker images

查看当前已经下载的镜像

docker run -d

Run container in background and print container ID

docker stop

Stop one or more running containers

docker stop [OPTIONS] CONTAINER [CONTAINER...]

docker run -m

Memory limit

docker container run

Run a command in a new container

docker container stop

Stop one or more running containers

docker exec

docker exec -it b00c6630464f /bin/bash  

-v参数

把宿主机的目录挂载到docker容器中,比如一些存储数据和配置文件的目录

注意:如果容器中什么都没运行,那么容器启动后会自动结束比如下面这个只含有java8的容器

docker run -it nimmis/java-centos:oracle-8-jre

--net="host"

配置让容器完全使用宿主机的网络,不安全,官方不推荐使用