Docker 常用指令

Env:

  • 安裝與啟動

    在安裝 Docker 前先更新一下 YUM Repository:

    # yum -y update

    更新 YUM Repository 後可以安裝 Docker 及其相關套件:

    # yum -y install docker docker-registry

    安裝好 Docker 後, 啟動 Docker 及設定 Docker 開機自動啟動:

    # systemctl enable docker

    # systemctl start docker

    要檢查 Docker 是否正確啟動, 可用 status 選項:

    # systemctl status docker.service

  • 登入 Docker hub to push or commit images

    # docker login

  • Show host IP

ip addr show docker0 | grep -Po 'inet \K[\d.]+'

Build image:

  • Create a file with multiple lines content in Dockerfile

RUN echo $'[repo] \n\
name            = YUM Repository \n\
baseurl         = https://example.com/packages/ \n\
enabled         = 1 \n\
gpgcheck        = 0' > /etc/yum.repos.d/Repo.repoxyz

Build container:

  • Start and attach to container

docker start -ai {{container_id}}
#or start and docker exec 
  • Build and expose port to container

  • docker run -d -p 8080:80 {image}

  • Build and go into container bash

    docker run -t -i ubuntu:12.04 bash

  • Build, expose port and go into container bash

    docker run -itd -p 8080:80 {image} bash

     

    # -p host_port:container_port

About Image:

  • 清除 <none> images

docker rmi $(docker images -f "dangling=true" -q)
  • 更新 image

  • docker commit -m “Setup to Jupyter truly” -a “John” {containerId} {image}

  • Build image

docker build -t jj/image -f Dockerfilexxx .
  • Push to Repository

    docker push {image}

  • Check the files permission in docker image

    docker run –rm -ti –entrypoint sh {image} -c “ls -alF /usr/local/bin/”

About Container:

  • Remove dead container

    • Linux

sudo rm -rf /var/lib/docker/<storage_driver>/{CONTAINER ID}/
    • Windows

del D:\ProgramData\docker\containers\{CONTAINER ID}
del D:\ProgramData\docker\windowsfilter\{CONTAINER ID}
  • Check screen log output

docker logs -f <CONTAINER> --tail {lineNumber}
  • List all the containers

    docker ps -aq

  • Stop all running containers

    docker stop $(docker ps -aq)

  • Remove all containers

    docker rm $(docker ps -aq)

  • Remove all images

    docker rmi $(docker images -q)

  • Copy file from host to container

    docker cp foo.txt mycontainer:/foo.txt

    from container to host

    docker cp mycontainer:/foo.txt foo.txt

  • 進入 Running Container bash

    docker exec -i -t <CONTAINER_ID> bash

    docker exec -i -t <CONTAINER_ID> sh

  • 進入 Stopped(Exited) container bash

docker images   # Find the {imageId} of exited container
docker run -it --entrypoint=sh {imageId}

# not verify? 
docker start -ai {CONTAINER_ID} bash
  • 修改初始 command 並 start new container

    docker run -ti –entrypoint=sh <IMAGE_NAME>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。