Category: Docker

  • docker build Could not resolve host

    在进行 docker build 时,遇到

    Could not resolve host: mirrors.aliyun.com; Unknown error

    解决办法

    docker build --network=host --no-cache  .
  • Dockerfile

    CMD

    CMD 是 docker 启动时运行的命令,在 Dockerfile 中只能指定一条 CMD 命令

    EXPOSE

    暴露端口

  • docker 镜像

    查看本地 docker 镜像

    docker images

    查看 repositories

    curl -XGET http://127.0.0.1:5000/v2/_catalog

    查找 registry 中的镜像

    docker search ansible

    删除所有镜像

    docker rmi `docker images -a -q`
  • 运行docker容器

    运行一个 centos 容器
    The -i and -t flags can be passed to the exec command to keep an interactive shell open, and you can specify the shell you want to attach after the container ID.

    [root@localhost ~]# docker run  -i -t centos /bin/bash
    Unable to find image 'centos:latest' locally
    latest: Pulling from library/centos
    8d30e94188e7: Pull complete 
    Digest: sha256:2ae0d2c881c7123870114fb9cc7afabd1e31f9888dac8286884f6cf59373ed9b
    Status: Downloaded newer image for centos:latest
    

    运行 docker 以 daemon 方式运行,host 开放端口 5001,对应容器内端口 5000
    The -p means “publish ports”

    docker run -d -p 5001:5000 python-flask
    

    查看信息

    docker info
    Containers: 1
     Running: 0
     Paused: 0
     Stopped: 1
    Images: 1
    

    查看容器列表

    docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    ebf5f9bf4bc9        centos              "/bin/bash"         16 minutes ago      Exited (0) 7 minutes ago                       furious_leakey
    

    查看 docker 运行的 log

    docker logs  furious_leakey

    探测 docker 更详尽信息,json 输出

    docker inspect furious_leakey

    退出后的容器是 stopped 状态,现在根据名字 furious_leakey 启动它

    docker start furious_leakey

    通过 shell 进入到容器中

    docker attach furious_leakey

    上面运行的容器是交互式容器

    docker工作目录在

    /var/lib/docker/

    容器存放在

    /var/lib/docker/containers

    删除所有 docker 容器

    docker rm `docker ps -a -q`

    进入到运行中的 docker

    docker exec -i -t 82d98bdf2d77046ce52a61f2bab27465db868a97faebe94a83581fc0322e4c26 /bin/bash

    docker ps 只显示容器名

    docker ps -a --format '{{ .Names }}'
    

    docker 及对应的所有 ip

    docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}} {{.IPAddress}} {{end}}' $(docker ps -aq)
    
  • centos7 docker 安装

    在 centos7 下,安装

    curl -s https://get.docker.com/ | sh

    启动并设置开机启动

    systemctl start docker.service
    systemctl enable docker.service
    

    检查 docker 状态

    docker info
    systemctl status docker
  • docker使用

    pull

    docker pull centos

    build

    docker build -t jpuyy.com:master --rm=true .

    对已有 image 再打一个 tag

    docker tag jpuyy.com:master notexit.com:master

    推到远端

    docker push $REPO:$VER

    复制文件到 docker 里

    docker cp page.html elegant_noether:/user/local/apache2/htdocs

    查看端口映射

    docker port 10936519db14

    找出一批 docker 容器在系统里的 pid

    docker ps -a | grep -i demo1  | awk '{print $1}' | xargs docker inspect --format '{{.State.Pid}} {{.Name}}'
    

    docker 修剪 清理 https://docs.docker.com/config/pruning/

    docker system prune