赞
踩
回顾刚刚安装docker的时候,测试hello-word:
[root@localhost ~] docker run hello-world Unable to find image 'hello-world:latest' locally #没有这个镜像,需要从外网下载 latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ #下载的真实地址
此时run的流程图大概是这样的
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问。
Docker-Server接收到Docker-Client的指令,就会执行这个命令!
为什么Docker比VM快?
1、Dokcer有着比虚拟机更少的抽象层。由于docker不需要Hypervisor实现硬件资源虚拟化,运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势。
2、docker利用的是宿主机的内核,vm 需要是Guest OS
所以说,新建一个容器的时候,docker 不需要像虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是加载 Guest OS,分钟级别的,而docker 是利用宿主机的操作系统,省略了这个复杂的过程!
docker version 显示docker的版本信息。
docker info 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help 帮助命令
docker version #显示docker的版本信息。
[root@localhost ~] docker version Client: Docker Engine - Community Version: 20.10.8 API version: 1.41 Go version: go1.16.6 Git commit: 3967b7d Built: Fri Jul 30 19:53:39 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.8 API version: 1.41 (minimum version 1.12) Go version: go1.16.6 Git commit: 75249d8 Built: Fri Jul 30 19:52:00 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.9 GitCommit: e25210fe30a0a703442421b0f60afac609f950a3 runc: Version: 1.0.1 GitCommit: v1.0.1-0-g4144b63 docker-init: Version: 0.19.0 GitCommit: de40ad0
docker info #显示docker的系统信息,包括镜像和容器的数量
[root@localhost ~] docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.6.1-docker) scan: Docker Scan (Docker Inc., v0.8.0) Server: Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 1 Server Version: 20.10.8 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc Default Runtime: runc Init Binary: docker-init containerd version: e25210fe30a0a703442421b0f60afac609f950a3 runc version: v1.0.1-0-g4144b63 init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-305.19.1.el8_4.x86_64 Operating System: CentOS Linux 8 OSType: linux Architecture: x86_64 CPUs: 8 Total Memory: 1.748GiB Name: localhost.centos8 ID: YR3E:2WHT:GZIZ:BWI2:UPOO:2BJB:B6TV:QZD7:4GBC:KSIV:KXCJ:OUXY Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
docker 命令名字 --help #帮助命令
[root@localhost ~]docker run --help Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: --add-host list Add a custom host-to-IP mapping (host:ip) -a, --attach list Attach to STDIN, STDOUT or STDERR --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --blkio-weight-device list Block IO weight (relative device weight) (default []) --cap-add list Add Linux capabilities --cap-drop list Drop Linux capabilities --cgroup-parent string Optional parent cgroup for the container --cgroupns string Cgroup namespace to use (host|private) 'host': Run the container in the Docker host's cgroup namespace 'private': Run the container in its own private cgroup namespace '': Use the cgroup namespace as configured by the default-cgroupns-mode option on the daemon (default) --cidfile string Write the container ID to the file --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit CPU real-time period in microseconds --cpu-rt-runtime int Limit CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) -d, --detach Run container in background and print container ID --detach-keys string Override the key sequence for detaching a container --device list Add a host device to the container --device-cgroup-rule list Add a rule to the cgroup allowed devices list --device-read-bps list Limit read rate (bytes per second) from a device (default []) --device-read-iops list Limit read rate (IO per second) from a device (default []) --device-write-bps list Limit write rate (bytes per second) to a device (default []) --device-write-iops list Limit write rate (IO per second) to a device (default []) --disable-content-trust Skip image verification (default true) --dns list Set custom DNS servers --dns-option list Set DNS options --dns-search list Set custom DNS search domains --domainname string Container NIS domain name --entrypoint string Overwrite the default ENTRYPOINT of the image -e, --env list Set environment variables --env-file list Read in a file of environment variables --expose list Expose a port or a range of ports --gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs) --group-add list Add additional groups to join --health-cmd string Command to run to check health --health-interval duration Time between running the check (ms|s|m|h) (default 0s) --health-retries int Consecutive failures needed to report unhealthy --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s) --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s) --help Print usage -h, --hostname string Container host name --init Run an init inside the container that forwards signals and reaps processes -i, --interactive Keep STDIN open even if not attached --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --ipc string IPC mode to use --isolation string Container isolation technology --kernel-memory bytes Kernel memory limit -l, --label list Set meta data on a container --label-file list Read in a line delimited file of labels --link list Add link to another container --link-local-ip list Container IPv4/IPv6 link-local addresses --log-driver string Logging driver for the container --log-opt list Log driver options --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33) -m, --memory bytes Memory limit --memory-reservation bytes Memory soft limit --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1) --mount mount Attach a filesystem mount to the container --name string Assign a name to the container --network network Connect a container to a network --network-alias list Add network-scoped alias for the container --no-healthcheck Disable any container-specified HEALTHCHECK --oom-kill-disable Disable OOM Killer --oom-score-adj int Tune host's OOM preferences (-1000 to 1000) --pid string PID namespace to use --pids-limit int Tune container pids limit (set -1 for unlimited) --platform string Set platform if server is multi-platform capable --privileged Give extended privileges to this container -p, --publish list Publish a container's port(s) to the host -P, --publish-all Publish all exposed ports to random ports --pull string Pull image before running ("always"|"missing"|"never") (default "missing") --read-only Mount the container's root filesystem as read only --restart string Restart policy to apply when a container exits (default "no") --rm Automatically remove the container when it exits --runtime string Runtime to use for this container --security-opt list Security Options --shm-size bytes Size of /dev/shm --sig-proxy Proxy received signals to the process (default true) --stop-signal string Signal to stop a container (default "SIGTERM") --stop-timeout int Timeout (in seconds) to stop a container --storage-opt list Storage driver options for the container --sysctl map Sysctl options (default map[]) --tmpfs list Mount a tmpfs directory -t, --tty Allocate a pseudo-TTY --ulimit ulimit Ulimit options (default []) -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) --userns string User namespace to use --uts string UTS namespace to use -v, --volume list Bind mount a volume --volume-driver string Optional volume driver for the container --volumes-from list Mount volumes from the specified container(s) -w, --workdir string Working directory inside the container
docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search 搜索镜像
docker pull 下载镜像 docker image pull
docker rmi 删除镜像 docker image rm
docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
[root@localhost ~] docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 days ago 13.3kB [root@localhost ~] docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 days ago 13.3kB [root@localhost ~] docker images -a #查看所有的镜像的所有信息 REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 days ago 13.3kB [root@localhost ~] docker images -aq #查看镜像的id feb5d9fea6a5 [root@localhost ~] docker images -aq #显示所有镜像的id 因为只有一个hello-world镜像所以只有一个 feb5d9fea6a5 #所有信息的意思 REPOSITORY 镜像的仓库源 TAG 镜像的标签 IMAGE ID 镜像的ID CREATED 镜像的创建时间 SIZE 镜像的大小
docker search搜索镜像名字
[root@localhost ~] docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3137 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 92 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 58 [OK] kubeguide/tomcat-app Tomcat image for Chapter 1 31 consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK] cloudesire/tomcat Tomcat server, 6/7/8 15 [OK] aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK] arm32v7/tomcat Apache Tomcat is an open source implementati… 11 maluuba/tomcat7-java8 Tomcat7 with java8. 6 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 6 [OK] arm64v8/tomcat Apache Tomcat is an open source implementati… 6 unidata/tomcat-docker Security-hardened Tomcat Docker container. 5 [OK] amd64/tomcat Apache Tomcat is an open source implementati… 3 cfje/tomcat-resource Tomcat Concourse Resource 2 jelastic/tomcat An image of the Tomcat Java application serv… 2 fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK] oobsri/tomcat8 Testing CI Jobs with different names. 2 camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK] picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 1 [OK] chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [OK] 99taxis/tomcat7 Tomcat7 1 [OK] ppc64le/tomcat Apache Tomcat is an open source implementati… 1 secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] s390x/tomcat Apache Tomcat is an open source implementati… 0 softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK] [root@localhost ~]docker search tomcat --filter=stars=3000 # 搜索出来的镜像就死starts 大于3000的 NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3137 [OK]
docker pull下载镜像: docker image名字 [:版本号]pull
[root@localhost ~] docker pull tomcat:8 8: Pulling from library/tomcat 955615a668ce: Pull complete 2756ef5f69a5: Pull complete 911ea9f2bd51: Pull complete 27b0a22ee906: Pull complete 785dffb36c6c: Pull complete 3fccb14f0369: Pull complete 4a0c30fedd9c: Pull complete 54a2fe1e7768: Pull complete 99fc40810699: Pull complete 17fef45f952c: Pull complete Digest: sha256:658c3c54069e7c969803212191f654d0afb30660cd6702cabc3811ce753d1939 Status: Downloaded newer image for tomcat:8 docker.io/library/tomcat:8 [root@localhost ~] docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 days ago 13.3kB tomcat 8 551efb60a0a2 11 days ago 678MB
docker rmi 删除镜像 docker rmi 镜像id
[root@localhost ~] docker rmi 551efb60a0a2 Untagged: tomcat:8 Untagged: tomcat@sha256:658c3c54069e7c969803212191f654d0afb30660cd6702cabc3811ce753d1939 Deleted: sha256:551efb60a0a200cb11039fe4e29a2f78c2acabd4604e5af8cd345e6ce4486a99 Deleted: sha256:8a8887e79df466c7590dd80c5d30de0cb7b127470883202b2d6664a026a2d54d Deleted: sha256:d7d66a00b3cd43b2d195dca6fba2a726a2980e75cdeb94217ad0f5b579c086e7 Deleted: sha256:dce75d71440205925bab1b50a73d42fd6866631b998ad563da8047f67163ddf6 Deleted: sha256:25f34361d25b41cf15bcefe00e320772675539d74547001c46dce046b7ad787e Deleted: sha256:5ab2e6c924e94042dc07fd33ddb68ad2bd87bb4f25f59f32e08be319700edce9 Deleted: sha256:8ef2b7d4d3c6fa6899e53a70ddeb662fd538fbb8e8ee8bf89341c806caa85d9f Deleted: sha256:7c8e6d275c16b312f3a9fbd14dac058e0925c3071c61f85c14dd85d070567aa2 Deleted: sha256:dde6172a7948c5a7c96cc46703d13946bfd47b726decc1acf48abeda64f32ac4 Deleted: sha256:f11c0e4e16dc4cc8c0ffac9ac15db9a7c5091f16a86d53cab55306be2d4891a8 Deleted: sha256:799760671c382bd2492346f4c36ee4033cf917400be4354c8b096ecef88df34b # docker rmi -f 镜像id 删除指定的镜像 # docker rmi -f 镜像id 镜像id 镜像id 镜像id 删除多个镜像 # docker rmi -f $(docker images -aq) 删除全部的镜像 [root@localhost ~] docker rmi -f $(docker images -aq) Untagged: hello-world:latest Untagged: hello-world@sha256:393b81f0ea5a98a7335d7ad44be96fe76ca8eb2eaa76950eb8c989ebf2b78ec0 Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
docker run 镜像id 新建容器并启动
docker ps 列出所有运行的容器 docker container list
docker rm 容器id 删除指定容器
docker start 容器id #启动容器
docker restart容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
创建容器并且启动:docker run创建容器并且启动
我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
[root@localhost ~] docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest [root@localhost ~]docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 10 days ago 231MB [root@localhost ~] docker run -it centos /bin/bash # -it参数:使用交互方式运行,进入容器查看内容 ;/bin/bash开启容器中的终端。 [root@006db0c94d9c /]# 此时已经进入到centos镜像的终端了 ====>‘root@006db0c94d9c /’ [root@006db0c94d9c /] ls -al #镜像centos中 文件列表 total 0 drwxr-xr-x. 1 root root 6 Sep 26 10:17 . drwxr-xr-x. 1 root root 6 Sep 26 10:17 .. -rwxr-xr-x. 1 root root 0 Sep 26 10:17 .dockerenv lrwxrwxrwx. 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x. 5 root root 360 Sep 26 10:17 dev drwxr-xr-x. 1 root root 66 Sep 26 10:17 etc drwxr-xr-x. 2 root root 6 Nov 3 2020 home lrwxrwxrwx. 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------. 2 root root 6 Sep 15 14:17 lost+found drwxr-xr-x. 2 root root 6 Nov 3 2020 media drwxr-xr-x. 2 root root 6 Nov 3 2020 mnt drwxr-xr-x. 2 root root 6 Nov 3 2020 opt dr-xr-xr-x. 306 root root 0 Sep 26 10:17 proc dr-xr-x---. 2 root root 162 Sep 15 14:17 root drwxr-xr-x. 11 root root 163 Sep 15 14:17 run lrwxrwxrwx. 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Nov 3 2020 srv dr-xr-xr-x. 13 root root 0 Sep 26 05:52 sys drwxrwxrwt. 7 root root 171 Sep 15 14:17 tmp drwxr-xr-x. 12 root root 144 Sep 15 14:17 usr drwxr-xr-x. 20 root root 262 Sep 15 14:17 var # 命令 docker run -d 镜像名! [root@root ~] docker run -d centos # 问题docker ps,发现centos停止了 # 常见的坑,docker 容器使用后台运行,就必须要有一个前台进程, docker 发现没有应用,就会自动停止 # nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
#Ctrl + P + Q # 容器不停止退出
[root@2bb7d2c76281 /]# [root@localhost ~]#
查看当前正在运行的容器:docker ps命令
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@localhost ~]docker ps #列出当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~] docker ps -a # 只显示容器的编号
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c38bdc4b405 centos "/bin/bash" 1 second ago Exited (0) Less than a second ago peaceful_thompson
006db0c94d9c centos "/bin/bash" 15 minutes ago Exited (127) 3 seconds ago silly_gates
6a0af11ac3b7 feb5d9fea6a5 "/hello" 4 hours ago Exited (0) 4 hours ago sharp_cerf
[root@localhost ~] docker ps -n=2 #显示最近创建的两个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c38bdc4b405 centos "/bin/bash" 1 second ago Exited (0) 1 second ago peaceful_thompson
006db0c94d9c centos "/bin/bash" 15 minutes ago Exited (127) 3 seconds ago silly_gates
启动和停止容器
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器
[root@localhost ~] docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2bb7d2c76281 centos "/bin/bash" About a minute ago Up About a minute nervous_lichterman
1af753f5ffc3 tomcat "catalina.sh run" 2 minutes ago Up 2 minutes 8080/tcp quizzical_brown
[root@localhost ~] docker stop 2bb7d2c76281 #停止当前正在运行的容器
2bb7d2c76281
[root@localhost ~] docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1af753f5ffc3 tomcat "catalina.sh run" 7 minutes ago Up 7 minutes 8080/tcp quizzical_brown
删除容器docker rm
docker rm 容器id # 删除指定容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有的容器
docker ps -a -q|xargs docker rm # 删除所有的容器
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。