Docker容器自动重启设置


重启reboot操作系统后,发现docker 服务未启动,容器也未启动,怎么才能重启后自动启动呢

1、docker服务自动重启设置

[root@localhost ~]# systemctl enable docker.service

2、docker容器自动启动设置

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        10 months ago       126MB
[root@localhost ~]# docker run -itd -p 80:80 nginx
3e28c4b5c6256c0ba04666751e426987d848b7afeb9c59774d5e9831dc78e5ee
[root@localhost ~]# docker run -itd -p 81:80 nginx
f0597c725fd6b7f4229aa9ab5de4a3cb29d09097a81dc8f64d1a60d469001379
[root@localhost ~]# docker port f0597c725fd6
80/tcp -> 0.0.0.0:81


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
f0597c725fd6        nginx               "nginx -g 'daemon of??   30 seconds ago      Up 29 seconds       0.0.0.0:81->80/tcp   elastic_allen
3e28c4b5c625        nginx               "nginx -g 'daemon of??   35 seconds ago      Up 33 seconds       0.0.0.0:80->80/tcp   tender_volhard

3、 docker容器自动启动设置

[root@localhost ~]# docker update --restart=always f0597c725fd6 3e28c4b5c625 
f0597c725fd6
3e28c4b5c625


[root@localhost ~]# reboot -h now
Connection closed by foreign host.


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
f0597c725fd6        nginx               "nginx -g 'daemon of??   13 minutes ago      Up 2 minutes        0.0.0.0:81->80/tcp   elastic_allen
3e28c4b5c625        nginx               "nginx -g 'daemon of??   13 minutes ago      Up 2 minutes        0.0.0.0:80->80/tcp   tender_volhard

 

 

当一个host中镜像和容器较多,需要重置时可选择删除其中部分或全部的镜像和容器。那么你就需要下面的操作了。

1、删除容器

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
27c5c4d3cf86        nginx               "nginx -g 'daemon of??   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   gracious_nash
61cccfe238a8        nginx               "nginx -g 'daemon of??   2 minutes ago       Up 2 minutes        0.0.0.0:81->80/tcp   distracted_grothendieck
[root@localhost ~]# docker ps -aq
27c5c4d3cf86
61cccfe238a8


1)首先需要停止所有的容器
[root@localhost ~]# docker ps -aq
f0597c725fd6
3e28c4b5c625
8855c7777f83
466d2efe3dd9
20ca589b1a10
e5457b41cae6
314d1d01c941

[root@localhost ~]# docker stop $(docker ps -aq)
f0597c725fd6
3e28c4b5c625
8855c7777f83
466d2efe3dd9
20ca589b1a10
e5457b41cae6
314d1d01c941
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2)删除所有的容器(只删除单个时把后面的变量改为image id即可)
[root@localhost ~]# docker rm -f $(docker ps -aq)
f0597c725fd6
3e28c4b5c625
8855c7777f83
466d2efe3dd9
20ca589b1a10
e5457b41cae6
314d1d01c941
[root@localhost ~]# docker ps -aq
[root@localhost ~]# 

2、删除镜像

1)查看host中的镜像
docker images

2)删除指定id的镜像
docker rmi <image id>

3)删除全部的images

docker rmi $(docker images -q)

3、当要删除的iamges和其他的镜像有关联而无法删除时
可通过 -f 参数强制删除
docker rmi -f $(docker images -q)


 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐