docker--常用命令

一.docker信息查看

# 查看docker版本
docker version
# 显示docker系统的信息 docker info
# 日志信息 docker logs
# 故障检查 service docker status
# 启动关闭docker sudo service docker start|stop

二.常见镜像容器操作 

#查看容器日志
docker logs -f 容器名或id

#拉取镜像
docker pull 镜像名(如:centos:lasted)

#查看镜像
docker images

#删除镜像(-f 强制删除)
docker rmi 镜像名或id

#删除本地所有镜像
docker rmi `docker images -a -q`

#查看容器(-a 查看所有容器)

docker ps -a -q
    -a : 所有容器
    -q : 所有信息

#删除容器(-f 强制删除)
docker container rm 容器名或容器id

三.docker run 命令

#创建并后台启动一个容器名为test,使用centos:7镜像

docker run -d -p 8888:8080 --name test --privileged -it centos:7 /usr/sbin/init
    -d : 后台运行
    -p : 端口映射 宿主机的8888端口映射到容器的8080端口(可以同时映射多个)
    --name : 为容器另起名
    --privileged:为了解决进入centos系统后使用systemctl启动服务报错

run命令详解:

-a, --attach=[]            Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
  --add-host=[]              Add a custom host-to-IP mapping (host:ip)
  --blkio-weight=0            Block IO (relative weight), between 10 and 1000
  -c, --cpu-shares=0          CPU shares (relative weight)
  --cap-add=[]                Add Linux capabilities
  --cap-drop=[]              Drop Linux capabilities
  --cgroup-parent=            Optional parent cgroup for the container
  --cidfile=                  Write the container ID to the file
  --cpu-period=0              Limit CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0              Limit the CPU CFS quota
  --cpuset-cpus=              CPUs in which to allow execution (0-3, 0,1) 绑定容器到指定CPU运行
  --cpuset-mems=              MEMs in which to allow execution (0-3, 0,1) 绑定容器到指定MEM运行
  -d, --detach=false          Run container in background and print container ID 后台运行容器,并返回容器ID
  --device=[]                Add a host device to the container
  --dns=[]                    Set custom DNS servers 指定容器使用的DNS服务器,默认和宿主一致
  --dns-search=[]            Set custom DNS search domains 指定容器DNS搜索域名,默认和宿主一致
  -e, --env=[]                Set environment variables 设置环境变量
  --entrypoint=              Overwrite the default ENTRYPOINT of the image
  --env-file=[]              Read in a file of environment variables 从指定文件读入环境变量
  --expose=[]                Expose a port or a range of ports
  -h, --hostname=            Container host name 指定容器的hostname
  --help=false                Print usage
  -i, --interactive=false    Keep STDIN open even if not attached 以交互模式运行容器,通常与 -t 同时使用
  --ipc=                      IPC namespace to use
  -l, --label=[]              Set meta data on a container
  --label-file=[]            Read in a line delimited file of labels
  --link=[]                  Add link to another container
  --log-driver=              Logging driver for container
  --log-opt=[]                Log driver options
  --lxc-conf=[]              Add custom lxc options
  -m, --memory=              Memory limit
  --mac-address=              Container MAC address (e.g. 92:d0:c6:0a:29:33)
  --memory-swap=              Total memory (memory + swap), '-1' to disable swap
  --name=                    Assign a name to the container 为容器指定一个名称
  --net=bridge                Set the Network mode for the container  指定容器的网络连接类型,支持 bridge/host/none/container:<name|id> 四种类型
  --oom-kill-disable=false    Disable OOM Killer
  -P, --publish-all=false    Publish all exposed ports to random ports
  -p, --publish=[]            Publish a container's port(s) to the host
  --pid=                      PID namespace to use
  --privileged=false          Give extended privileges to this container
  --read-only=false          Mount the container's root filesystem as read only
  --restart=no                Restart policy to apply when a container exits
  --rm=false                  Automatically remove the container when it exits
  --security-opt=[]          Security Options
  --sig-proxy=true            Proxy received signals to the process
  -t, --tty=false            Allocate a pseudo-TTY 为容器重新分配一个伪输入终端,通常与 -i 同时使用
  -u, --user=                Username or UID (format: <name|uid>[:<group|gid>])
  --ulimit=[]                Ulimit options
  --uts=                      UTS namespace to use
  -v, --volume=[]            Bind mount a volume
  --volumes-from=[]          Mount volumes from the specified container(s)
  -w, --workdir=              Working directory inside the container

四.常用操作

进入运行中的容器中
docker exec -it test /bin/bash

#从宿主机拷贝文件到容器中
docker cp 宿主机文件路径 容器名:路径(例:docker cp ./test.sh test:/home/)

#从容器拷贝文件到宿主机
docker cp
容器名:路径(例:docker cp ./test.sh test:/home/) 宿主机路径

#停止运行中的容器
docker stop 容器名

#开始运行容器
docker start 容器名

#查看所有容器的ip
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

五.镜像和容器的导入和导出

原文地址:https://www.cnblogs.com/mswei/p/10365407.html