这里我们拿 java Web 应用程序举例,我们一个 java Web 应用程序涉及很多东西,比如 jdk、tomcat、mysql 等软件环境。当这些其中某一项版本不一致的时候,可能就会导致应用程序跑不起来这种情况。Docker 则将程序以及使用软件环境直接打包在一起,无论在那个机器上保证了环境一致。
┌──(root㉿kali)-[~] └─# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f 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/
Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to ......
镜像命令
搜索镜像:docker search ImageName
1 2 3 4 5 6 7 8 9 10
┌──(root㉿kali)-[~] └─# docker search kali NAME DESCRIPTION STARS OFFICIAL AUTOMATED kalilinux/kali-rolling Official Kali Linux Docker image (weekly sna… 782 kasmweb/core-kali-rolling Kali Rolling XFCE Desktop With Default Tools… 61 kalilinux/kali-last-release Image built from the last snapshot of the of… 55 kalilinux/kali-bleeding-edge Same as kali-rolling with kali-bleeding-edge… 52 donaldrich/kali-linux Multi-arch Kali-rolling base image with kali… 18 kasmweb/kali-rolling-desktop Kali Rolling desktop for Kasm Workspaces 11 kalilinux/kali-dev Image built from the kali-dev development re… 11
拉取镜像:docker pull ImagesName[:TAG]
1 2 3 4 5 6 7 8
┌──(root㉿kali)-[~] └─# docker pull kalilinux/kali-rolling Using default tag: latest latest: Pulling from kalilinux/kali-rolling cecc8ba0ec42: Pull complete Digest: sha256:402b70af0e6a768e06543aa9403dd8a120ca7d16c0b023b9cf0e21e41f34b31a Status: Downloaded newer image for kalilinux/kali-rolling:latest docker.io/kalilinux/kali-rolling:latest
查看所有镜像:docker images
-a:列出所有镜像(包括构建失败的镜像);
-q:只显示 image id。
1 2 3 4 5 6 7 8 9 10
┌──(root㉿kali)-[~] └─# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 9c7a54a9a43c 5 months ago 13.3kB kalilinux/kali-rolling latest c2fadbc65f8d 21 months ago 126MB
┌──(root㉿kali)-[~] └─# docker run -it --name kali kalilinux/kali-rolling
┌──(root💀584d4750ea76)-[/] └─# id uid=0(root) gid=0(root) groups=0(root)
┌──(root💀584d4750ea76)-[/] └─# uname -a Linux 584d4750ea76 6.3.0-kali1-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.3.7-1kali1 (2023-06-29) x86_64 GNU/Linux
查看运行的容器:docker ps
-a:正在运行的和历史运行过的容器;
-q:只显示 container id。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
┌──(root💀584d4750ea76)-[/] └─# exit exit
┌──(root㉿kali)-[~] └─# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
┌──(root㉿kali)-[~] └─# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 584d4750ea76 kalilinux/kali-rolling "bash" About a minute ago Exited (0) 51 seconds ago kali ┌──(root㉿kali)-[~] └─# docker ps -aq 584d4750ea76