docker 常用命令总结及其解析
程序员文章站
2024-03-13 22:56:10
...
1. 启动docker服务
[[email protected] src]# service docker start
Redirecting to /bin/systemctl start docker.service
[[email protected] src]#
表示docker服务已经启动
2. 拉取镜像:
# 默认拉取镜像的命令格式
[[email protected] ~]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:92695bc579f31df7a63da6922075d0666e565ceccad16b59c3374d2cf4e8e50e
Status: Downloaded newer image for docker.io/hello-world:latest
# 从 国内站c.163.com 拉取nginx镜像, 当然, 使用第一种方式也OK
[[email protected] ~]# docker pull hub.c.163.com/library/nginx
Using default tag: latest
Trying to pull repository hub.c.163.com/library/nginx ...
latest: Pulling from hub.c.163.com/library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest fce289e99eb9 4 months ago 1.84 kB
hub.c.163.com/library/nginx latest 46102226f2fd 2 years ago 109 MB
[[email protected] ~]#
2. 使用docker运行镜像
- 运行起来的镜像可以认为是容器
# 在docker下使用ubuntu运行echo命令并输出, 这条命令也做了pull的事情,
[[email protected] src]# docker run ubuntu echo hello world
Unable to find image 'ubuntu:latest' locally
Trying to pull repository docker.io/library/ubuntu ...
latest: Pulling from docker.io/library/ubuntu
898c46f3b1a1: Pull complete
63366dfa0a50: Pull complete
041d4cd74a92: Pull complete
6e1bee0f8701: Pull complete
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for docker.io/ubuntu:latest
hello world
# docker 下运行nginx
[[email protected] src]# docker run nginx
Unable to find image 'nginx:latest' locally
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
27833a3ba0a5: Pull complete
ea005e36e544: Pull complete
d172c7f0578d: Pull complete
Digest: sha256:e71b1bf4281f25533cf15e6e5f9be4dac74d2328152edf7ecde23abc54e16c1c
Status: Downloaded newer image for docker.io/nginx:latest
[[email protected] src]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 27a188018e18 2 weeks ago 109 MB
docker.io/hello-world latest fce289e99eb9 4 months ago 1.84 kB
hub.c.163.com/library/nginx latest 46102226f2fd 2 years ago 109 MB
# 注意: 现在有两个nginx, 因为下载源不一致导致的, 默认使用的docker.io下载的
# docker下再次运行nginx, 并将nginx的80端口映射为本地8080端口, 返回containerID
[[email protected] src]# docker run -p 8080:80 -d docker.io/nginx
738a4e2eee94b66f13d3d6cf1095b418f4148e904a482335f4a38c5b6a1b444b
# docker下正在运行的进程
[[email protected] src]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
738a4e2eee94 docker.io/nginx "nginx -g 'daemon ..." 6 minutes ago Up 6 minutes 0.0.0.0:8080->80/tcp hungry_kirch
# 进入正在运行的容器中,并执行一些命令, 看起来像是进入了一个新的虚拟机中, 常用的shell命令都可以使用
[[email protected] ~]# docker exec -it 738a4e2eee94 bash
[email protected]:/# pwd
/
[email protected]:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[email protected]:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
# 退出 容器编辑命令行
[email protected]:/# exit
exit
# 检查主机的8080端口
[[email protected] ~]# netstat -na | grep 8080
tcp6 0 0 :::8080 :::* LISTEN
# 停止某个正在运行的容器
[[email protected] ~]# docker stop 738a4e2eee94
在浏览器中输入服务器地址:8080
, 看是否可以看到nginx的欢迎页面.
# 以下为测试docker中的nginx是否已经启动
[[email protected] src]# cd /home/
[[email protected] home]# vim index.html
# 复制index.html 到docker中的nginx中的,以修改nginx的欢迎页面内容
[[email protected] home]# docker cp index.html 738a4e2eee94://usr/share/nginx/html
# 将复制后的index.html提交到image中
[[email protected] home]# docker commit -m 'fun' 738a4e2eee94 nginx-fun
sha256:13164158f600ff6630ea6cc330390acf3782ee594c861920094f0ba1c94574c3
# 下面的nginx-fun即是新生成的image
[[email protected] home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-fun latest 13164158f600 10 seconds ago 109 MB
docker.io/nginx latest 27a188018e18 7 days ago 109 MB
docker.io/ubuntu latest 94e814e2efa8 6 weeks ago 88.9 MB
# 删除imageID=13164158f600的image
[[email protected] home]# docker rmi 13164158f600
- 本地没有
ubuntu
和nginx
image, 结果就自动从远程pull最新的
命令小结
命令 | 用途 | 类比 |
---|---|---|
docker pull | 获取image | git pull |
docker build | 创建 image | git add |
docker images | 列出所有的image | – |
docker run | 运行 container | shell中的service start |
docker ps | 列出运行中的container | shell中的ps |
docker rm | 删除container | shell中的rm |
docker rmi | 删除image | – |
docker cp | 在host和container之间拷贝文件 | shell中的cp |
docker commit | 保存改动为新的image | git commit |