Docker installation and basic use

image.png

1. Introduction

  • Docker official documents , Docker Chinese documents, the update will lag behind the official documents

  • what Dockeris

    DockerIt is an application 打包, tool 分发, and 部署tool. It can also be understood as a lightweight virtual machine. It only virtualizes the operating environment required by the software, and does not need any extras. A normal virtual machine is a complete and huge system, including various software whether it is necessary or not.

    • 打包: It is to package the dependencies, third-party libraries, and software required for software operation into one 安装包.
    • 分发: You can 安装包upload the packaged .
    • 部署: Take 安装包, you can run the application with one command, and automatically simulate the exact same operating environment, no matter where you are Windows/Mac/Linux.
  • feature comparison

    characteristic Ordinary virtual machine Docker
    Cross-platform Usually only run on desktop-class systems, e.g. Windows/Macnot on a server without a GUI There are many supported systems, all kinds of windowsand Linuxare supported
    performance High performance loss and high memory usage, because the entire system is virtualized Good performance, only the operating environment required by the virtual software, minimizing useless configuration
    automation need to install everything manually One command can automatically deploy the required environment
    stability The stability is not high, and the difference between different systems is large Good stability, different systems are deployed in the same way

2. Installation

  • docker installation , docker-compose installation , docker quick start, including detailed steps such as windows installation

    • Desktop version: https://www.docker.com/products/docker-desktop

    • Server version: https://docs.docker.com/engine/install/#server

    • dockerAfter the installation is successful, you can use $ docker -vto check whether the installation is successful.

    • docker-composeAfter the installation is successful, you can use $ docker-compose -vto check whether the installation is successful.

      Notice:

      Except when Linuxinstalling on docker, it will not be installed automatically docker-compose, and other platforms ( window, mac) will be automatically installed docker-compose.

      After installation docker, you can $ docker-compose -vcheck whether it is installed by the way docker-compose. If it is not installed, you can install it according to the official website.

    • After the installation is successful, in the desktop version, you only need to start it to use normally. Even if the configuration is modified, you can restart the software. In the server version, you need to start systemctlor restart:

      # 前面的 sudo 可更具情况进行添加
      # 将 docker 加入开机自启动的列表
      $ sudo systemctl enable docker
      # 启动
      $ sudo systemctl start docker
      # 停止
      $ sudo systemctl stop docker
      # 重启
      $ sudo systemctl restart docker
      # 查看状态
      $ sudo systemctl status docker
      
  • If it is in Windowsthe environment, you also need to configure WSLthe environment:

    • Introduction and use of WSL

    • Get started with Windows Subsystem for Linux (WSL)

    • Open Windows 虚拟化and Linux 子系统(WSL2), attached: docker windows installation and other detailed steps .

      image.png

      image.png

    • Ubuntu(乌班图)It is an operating system based on Debiandesktop applications Linux, which can be searched and downloaded online in the application, or installed through commands.

    • The command install (default install ubuntu linux) must be run on Windows 10 版本 2004and above (build 19041and above) or Windows 11to use the following:

      # 安装好 ubuntu 后,并启动,会要求输入账号密码
      $ wsl --install 
      
      # 上面等于下面,默认安装就是 Ubuntu
      $ wsl --install -d Ubuntu
      
      # 输出
      正在安装: Ubuntu  
      已安装 Ubuntu。  
      正在启动 Ubuntu...  
      Installing, this may take a few minutes...
      Please create a default UNIX user account. The username does not need to match your Windows username.  
      For more information visit: <https://aka.ms/wslusers>  
      Enter new UNIX username: xxxx  
      New password:  
      Retype new password:  
      passwd: password updated successfully  
      Installation successful!  
      To run a command as administrator (user "root"), use "sudo <command>".
      ...
      

      After installation, how to enter ubuntu?

      1. You can cmd/PowerShellexecute $ ubuntuthe command in to enter ubuntu.

      2. In the system search, search for ubuntuthe application and open it.

  • mirror configuration

    • mirror list

      Mirror attribution mirror address
      DockerChinese official mirror https://registry.docker-cn.com
      DaoCloudmirror site http://f1361db2.m.daocloud.io
      AzureChina Mirror https://dockerhub.azk8s.cn
      HKUST Mirror Station https://docker.mirrors.ustc.edu.cn
      Ali Cloud https://<your_code>.mirror.aliyuncs.com You need to register an account to get a private one, here is someone else’s https://ud6340vz.mirror.aliyuncs.com
      Qiniu Yun https://reg-mirror.qiniu.com
      NetEase Cloud https://hub-mirror.c.163.com
      Tencent Cloud https://mirror.ccs.tencentyun.com
    • mirror format

      // 单个
      "registry-mirrors": ["https://registry.docker-cn.com"]
      
      // 可以支持多个
      "registry-mirrors": ["https://registry.docker-cn.com", "https://dockerhub.azk8s.cn", ...]
      
    • Configure mirroring:

      # 方法一【使用命令修改配置】
      # 1.运行以下命令来编辑 Docker 配置文件:
      $ sudo vim /etc/docker/daemon.json
      # 2.将以下内容复制到文件中,文件中有对象则去掉 {} 拷贝进去:
      {
              
               "registry-mirrors": ["https://registry.docker-cn.com"] }
      # 3. 保存并关闭文件。
      # 4. 重启Docker服务:
      $ sudo systemctl restart docker
      # 5. 完成后,可以通过运行以下命令来验证是否成功:
      $ docker info
      # 6. 注意:如果是从其他镜像源获取镜像,需要将 https://registry.docker-cn.com 替换为相应的镜像源地址。
      
      
      # 方法二:【使用 Docker Desktop 界面修改】
      # 1.在 Docker Desktop 的状态栏上右键单击图标,选择 Settings。
      # 2. 在 Docker Engine 选项卡中,找到 Registry mirrors 并单击 + 按钮。
      # 3. 输入新的镜像源地址。
      "registry-mirrors": ["https://registry.docker-cn.com"]
      # 4. 单击 Apply & restart 保存更改并重启服务。
      # 5. 如果按钮自带重启,则不需要管,没有 restart 则需重启 Docker 服务。
      # 6. 完成后,可以通过运行以下命令来验证是否成功:
      $ docker info
      

      image.png

    • View current mirror

      $ docker info
      

      Execute the above command to see if there is any in the output information Registry Mirrors. If there is no default, it means a foreign mirror. If there is, it means that there is a custom mirror address:

      ...
      Registry Mirrors:
        https://registry.docker-cn.com/
      

Three, use

  • Common commands

    # 运行容器
    $ docker run ...
    # 查看当前运行中的容器
    $ docker ps
    # 查看镜像列表
    $ docker images
    # 从仓库中拉取镜像
    $ docker pull
    # 删除指定 id 的容器
    $ docker rm <container-id>
    # 停止/启动指定 id 的容器
    $ docker stop/start <container-id>
    # 删除指定 id 的镜像
    $ docker rmi <image-id>
    # 查看 volume 列表
    $ docker volume ls
    # 查看网络列表
    $ docker network ls
    ......
    
  • DockerCommand command list: https://docs.docker.com/engine/reference/commandline/run/

  • DockerOfficial mirror warehouse: https://hub.docker.com/

    For example: search redis, download and run.

    $ docker run -d -p 6379:6379 --name redis redis:latest
    
    -d # 表示可后台运行容器(守护式运行)  
    -p # 宿主机端口:docker容器端口,将容器端口映射到宿主机上,通过宿主机访问内部端口
    --name # 给要运行的容器起的名字  
    redis:latest # 指定版本
    -it # 表示与容器进行交互式启动
    /bin/bash # 交互路径
    -v # 将容器内的指定文件夹挂载到宿主机对应位置
    ......
    
  • docker-composeway to run

    Example: Search wordpress, which can be run via docker-compose.ymldownload.

    Create a new one docker-compose.yml, copy the content in and save it, and then execute the installation command in this directory

    $ docker-compose up -d
    

    After the installation is complete, you can access

Guess you like

Origin blog.csdn.net/zz00008888/article/details/131683257