我的Docker学习笔记
2014年12月13日

Docker学习资料:

官方:http://docs.docker.com/userguide/

中文:http://yeasy.gitbooks.io/docker_practice/content/introduction/what.html


docker compose 连接外部容器

方法一:将docker compose使用的默认网络指定为外部容器的网络:

docker-compose.yml文件如下:

version: '2'
services:
  kafka:
    image: wurstmeister/kafka:2.12-2.3.0
    ports:
      - "9092"
    environment:
      DOCKER_API_VERSION: 1.22
      KAFKA_ADVERTISED_HOST_NAME: 192.168.66.11
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
networks:
  default:
    external:
      name: zookeeper

这里例子中,指定了外部的名为“zookeeper”的网络。

提示:关于怎么创建 docker 自定义网络,参见docker官方文档。

方法二:扩展docker compose的网络,加入外部网络进来:

docker-compose.yml文件如下:

version: '2'
services:
  kafka:
    image: wurstmeister/kafka:2.12-2.3.0
    networks: 
      - default
      - zookeeper
    ports:
      - "9092"
    environment:
      DOCKER_API_VERSION: 1.22
      KAFKA_ADVERTISED_HOST_NAME: 192.168.66.11
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
networks:
  zookeeper:
    external: true

这里例子中,声明了名为“zookeeper”的外部网络,并在kafka中把它和default网络连接起来。


docker-compose在配置和环境变量中获取docker ip

需要Dockerfile支持,以下方法仅供参考:

先像这样配置

environment:
  KAFKA_ADVERTISED_HOST_NAME: 192.168.66.11
  PORT_COMMAND: "docker port $$(hostname) 9092/tcp | cut -d: -f2"
  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:_{PORT_COMMAND}

然后在Dockerfile的 Entrypoint脚本中执行 PORT_COMMAND,得到PORT_VALUE,然后把 _{PORT_COMMAND} 替换成PORT_VALUE。如下示例:

# Store original IFS config, so we can restore it at various stages 
ORIG_IFS=$IFS
if [[ -n "$PORT_COMMAND" ]]; then
    PORT_VALUE=$(eval "$PORT_COMMAND")

    # Replace any occurences of _{PORT_COMMAND} with the value
    IFS=$'\n'
    for VAR in $(env); do
        if [[ "$VAR" =~ "_{PORT_COMMAND}" ]]; then
	    eval "export ${VAR//_\{PORT_COMMAND\}/$PORT_VALUE}"
        fi
    done
    IFS=$ORIG_IFS
fi



一、Ubuntu下安装docker
安装教程:http://docs.docker.com/installation/#installation

系统安装好后
vi /etc/environment
追加下面两行
export http_proxy="http://172.17.18.84:8080"
export https_proxy="https://172.17.18.84:8080"
保存
sudo apt-get install docker.io(等待执行完成,装完后网络会因冲突中断)
sudo service docker.io stop
ip link del docker0 (网络恢复)
vim /etc/default/docker.io
追加下面两行
DOCKER_OPTS="--bip=10.0.42.1/20"
export http_proxy="http://172.17.18.84:8080"
保存
sudo service docker.io start
————————————————————————————————————————————————————
安装nsenter
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
验证安装是否成功
which nsenter
which docker-enter
—————————————————————————————————————————————————————


二、Docker基本操作

例如:

sudo docker run ...

$ sudo docker stop insane_babbage


Commands:
    attach    Attach to a running container
    build     Build an image from a Dockerfile
    commit    Create a new image from a container's changes
    cp        Copy files/folders from the containers filesystem to the host path
    diff      Inspect changes on a container's filesystem
    events    Get real time events from the server
    export    Stream the contents of a container as a tar archive
    history   Show the history of an image
    images    List images
    import    Create a new filesystem image from the contents of a tarball
    info      Display system-wide information
    inspect   Return low-level information on a container
    kill      Kill a running container
    load      Load an image from a tar archive
    login     Register or Login to the docker registry server
    logs      Fetch the logs of a container
    port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
    pause     Pause all processes within a container
    ps        List containers
    pull      Pull an image or a repository from the docker registry server
    push      Push an image or a repository to the docker registry server
    restart   Restart a running container
    rm        Remove one or more containers
    rmi       Remove one or more images
    run       Run a command in a new container
    save      Save an image to a tar archive
    search    Search for an image in the docker index
    start     Start a stopped container
    stop      Stop a running container
    tag       Tag an image into a repository
    top       Lookup the running processes of a container
    unpause   Unpause a paused container
    version   Show the docker version information
    wait      Block until a container stops, then print its exit code

1、保持bash运行容器
$ sudo docker run -t -i ubuntu:14.04 /bin/bash
The -t flag assigns a pseudo-tty or terminal inside our new container and the -i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.

退出:
root@af8bae53bdd3:/# exit
2、以守护进程运行
A Daemonized Hello world
$ sudo docker run -d ubuntu:14.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

$ sudo docker ps
CONTAINER ID  IMAGE         COMMAND               CREATED        STATUS       PORTS NAMES
1e5535038e28  ubuntu:14.04  /bin/sh -c 'while tr  2 minutes ago  Up 1 minute        insane_babbage

$ sudo docker ps -l

查看输出
$ sudo docker logs insane_babbage

停止容器
$ sudo docker stop insane_babbage

查看指令的帮助:
例如:$ sudo docker attach --help

命令详细说明和例子:
http://docs.docker.com/reference/commandline/cli/