docker 安装与启动
构建 docker 镜像:
docker pull centos:laster
慢慢的等待镜像文件的下载。下载完可以通过如下命令查看镜像
docker images //查看镜像信息
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB
通过 docker run 来启动镜像,同时会创建一个容器,看下 docker run 的启动命令:
[root@localhost /]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
-a, --attach=[] Attach to STDIN, STDOUT or STDERR
--add-host=[] Add a custom host-to-IP mapping (host:ip)
--blkio-weight Block IO (relative weight), between 10 and 1000
--blkio-weight-device=[] Block IO weight (relative device weight)
--cpu-shares 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 Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota Limit CPU CFS (Completely Fair Scheduler) quota
--cpuset-cpus CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys Override the key sequence for detaching a container
--device=[] Add a host device to the container
--device-read-bps=[] Limit read rate (bytes per second) from a device
--device-read-iops=[] Limit read rate (IO per second) from a device
--device-write-bps=[] Limit write rate (bytes per second) to a device
--device-write-iops=[] Limit write rate (IO per second) to a device
--disable-content-trust=true Skip image verification
--dns=[] Set custom DNS servers
--dns-opt=[] Set DNS options
--dns-search=[] Set custom DNS search domains
-e, --env=[] 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 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
--group-add=[] Add additional groups to join
-h, --hostname Container host name
--help Print usage
-i, --interactive Keep STDIN open even if not attached
--ip Container IPv4 address (e.g. 172.30.100.104)
--ip6 Container IPv6 address (e.g. 2001:db8::33)
--ipc IPC namespace to use
--isolation Container isolation level
--kernel-memory Kernel memory limit
-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
-m, --memory Memory limit
--mac-address Container MAC address (e.g. 92:d0:c6:0a:29:33)
--memory-reservation Memory soft limit
--memory-swap Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness=-1 Tune container memory swappiness (0 to 100)
--name Assign a name to the container
--net=default Connect a container to a network
--net-alias=[] Add network-scoped alias for the container
--oom-kill-disable Disable OOM Killer
--oom-score-adj Tune host's OOM preferences (-1000 to 1000)
-P, --publish-all Publish all exposed ports to random ports
-p, --publish=[] Publish a container's port(s) to the host
--pid PID namespace to use
--privileged Give extended privileges to this container
--read-only Mount the container's root filesystem as read only
--restart=no Restart policy to apply when a container exits
--rm Automatically remove the container when it exits
--security-opt=[] Security Options
--shm-size Size of /dev/shm, default value is 64MB
--sig-proxy=true Proxy received signals to the process
--stop-signal=SIGTERM Signal to stop a container, SIGTERM by default
--sysctl=map[] Sysctl options
-t, --tty Allocate a pseudo-TTY
--tmpfs=[] Mount a tmpfs directory
-u, --user Username or UID (format: <name|uid>[:<group|gid>])
--ulimit=[] Ulimit options
--uts UTS namespace to use
-v, --volume=[] Bind mount a volume
--volume-driver Optional volume driver for the container
--volumes-from=[] Mount volumes from the specified container(s)
-w, --workdir Working directory inside the container
启动并创建一个交互式的 docker 容器:
[root@localhost /]# docker run -ti -d 686672a1d0cc
//-d 为后台启动
通过 docker ps 来查看当前运行的容器,看下 docker ps 的相关指令:
[root@localhost /]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
-a, --all Show all containers (default shows just running)
-f, --filter=[] Filter output based on conditions provided
--format Pretty-print containers using a Go template
--help Print usage
-l, --latest Show the latest created container (includes all states)
-n=-1 Show n last created containers (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
[root@localhost /]# docker ps//查看当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d95b3df8674 686672a1d0cc "/bin/bash" 5 hours ago Up About an hour admiring_kowalevski
[root@localhost /]# docker ps -a//查看所有的容器信息
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d95b3df8674 686672a1d0cc "/bin/bash" 5 hours ago Up About an hour admiring_kowalevski
1b4ac575eda0 686672a1d0cc "/bin/bash" 23 hours ago Exited (137) About an hour ago elated_turing
通过 docker start,docker stop 来启动和停止容器:
[root@localhost /]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys Override the key sequence for detaching a container
--help Print usage
-i, --interactive Attach container's STDIN
[root@localhost /]# docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop a running container.
Sending SIGTERM and then SIGKILL after a grace period
--help Print usage
-t, --time=10 Seconds to wait for stop before killing it
[root@localhost /]# docker restart --help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart a container
--help Print usage
-t, --time=10 Seconds to wait for stop before killing the container
使用 docker exec 可以进入到已经启动的容器中,低版本的 docker 可能不行。
[root@localhost Desktop]# docker exec -ti 7d95b3df8674 /bin/bash
[root@7d95b3df8674 /]#
容器的工作是建立在镜像的基础之上的,如果需要删除镜像的话,需要先删除使用该镜像的容器,然后才能删除镜像,否则删除镜像的时候,会有如下的类似的错误信息提示:Failed to remove image
(e7b): Error response from daemon: conflict: unable to delete e7b2de517efa (must be forced) - image is being used by stopped container 4fbc3cd00987,可以通过 docker rm 删除容器,docker rmi 删除镜像。
[root@localhost /]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
-f, --force Force the removal of a running container (uses SIGKILL)
--help Print usage
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
[root@localhost /]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
-f, --force Force removal of the image
--help Print usage
--no-prune Do not delete untagged parents
删除停止的容器
docker rm $(docker ps --all -q -f status=exited)
删除没有使用的镜像
docker rmi -f (docker images | grep "<none>" | awk "{print 3}")
批量删除容器
docker ps -a | awk '{print $1}' | xargs docker rm
批量删除镜像
docker images | awk '{print $3}' | xargs docker rmi
持久化容器与镜像
1.通过容器生成新的镜像
运行中的镜像称为容器。你可以修改容器(比如删除一个文件),但这些修改不会影响到镜像。不过,你使用 docker commit 命令可以把一个正在运行的容器变成一个新的镜像。
docker commit [repo:tag] 将一个 container 固化为一个新的 image,后面的 repo:tag 可选。
2.持久化容器
docker export 用于持久化容器。
[root@localhost /]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
--help Print usage
-o, --output Write to a file, instead of STDOUT
[root@localhost /]# docker export <CONTAINER ID> container.tar
[](()3.持久化镜像
======================================================================
docker save 用于持久化镜像:
[root@localhost /]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save an image(s) to a tar archive (streamed to STDOUT by default)
--help Print usage
-o, --output Write to a file, instead of STDOUT
[root@localhost /]# docker save <CONTAINER ID> image.tar
[](()4.导入持久化镜像,容器:
============================================================================
使用 docker import,docker load 导入镜像容器:
[root@localhost /]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
-c, --change=[] Apply Dockerfile instruction to the created image
--help Print usage
-m, --message Set commit message for imported image
[root@localhost /]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
--help Print usage
-i, --input Read from a tar archive file, instead of STDIN
5.对镜像打 tag
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 7.2.1511 686672a1d0cc 5 weeks ago 194.6 MB
评论