查看所有容器
docker ps -a #-a全部 默认 查询存活
启动容器(运行镜像)
docker run是日常用的最频繁用的命令之一,同样也是较为复杂的命令之一
命令格式: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS :选项
-i:表示启动-一个可交互的容器, 并持续打开标准输入
-t:表示使用终端关联到容器的标准输入输出上
-d :表示将容器放置后台运行
--rm:退出后即删除容器
--name :表示定义容器唯一名称
IMAGE :表示要运行的镜像
COMMAND :表示启动容器时要运行的命令*
ARG:参数
–privileged=true #加上之后容器内部权限更多,不会出现权限问题
#服务器启动后不退出
/usr/sbin/init #启动方式
交互式启动容器
[root@alice ~]# docker run -it zhaokuner/alpine:latest /bin/sh
/ #
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
47: eth0@if48: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:18:26:02 brd ff:ff:ff:ff:ff:ff
inet 172.24.38.2/24 brd 172.24.38.255 scope global eth0 # 之前/etc/docker/daemon.json写了网段地址
valid_lft forever preferred_lft forever
非交互式启动容器
[root@alice ~]# docker run -d --name alpine_sleep docker.io/zhaokuner/alpine:latest /bin/sleep 300
ac75ed049d2a2a3020310a3bb24496d4c72aae76a4a71449cb3c37c589df9395
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac75ed049d2a mmdghh/alpine:latest "/bin/sleep 300" 21 seconds ago Up 20 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 12 minutes ago Exited (130) 9 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 27 minutes ago Exited (0) 27 minutes ago hopeful_edison
[root@alice ~]#
这里有进程夯住之后 容器就不会挂掉了
在宿主机查看进程
root@alice ~]# ps aux |grep sleep|grep -v grep
root 20957 0.0 0.0 1540 248 ? Ss 16:30 0:00 /bin/sleep 300
[root@alice ~]#
docker用了宿主机的内核 所以虽然是隔离的 但是在宿主机仍然可以查看到docker的进程 而且有自己的pid
进入容器
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 33 seconds ago Up 32 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 18 minutes ago Exited (130) 16 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 33 minutes ago Exited (0) 33 minutes ago hopeful_edison
[root@alice ~]# docker exec -it 06fbbee401aa /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
6 root 0:00 /bin/sh
11 root 0:00 ps aux
/ #
[root@alice ~]# docker exec -it alpine_sleep /bin/sh # 也可以使用容器的名称进入
/ #
退出容器
exit
ctrl + P + Q #退出并后台运行
容器的启动/停止/重启
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 2 minutes ago Up 2 minutes alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 20 minutes ago Exited (130) 17 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 35 minutes ago Exited (0) 35 minutes ago hopeful_edison
[root@alice ~]# docker stop 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 2 minutes ago Exited (137) 4 seconds ago alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 20 minutes ago Exited (130) 18 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker start 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 3 minutes ago Up 1 second alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 21 minutes ago Exited (130) 18 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker restart 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 3 minutes ago Up 3 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 21 minutes ago Exited (130) 19 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker restart alpine_sleep # 可以用名字也可以用ID
alpine_sleep
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 4 minutes ago Up 3 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 22 minutes ago Exited (130) 19 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago hopeful_edison
[root@alice ~]#
在宿主机和容器之间传输文件
docker cp container_id:/tmp/xxx.txt
删除容器
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 5 minutes ago Up About a minute alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 23 minutes ago Exited (130) 21 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]# docker rm facbbda54346 #也可以是名字
facbbda54346
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 5 minutes ago Up About a minute alpine_sleep
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]# docker rm 06fbbee401aa # 正在运行的容器需要用-f 来强制删除
Error response from daemon: You cannot remove a running container 06fbbee401aaad02da272f920dcb264d539187121f962c6bb3dea5acb90321dd. Stop the container before attempting removal or force remove
[root@alice ~]# docker rm -f 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]#
删除所有未在运行的容器
docker rm `docker ps -a -q`
or
for i in `docker ps -a | grep -i exit | awk '{print $1}'` ;do docker rm -f $i;done
docker rm `docker ps -a | grep -i exit | awk '{print $1}'`
保存镜像
[root@alice ~]# docker run -d --name alpine_sleep docker.io/mmdghh/alpine:latest /bin/sleep 300s
c3d1aa7a1bc2df47f47621f16e420883b334ef8e242349f1f734ad9cb4533968
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 seconds ago Up 4 seconds alpine_sleep
[root@alice ~]# docker exec -it c3d1aa7a1bc2 /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # echo "hello world" >1.txt
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # [root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 45 seconds ago Up 44 seconds alpine_sleep
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:latest /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ #
写入到容器的文件并不会保存在镜像里
-p 保存到执行命令这一时刻的内容 之后更新的不会保存
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 minutes ago Up 4 minutes alpine_sleep
[root@alice ~]# docker commit -p alpine_sleep docker.io/mmdghh/alpine:v_1.txt
sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 8 seconds ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
hello world
/ # [root@alice ~]#
导入导出镜像
导出: docker save image_name/image_id > xxx.tar
导入: docker load -i xxx.tar 或 docker load < xxx.tar
tip: 如果你导出的时候名称用了: 记得导入的时候用\转义
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 10 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker save 11be52147924 > mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# ll mmdghh_alpine_v_1.txt.tar
-rw-r--r-- 1 root root 5829632 Jan 10 17:18 mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# docker rmi mmdghh/alpine:v_1.txt -f
Untagged: mmdghh/alpine:v_1.txt
Deleted: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker load < mmdghh_alpine_v_1.txt.tar
Loaded image ID: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 11be52147924 12 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker tag 11be52147924 docker.io/mmdghh/alpine:v_1.txt
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 12 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
hello world
/ #
查看容器日志
docker inspect container_name/container_di # 获取容器/镜像的元数据
docker logs -f container_id(/container_name) #查询容器的标准输出 -f 同 tail -f
args
端口映射
-p host_port:container_port
[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx 1.12.2 4037a5562b03 2 years ago 108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:83->80/tcp nginx
[root@alice ~]#
挂载目录
-v host_path:container_path
[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx 1.12.2 4037a5562b03 2 years ago 108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:83->80/tcp nginx
[root@alice ~]#
[root@alice ~]# mkdir html
[root@alice ~]# cd html/
[root@alice html]# wget www.baidu.com -O index.html
--2021-01-10 17:54:31-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 220.181.38.149, 220.181.38.150
Connecting to www.baidu.com (www.baidu.com)|220.181.38.149|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’
100%[===================================================================================================================>] 2,381 --.-K/s in 0s
2021-01-10 17:54:31 (264 MB/s) - ‘index.html’ saved [2381/2381]
[root@alice html]# cat index.html
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@alice html]# docker run -d --name nginx_with_baidu -p 84:80 -v /root/html:/usr/share/nginx/html nginx:1.12.2
acf79798ce19fdb6e584723d0ab1cc057508082466f6b9be92acc19eca737699
[root@alice html]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acf79798ce19 nginx:1.12.2 "nginx -g 'daemon of…" 7 seconds ago Up 5 seconds 0.0.0.0:84->80/tcp nginx_with_baidu
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:83->80/tcp nginx
[root@alice html]# docker exec -it acf79798ce19 /bin/bash
root@acf79798ce19:/# ls /usr/share/nginx/html/
index.html
查看挂载的详细信息
[root@alice html]# docker inspect nginx_with_baidu |grep -A 9 'Mounts'
"Mounts": [
{
"Type": "bind",
"Source": "/root/html",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
[root@alice html]#
传递环境变量
-e variate_name=variate_value
[root@alice ~]# docker run --rm -e E_OPTS=qwert docker.io/mmdghh/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=62db172fe9da
E_OPTS=qwert
HOME=/root
[root@alice ~]# docker run --rm -e E_OPTS=qwert -e C_OPTS=12345 docker.io/mmdghh/alpine:latest printenv #传递多个变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=3ac265a1cf85
E_OPTS=qwert
C_OPTS=12345
HOME=/root
[root@alice ~]#
容器内下载软件
红帽系 yum
debian系 apt-get
alpine apt
[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# curl
bash: curl: command not found
root@acf79798ce19:/# exit
[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# tee /etc/apt/sources.list << EOF
> deb http://mirrors.163.com/debian/ jessie main non-free contrib
> deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
> EOF
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# cat /etc/apt/sources.list
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# apt-get update && apt-get install curl -y
安装好后commit并且推送到仓库
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acf79798ce19 nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:84->80/tcp nginx_with_baidu
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:83->80/tcp nginx
[root@alice ~]# docker commit -p acf79798ce19 mmdghh/nginx:curl
sha256:84b7a98f5ee209f0139febe7cac04a7edaaca7254ddf1c043e8ac779504204ba
[root@alice ~]# docker push docker.io/mmdghh/nginx:curl
The push refers to repository [docker.io/mmdghh/nginx]
bbadc5b62281: Pushed
4258832b2570: Mounted from library/nginx
683a28d1d7fd: Pushed
d626a8ad97a1: Mounted from library/nginx
curl: digest: sha256:f86f97bacf0ff37e3cc09f98dfb8153c486ee1e8bb9caad5046ed6aa58c43c50 size: 1160
[root@alice ~]#