[root@sunmmerwind ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Status: Downloaded newer image for hello-world:latestHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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/
8、查看運行的鏡像
[root@sunmmerwind ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 weeks ago 13.3kB
[root@sunmmerwind ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58715163d8e3 hello-world "/hello" About a minute ago Exited (0) About a minute ago wonderful_hellman
Usage: docker images [OPTIONS][REPOSITORY[:TAG]]List imagesOptions:-a,--all Show all images (default hides intermediate images)--digests Show digests-f,--filterfilterFilter output based on conditions provided--format string Pretty-print images using a Go template--no-trunc Don't truncate output-q, --quiet Only show numeric IDs
[root@iZbf2jzc4i64zvZ ~]# docker --helpUsage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersOptions:--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_HOSTenv var and default context set with "docker context use")-D, --debug Enable debug mode-H, --host list Daemon socket(s) to connect to-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")--tls Use TLS; implied by --tlsverify--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")--tlskey string Path to TLS key file (default "/root/.docker/key.pem")--tlsverify Use TLS and verify the remote-v, --version Print version information and quitManagement Commands:builder Manage buildsconfig Manage Docker configscontainer Manage containerscontext Manage contextsengine Manage the docker engineimage Manage imagesnetwork Manage networksnode Manage Swarm nodesplugin Manage pluginssecret Manage Docker secretsservice Manage servicesstack Manage Docker stacksswarm Manage Swarmsystem Manage Dockertrust Manage trust on Docker imagesvolume Manage volumesCommands:attach Attach local standard input, output, and error streams to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescpCopy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on Docker objectskillKill one or more running containersload Load an image from a tar archive or STDINlogin Log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save one or more images to a tar archive (streamed to STDOUT by default)search Search the Docker Hub for imagesstartStart one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containersversion Show the Docker version informationwait Block until one or more containers stop, then print their exit codesRun 'docker COMMAND --help'for more information on a command.
docker images --help(顯示該命令的幫助信息)
Usage: docker images [OPTIONS][REPOSITORY[:TAG]]List imagesOptions:-a,--all Show all images (default hides intermediate images)--digests Show digests-f,--filterfilterFilter output based on conditions provided--format string Pretty-print images using a Go template--no-trunc Don't truncate output-q,--quiet Only show numeric IDs
2. 鏡像命令
鏡像類似 我們所說的系統盤 O(∩_∩)O哈哈~
docker images(查詢容器所有的鏡像)
[root@sunmmerwind ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 6 weeks ago 13.3kB
docker search(搜索鏡像)
搜索命令
[root@sunmmerwind ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10768 [OK]
mariadb MariaDB Server is a high performing open sou… 4056 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 792 [OK]
percona Percona Server is a fork of the MySQL relati… 533 [OK]
過濾搜索
[root@sunmmerwind ~]# docker search mysql --filter=stars=5000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10768 [OK]
# centos 是 下載的一個鏡像[root@iZbf2jzc4i64zvZ ~]# docker run -it centos /bin/bash# 換成鏡像ID了[root@755709dbc6b0 /]# # centos基礎版本,很多命令不完善!ll命令就沒有~~~[root@755709dbc6b0 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
后臺啟動命令 及 坑~~!!
#命令:docker run -d 鏡像名[root@iZbf2jzc4i64zvZ ~]# docker run -d centos /bin/bash
27be24a0c1dda2815bc2dec8bb6efc09f81c2b6ca7a9b0380a11e6d6ef315a5b
#問題docker ps,發現centos停止了~[root@iZbf2jzc4i64zvZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZbf2jzc4i64zvZ ~]# #常見的坑:docker容器使用后臺運行,就必須要有一個前臺的進程,docker發現沒有應用,就會自動停止#還有nginx,容器后臺啟動后,發現自己沒有提供服務,就會立刻停止
(退出容器)
容器停止 并 退出容器
exit
容器不停止 退出容器
Ctrl+P+Q(順序不能錯)
docker ps(查看當前正在運行的容器)
參數說明:
???????#?不加任何參數,只顯示當前正在運行的容器 -a ????#?查看當前正在運行的容器 + 歷史運行過的程序 -n=? ??#?顯示最近創建的容器(?代表顯示幾個) -q ??#?顯示容器的編號(CONTAINER ID)
[root@iZbf2jzc4i64zvZ ~]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
755709dbc6b0 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago pedantic_mccarthy
[root@iZbf2jzc4i64zvZ ~]# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
755709dbc6b0 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago pedantic_mccarthy
6b5fb686c28d bf756fb1ae65 "/hello" 35 hours ago Exited (0) 35 hours ago silly_stonebraker
[root@iZbf2jzc4i64zvZ ~]# docker ps -a -n=3
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
755709dbc6b0 centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago pedantic_mccarthy
6b5fb686c28d bf756fb1ae65 "/hello" 35 hours ago Exited (0) 35 hours ago silly_stonebraker
[root@iZbf2jzc4i64zvZ ~]# docker rm 87e99063bbbe
Error response from daemon: You cannot remove a running container 87e99063bbbe.
(在試圖移動或強行移動之前,請停止容器)
Stop the container before attempting removal or force remove
[root@iZbf2jzc4i64zvZ ~]# docker rm -f 87e99063bbbe
87e99063bbbe
[root@iZbf2jzc4i64zvZ ~]# docker run -d centos /bin/bash -c "while true;do echo SummerWind;sleep 1;done" # 這段shell腳本,意思是循環打印SummerWind
c5672e9223d29718ff49ac61be7dbc96bdee87d84f451c24f95fa977ac2b442c
[root@iZbf2jzc4i64zvZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5672e9223d2 centos "/bin/bash -c 'while猞" 4 seconds ago Up 3 seconds recursing_cartwright[root@iZbf2jzc4i64zvZ ~]# docker logs -tf --tail 10 c5672e9223d2
2020-07-24T07:15:25.177771117Z SummerWind
2020-07-24T07:15:26.180032231Z SummerWind
2020-07-24T07:15:27.181983254Z SummerWind
2020-07-24T07:15:28.184071541Z SummerWind
2020-07-24T07:15:29.186062819Z SummerWind
docker top(查看正在運行的容器內部的進程信息)
docker top 容器ID(這要是正在運行的容器)
[root@iZbf2jzc4i64zvZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3c259fa54b6 centos "/bin/bash -c 'while猞" About a minute ago Up About a minute hardcore_benz
[root@iZbf2jzc4i64zvZ ~]# docker top b3c259fa54b6
UID PID PPID C STIME TTY TIME CMD
root 17631 17615 0 15:22 ? 00:00:00 /bin/bash -c while true;doecho SummerWind;sleep 1;done
root 17742 17631 0 15:23 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep/usr/bin/sleep 1
#后臺交互運行一個容器[root@iZbf2jzc4i64zvZ ~]# docker run -it centos /bin/bash#運行并進入容器[root@d250d9bba70b /]# [root@iZbf2jzc4i64zvZ ~]# #容器的文件[root@d250d9bba70b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var[root@d250d9bba70b /]# cd /home/#進入容器home目錄,創建SummerWind.java文件[root@d250d9bba70b home]# touch SummerWind.java#顯示已經創建[root@d250d9bba70b home]# ls
SummerWind.java
#退出容器并停止容器[root@d250d9bba70b home]# exitexit#查看運行著的容器 沒有[root@iZbf2jzc4i64zvZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#查看所有運行過的容器 有【容器在 里面的數據就在,不論是否運行都可以拷貝~~!!!】[root@iZbf2jzc4i64zvZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d250d9bba70b centos "/bin/bash" 2 minutes ago Exited (0) 17 seconds ago upbeat_volhard
#拷貝命令:docker cp 容器ID:容器內的目錄或文件 主機上的路徑[root@iZbf2jzc4i64zvZ ~]# docker cp d250d9bba70b:/home/SummerWind.java /home[root@iZbf2jzc4i64zvZ ~]# cd ..[root@iZbf2jzc4i64zvZ /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var[root@iZbf2jzc4i64zvZ /]# cd /home#查看主機上的home文件夾下 已經把容器內的文件拷貝到主機上了[root@iZbf2jzc4i64zvZ home]# ls
SummerWind.java
(從宿主機 拷貝文件到 容器中)
docker cp 要拷貝的文件路徑 容器名:要拷貝到容器里面對應的路徑
# 命令:
docker cp/MyFile/PAIANG.sql bd15ab4384fd:/tmp/# 容器中查看:
root@bd15ab4384fd:/# cd /tmp/
root@bd15ab4384fd:/tmp# ll
total 90460
drwxrwxrwt 1 root root 4096 Nov 10 14:04 ./
drwxr-xr-x 1 root root 4096 Nov 10 14:04 ../
drwxrwxrwt 2 root root 4096 Feb 14 2019 .oracle/-rw-r--r-- 1 root root 92616247 Nov 10 13:54 PAIANG.sql
4. 命令圖解
5. 命令實戰
安裝運行Nginx容器
#下載鏡像#運行Nginx容器(鏡像要放在最后)[root@iZbf2jzc4i64zvZ /]# docker run -d --name MyNginx -p 3344:80 nginx
aab1ba8e191cda24e673ca0f61f19c64a74393a685b35e31a0c5401ab3732ccb
[root@iZbf2jzc4i64zvZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aab1ba8e191c nginx "/docker-entrypoint.猞" 17 seconds ago Up 15 seconds 0.0.0.0:3344->80/tcp MyNginx#本地測試 curl,居然還有這?[root@iZbf2jzc4i64zvZ /]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you forusing nginx.</em></p>
</body>
</html>