美文网首页
[ Docker ] 常用命令

[ Docker ] 常用命令

作者: 爱上落入尘世间的你 | 来源:发表于2019-07-11 13:07 被阅读0次

    Reference: https://docker_practice.gitee.io/image/commit.html

    镜像

    # list local images
    docker image ls
    docker image ls filter_name
    docker image ls nginx
    
    # pull a image to local
    docker pull library_name/image_name:tag_name
    docker pull ubuntu:18.04
    #equal with
    docker pull library/ubuntu:18.04
    
    # show space used by docker images, containers, volumes and caches
    docker system df
    
    # show dangling  images(虚悬镜像)
    # docker image ls -f dangling=true
    
    # clean all dangling images
    docker prune
    
    # remove a image
    docker image rm image_identifier(name, tag, full id, or short id)
    
    # save a container and its change as a image
    docker commit --author author_info --message image_message container_name image_name:tag_name 
    docker commit --author "twesix" --message "alter index.html" webserver nginx:1.1
    
    # show histories of image
    docker history image_name:tag_name
    

    容器

    # list containers
    # -a list all containers(include stoped containers)
    docker container ls [-a]
    
    # launch a container from image
    # --name container_name
    # -d run in daemon mode
    # -i open stdin
    # -t allocate a pseudo-tty
    # -p manual port map, host-port:container-port
    # -P automatically port map
    docker run image_name:tag_name command_name
    docker run -p host-port:container-port image-name command-name
    
    # launch a stoped container
    docker container start container_identifier
    
    # stop the container
    docker container stop id_or_name
    
    # show logs of container
    docker container logs container_id_or_names
    
    # interact with a running container
    # container will stop when exit the interact shell, not recommended
    docker attach id_or_name
    
    # interact with a running container
    # -i same as docker run
    # -t same as docker run
    docker exec name_or_id command_name
    
    # remove a container
    # -f force to remove even the container is running
    docker container rm id_or_name
    
    # clean all the stoped containers
    docker container prune
    
    

    数据卷

    # create a volume
    docker volume create volume-name
    
    # show all volumes
    docker volume ls
    
    # show detail of volume
    docker volume inspect volume-name
    
    # mount a data volume when starting a container
    docker run -v volume-name:/path/to/mount/dir image-name command-name
    # or 
    docker run --mount source=volume-name, target=/path/to/mount/dir image-name command-name
    
    #mount a host dir as data volume
    docker run \
    # -v /host/dir:/mount/dir \
    # or
    --mount type=bind, source=/host/dir, target=/path/to/mount/dir, readonly \
    image-name command-name
    
    # remove a volume
    docker volume rm volume-name
    
    # clean the unnamed and unused volumes
    docker volume prune
    

    网络

    # port mapping format: host-ip:host-port:container-ip:container-port/prorocal
    # if port is omitted, it will be random allocated
    
    # map host port to container port
    # -P automatically map random ports of host to all the opened ports of container
    # -p map a port of host to a port of container
    docker run [-P | -p <mapping format>] image_name command_name
    
    # show binding details of the container's port
    docker port container-name container-port
    
    # create a new docker bridge network
    docker network create -d bridge network-name
    
    # use designated bridge as the network bridge when starting the container
    docker run -b bridge-name image-name
    

    相关文章

      网友评论

          本文标题:[ Docker ] 常用命令

          本文链接:https://www.haomeiwen.com/subject/lxwrkctx.html