Maorongrong smile like sunshine

Docker一些简便CLI

2016-05-16

收集Docker一些简便CLI OPEN资讯有很多有用的东西

获取容器id(写脚本时很有用)

# Get the id (-q) of the last (-l) run container
# 获取最后(-l)一个启动的容器id(-q)
$ docker ps -l -q
c8044ab1a3d0
  • 第二种方法:
c1=$(docker run -d --name c1 -P images)
echo ${c1}即为容器的ID

docker inspect可以带格式化的字符串—-Go语言模板作为参数,详细描述所需的数据。写脚本时同时有用:

$ docker inspect -f '' 6f2c42c05500

使用 docker exec来跟运行中的容器进行交互。

  • 获取容器环境变量
$ docker exec -it 6f2c42c05500 env
PATH=/usr/local/sbin:/usr...
HOSTNAME=6f2c42c05500
REDIS_1_PORT=tcp://172.17.0.9:6379
REDIS_1_PORT_6379_TCP=tcp://172.17.0.9:6379
...
  • 镜像构建

通过卷来避免每次运行时都重建镜像, 下面是一个Dockerfile,每次构建时,会拷贝当前目录到容器中。

  1 FROM dockerfile/nodejs:latest
  2
  3 MAINTAINER Anders Janmyr "anders@janmyr.com"
  4 RUN apt-get update && \
  5   apt-get install zlib1g-dev && \
  6   npm install -g pm2 && \
  7   mkdir -p /srv/app
  8
  9 WORKDIR /srv/app
 10 COPY . /srv/app
 11
 12 CMD pm2 start app.js -x -i 1 && pm2 logs
 13

**构建并运行镜像: **

$ docker build -t myapp .
$ docker run -it --rm myapp

data volume container

QQ截图20160413213557.png-17.5kB 1.png-52kB

Understand images, containers, and storage drivers

访问官网docs.docker.com

镜像迁移

The migration tool is provided by Docker, Inc., and runs as a container. You can download it from https://github.com/docker/v1.10-migrator/releases.

Container and layers

2.png-187.2kB

The copy-on-write strategy: Copy-on-write is a similar strategy of sharing and copying. In this strategy, system processes that need the same data share the same instance of that data rather than having their own copy. At some point, if one process needs to modify or write to the data, only then does the operating system make a copy of the data for that process to use. Only the process that needs to write has access to the data copy. All the other processes continue to use the original data. Docker uses a copy-on-write technology with both images and containers. This CoW strategy optimizes both image disk space usage and the performance of container start times.

All image and container layers exist inside the Docker host’s local storage area and are managed by the storage driver. On Linux-based Docker hosts this is usually located under /var/lib/docker/.

Content addressable storage:The new model improves security, provides a built-in way to avoid ID collisions, and guarantees data integrity after pull, push, load, and save operations. It also enables better sharing of layers by allowing many images to freely share their layers even if they didn’t come from the same build.

all versions of Docker still allow images to share layers. For example, If you pull an image that shares some of the same image layers as an image that has already been pulled, the Docker daemon recognizes this, and only pulls the layers it doesn’t already have stored locally. After the second pull, the two images will share any common image layers.


下一篇 面向对象

Comments