Time is so precious to learn a new skill in an hour! What about ten minutes? This article records several scenarios that need to be quickly used in work docker , and really helps you get started in ten minutes!
  • Scenario 1: Understanding what Docker is

    If you don’t know much about Docker, you should search it yourself. This article is based on what you at least know about Docker; Docker is a tool for creating a unified development or production environment through standard configuration files, and its core is the image (already configured Well various environments).

  • Scenario 2: Configure Docker domestic image

    Nothing to say, go directly to the method

    If you are installing a Mac or Windows client, you can configure the mirror address as shown in the figure

    image.png

Under Linux:

 # vi /etc/docker/daemon.json
{
    "registry-mirrors": ["http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"]
}
# 然后根据你是Ubuntu还是Centos等其他系统,重启docker服务即可
  • Scenario 3: Quickly find and download the Docker environment you need

     # 查找环境(后面的 `--no-trunc` 是为了完整输出描述信息)
    # 你也可以去 `https://hub.docker.com/` 网站搜索,搜索功能更强大
    docker search mysql --no-trunc
    # 搜索某个镜像的tags(这个docker并未提供原生命令,解决方案都是利用docker提供的开放api进行搜索,或者去 `https://hub.docker.com/` 查阅
    # 本命令来自 `https://stackoverflow.com/questions/28320134/how-can-i-list-all-tags-for-a-docker-image-on-a-remote-registry`)
    wget -q https://registry.hub.docker.com/v1/repositories/ubuntu/tags -O -  | sed -e 's/[][]//g' -e 's/"//g' -e 's/ //g' | tr '}' '\n'  | awk -F: '{print $3}'
    # 下载某个镜像 
    docker pull mysql
    # 下载某个特定 `tags`(通常是版本号)的镜像,强烈建议制定 `tags` 下载,尤其是生产环境
    docker pull mysql:8.0
    # 删除某个镜像
    docker rmi mysql
  • Scenario 4: Create, run and enter a container based on an image

     # 查看所有容器
    docker ps -a # -a 参数可以查看已停止运行的容器
    # 基于某个镜像创建容器并运行
    docker create -it --name='ubuntu-20' ubuntu  # 参数 `-it` 标识了可以支持标准输入输出,通常一起使用 `--name` 为该容器起个名字,强烈建议一定起一个独立名字
    docker start ubuntu-20 # 启动该容器
    docker exec -it ubuntu-20 /bin/bash # 在该容器运行交互命令行工具,即可进入容器
    
    # 快速创建并运行
    # 这是一条整合命令,相当于上面的三条,特别注意,如果不添加 `-d` 参数,运行后直接进入到容器交互模式,如果此时exit退出,则容器会自动停止,`-d` 是保证后台运行
    docker run -it -d --name='ubuntu-20' ubuntu /bin/bash
    # 容器操作是docker中最复杂的功能,需要多多学习研究
  • Scenario 5: Mapping local ports and file directories

     # Docker 最核心的功能就是代替宿主机运行我们的服务(尤其是微服务),所以要开放端口给外界能够访问,开发环境中还要开放目录方便代码及配置共享,调试发布镜像
    
    # `-P` 自动映射所有开放的端口, 映射多个直接指定多个 `--expose` 参数即可
    docker run -it -d -P --name=port-test --expose=8000 --expose=8001 ubuntu /bin/bash
    # `-p` 指定端口映射,通常我们都是使用-p指定端口,方便自动化运维
    docker run -it -d -p 8000:8000 --name=port-test ubuntu /bin/bash
    
    # 查看容器映射的端口
    docker port port-test
    # 8000/tcp -> 0.0.0.0:55002
    # 8000/tcp -> :::55002
    
    # 映射目录Docker提供了两个参数,分别是不同的方式,其中 `-v` 足够平时开发调试使用,如果是使用更加完善的文件管理,请使用 `--mount` 参数,这里只记录下 `-v` 的使用:
    docker run -it -d -v /path/to/source/test:/www --name=share-test ubuntu /bin/bash
    # 进入容器后,可发现 `/www` 可访问
  • Scenario 6: Fixed Docker container IP for easier automation

    After Docker is installed, three network types will be created, one of which can be specified when creating a container;
    When docker run, you can specify the network card type through the --network parameter
     $ docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    e6eaace1cd3e   bridge    bridge    local  #创建虚拟网卡进行通信
    a6a9b06010ae   host      host      local  #与主机共享网卡,特别提醒:该模式仅支持Linux系统,Mac下指定该模式后容器内的IP与主机不一致,且主机无法访问容器内
    547191ac0139   none      null      local  #无网络链接

    Non-special scenarios (such as high network requirements) are generally configured as bridge. In this mode, restarting the container by default will re-assign IP, and the default virtual network card --ip The parameters are invalid and must be created by creating a Customize the virtual network card to achieve the purpose of fixed IP

     # 指定网卡网段,注意下面两条命令分别创建了B类和C类IP两个网段的网卡,具体了解可自行百度,实际使用可选用 `--subnet=172.18.0.0/16` B类网段,IP地址很多,足够用了
    docker network create --subnet=172.18.0.0/16 mynetwork
    docker network create --subnet=192.168.22.0/24 mynetwork192
    
    # 创建容器并指定IP
    docker run -itd --name net172 --net=mynetwork --ip=172.18.0.2 ubuntu /bin/bash

Original address: https://www.j2do.com/post/docker/


MStone
334 声望33 粉丝

全栈攻城狮,PHP++GO+前端,长期工作于创业公司!