docker常用命令及设置开机自启方式

 更新时间:2024年06月18日 16:13:21   作者:Week.D.Awn  

这篇文章主要介绍了docker常用命令及设置开机自启方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

目录
  • docker常用命令及设置开机自启
    • 常用基本命令
    • 设置docker开机自启
    • 设置容器自启
  • docker及容器设置开机自启,并修改容器时间、编码格式
    • docker自启动
    • 容器自启动
    • 修改容器时间
    • 修改容器编码格式
  • 总结

    docker常用命令及设置开机自启

    常用基本命令

    # 启动docker
    systemctl start docker
    # 停止dokcer
    systemctl stop docker
    # 查看docker状态
    systemctl status docker 
    # 重启docker
    systemctl restart docker
    # 设置docker 开机自启
    systemctl enable docker
    # 查看docker 版本
    docker version
    # 查看docker 镜像
    docker images
    # 查看docker 运行列表
    docker ps 
    # 查看docker (运行、停止)
    docker ps -aq
    # 删除容器
    docker rm -f 容器ID/容器名称(CONTAINER ID/NAMES)
    #  删除多个容器(空格隔开)
    docker rm -f 容器ID/容器名称 容器ID/容器名称 ...
    # 删除全部容器
    docker rm -f $(docker ps -aq)
    

    设置docker开机自启

    systemctl enable docker

    设置容器自启

    1. 创建容器时设置:

    docker run -d --restart=always --name 容器名称 镜像名称
    # 例如:
    docker  run --restart=always 56f0b18af626

    2. 更新已有容器设置:

    docker update --restart=always 容器ID
    # 例如:
    docker  update --restart=always 56f0b18af626
    

    –restart具体参数,详见官方文档

    Policy Result
    no Do not automatically restart the container when it exits. This is the default. 默认设置,容器退出时不重启容器。
    on-failure[:max-retries] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts. 在容器非正常退出时重启容器,最多重启max-retries次。
    always Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container. 总是重启容器。
    unless-stopped Always restart the container regardless of the exit status, including on daemon startup, except if the container was put into a stopped state before the Docker daemon was stopped. 总是重启容器,除非在Docker守护程序停止之前容器就处于停止状态。

    docker及容器设置开机自启,并修改容器时间、编码格式

    服务器的机房,有时候需要关机重启,进行机房改造!

    docker自启动

    查看已经启动的服务

    systemctl list-units --type=service

    如上图,docker已经启动,

    如果没有启动,就先执行一下启动命令

    systemctl start docker

    查看docker是否设置了开机自启

    systemctl list-unit-files | grep docker

    如上图,设置成功,如果没有设置开机自启,就是disabled

    设置开机启动命令

    systemctl enable docker.service

    容器自启动

    设置容器自启动有两种方式,一个是在创建并启动容器时,一种是在容器启动之后

    创建并启动容器时,添加–restart=always

    docker run -d --restart=always --name demo -p 8080:8080 -v /home/java_backend/logs:/app/logs demo

    容器启动之后,执行如下命令

    docker update --restart=always   容器名称

    修改容器时间

    修改容器时间有三种方式:创建并启动容器时共享宿主机时间、启动之后的容器复制宿主机的时间、在Dockerfile中设置

    1.创建并启动容器时共享宿主机时间,在启动命令中增加-v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro

    docker run -d --restart=always --name demo -p 8080:8080 -v /home/java_backend/logs:/app/logs -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro demo

    2.启动之后的容器复制宿主机的时间

    docker cp /etc/localtime [containerId]:/etc/localtime
    docker cp /etc/timezone [containerId]:/etc/timezone

    containerId 为要修改的容器id,注意需要重启容器

    3.在Dockerfile中设置

    RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

    Dockerfile中添加如上命令即可

    修改容器编码格式

    首先进入容器查看当前字符编码

    locale

    查看本地拥有的字符编码

    locale -a

    C.UTF-8可以支持中文,只需要把容器编码设置为C.UTF-8即可(有的是zh_CN.UTF-8,不过我在本地没发现这种编码)

    临时修改字符编码

    设置字符编码为C.UTF-8

    export LANG=C.UTF-8

    重新加载环境变量

    source /etc/profile

    永久修改

    Dockerfile中添加命令

    ENV LANG C.UTF-8

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持小闻网。

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。