zoukankan      html  css  js  c++  java
  • docker 学习

    以下步骤,测试环境是windows 10,  + docker DockerToolbox-18.03.0-ce

    注意docker 自带viturlbox 可能存在兼容性问题,建议安装完docker 后,安装最新的vitriul box 版本

    在zhou 专家的指导下,完成了docker 搭建和mysql imager 导入,docker 启动mysql, docker 实例化 持久化数据文件

    ###linux 版本 docker 可以参考如下文档

    https://www.cnblogs.com/feiyun8616/p/9870390.html

    ###windows 10 强依赖快捷键

    1.windows 10 安装 docker 碰到的问题如下:

    问题1.in windows ,不一定需要hype-v no need hype-v ,j 只需要自带的vitual box ust vitual box

    Docker Quickstart Terminal 以管理员 运行时候,会提示如下报错:

    default) Downloading C:Usersdd.dockermachinecacheoot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v19.03.12/boot2docker.iso.


    感谢 菜到怀疑人生 https://blog.csdn.net/dhaiuda/article/details/81975550
    解决办法
    step 1:copy D:Program FilesDocker Toolboxoot2docker.iso to C:Usersdd.dockermachinecache

    step 2: 断开网络,继续用 管理员角色 运行 Docker Quickstart Terminal


    Running pre-create checks...
    (default) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp: lookup api.github.com: no such host
    Creating machine...
    (default) Unable to get the latest Boot2Docker ISO release version: Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp: lookup api.github.com: no such host
    (default) Copying C:Usersdd.dockermachinecacheoot2docker.iso to C:Usersdd.dockermachinemachinesdefaultoot2docker.iso...
    (default) Creating VirtualBox VM...
    (default) Creating SSH key...
    (default) Starting the VM...
    (default) Check network to re-create if needed...
    (default) Windows might ask for the permission to create a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
    (default) Found a new host-only adapter: "VirtualBox Host-Only Ethernet Adapter #2"
    (default) Windows might ask for the permission to configure a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
    (default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
    (default) Waiting for an IP...
    Waiting for machine to be running, this may take a few minutes...
    Detecting operating system of created instance...
    Waiting for SSH to be available...
    Detecting the provisioner...
    Provisioning with boot2docker...
    Copying certs to the local machine directory...
    Copying certs to the remote machine...
    Setting Docker configuration on the remote daemon...
    Checking connection to Docker...
    Docker is up and running!
    To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: D:Program FilesDocker Toolboxdocker-.exe env default

    ## .
    ## ## ## ==
    ## ## ## ## ## ===
    /"""""""""""""""""\___/ ===
    ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
    \______ o __/
    __/
    \____\_______/

    docker is configured to use the default machine with IP 192.168.99.100
    For help getting started, check out the docs at https://docs.docker.com


    Start interactive shell (安装成功)

    dd@R304AH9E MINGW64 /d/Program Files/Docker Toolbox
    $ Docker Quickstart Terminal

    查看docker info
    dd@R304AH9E MINGW64 /d/Program Files/Docker Toolbox

    $ docker info
    Containers: 0
    Running: 0
    Paused: 0
    Stopped: 0
    Images: 0
    Server Version: 18.03.0-ce
    Storage Driver: aufs
    Root Dir: /mnt/sda1/var/lib/docker/aufs
    Backing Filesystem: extfs
    Dirs: 0
    Dirperm1 Supported: true
    Logging Driver: json-file
    Cgroup Driver: cgroupfs
    Plugins:
    Volume: local
    Network: bridge host macvlan null overlay
    Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
    Swarm: inactive
    Runtimes: runc
    Default Runtime: runc
    Init Binary: docker-init
    containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c
    runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
    init version: 949e6fa
    Security Options:
    seccomp
    Profile: default
    Kernel Version: 4.9.89-boot2docker
    Operating System: Boot2Docker 18.03.0-ce (TCL 8.2.1); HEAD : 404ee40 - Thu Mar 22 17:12:23 UTC 2018
    OSType: linux
    Architecture: x86_64
    CPUs: 1
    Total Memory: 995.6MiB
    Name: default
    ID: QFYZ:DGR4:NUTN:NXAM:FO7P:SWMD:4RFG:T42S:U6EL:YWAC:KPBM:7D7H
    Docker Root Dir: /mnt/sda1/var/lib/docker
    Debug Mode (client): false
    Debug Mode (server): false
    Registry: https://index.docker.io/v1/
    Labels:
    provider=virtualbox
    Experimental: false
    Insecure Registries:
    127.0.0.0/8
    Live Restore Enabled: false

    $ docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE

    dd@R304AH9E MINGW64 /d/Program Files/Docker Toolbox

    (进入pwd,)
    $ pwd
    /d/dock_file

    (到了impage 目录)
    dd@R304AH9E MINGW64 /d/dock_file
    $ ls
    img-mysql57


    dd@R304AH9E MINGW64 /d/dock_file

    (载入 mysql_image)
    $ docker load -i img-mysql57
    13cb14c2acd3: Loading layer [==================================================>] 72.49MB/72.49MB
    365386a39e0e: Loading layer [==================================================>] 338.4kB/338.4kB
    c3f46b20a0d3: Loading layer [==================================================>] 9.539MB/9.539MB
    66c45123fd43: Loading layer [==================================================>] 4.2MB/4.2MB
    61cbb8ea6481: Loading layer [==================================================>] 1.536kB/1.536kB
    44853bb67274: Loading layer [==================================================>] 53.75MB/53.75MB
    3a2464d8e0c0: Loading layer [==================================================>] 6.656kB/6.656kB
    91ae264962fb: Loading layer [==================================================>] 3.584kB/3.584kB
    8f0182ef7c8c: Loading layer [==================================================>] 313.1MB/313.1MB
    ac7657905788: Loading layer [==================================================>] 16.38kB/16.38kB
    c90a34afcab0: Loading layer [==================================================>] 1.536kB/1.536kB
    Loaded image: mysql:5.7.30

    3、查看本地镜像
    $ docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql 5.7.30 9cfcce23593a 2 months ago 448MB


    删除镜像
    docker rm -f 7c04d746f308

    安装完成后,我们可以使用以下命令来创建 mysql 容器:
    docker run -itd --name mysql-test -p 8001:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30

    $ docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6b041197f432 mysql:5.7.30 "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 33060/tcp, 0.0.0.0:8001->3306/tcp mysql-test

    ##windows docker command 查看docker的映射 地址
    ### https://www.runoob.com/docker/docker-machine.html

    ###https://www.cnblogs.com/sparkdev/p/7044950.html

    (感谢 sparkdev)

    Docker Machine 是 Docker 官方提供的一个工具,它可以帮助我们在远程的机器上安装 Docker,或者在虚拟机 host 上直接安装虚拟机并在虚拟机中安装 Docker。我们还可以通过 docker-machine 命令来管理这些虚拟机和 Docker。下面是来自 Docker Machine 官方文档的一张图,很形象哦!


    、进入机器
    $ docker-machine ssh default

    $ docker-machine ip default
    192.168.99.100


    进入docker image 界面 (6d34a5cad5b6)来自 CONTAINER ID
    $ docker exec -it 6b041197f432 /bin/bash
    root@6d34a5cad5b6:/#

    启动下dock conn_id。已经创建好的docker conn
    docker start 6b041197f432


    停下dock conn_id
    docker stop 6b041197f432


    $ docker-machine stop
    Stopping "default"...

    #进入linux 宿主机器
    $ docker-machine ssh default
    ###
    #安装完成后,我们可以使用以下命令来运行 mysql 容器:
    #docker run -itd --name mysql-test -p 8001:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.30

    #创建container
    #docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.30 -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d

    #删除container
    docker rm -f 7c04d746f308


    ##安装mysql
    cd /d/dock_file
    (载入 mysql_image)
    $ docker load -i img-mysql57
    $ docker images

    #创建container
    # 可以使用相同的技术来持久化服务器的配置。以下命令创建一个MySQL Server容器,并绑定装载数据目录和服务器配置文件:
    #这种办法方便使用 宿主linux 服务器上命令 编辑my.cnf , 或者修改mysql 配置参数 参数
    ##docker run --name mysql1 -d mysql:5.7.30 --character-set-server=utf8mb4 --collation-server=utf8mb4_col

    ##创建container
    ##docker run --name mysql2 -v /data04/docker/mysql:/var/lib/mysql -v /opt/tianhe/mysql/my.cnf:/etc/mysql/my.cnf -v /opt/data:/opt/data -e "MYSQL_ROOT_PASSWORD=123456" -d mysql:5.7.30

    ##创建 持久化服务器 container 持久化服务器,也就是映射到宿主机,使得配置文件可以编辑
    ##https://dev.mysql.com/doc/refman/5.7/en/docker-mysql-more-topics.html
    ##这里要注意2点。1./opt/docker_v/mysql/conf/my.cnf 有如下2行数据
    ##[mysqld]
    ##user=mysql
    ##2. /opt/docker_v/data 目录为空
    docker run --name mysqlserver -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -v /opt/docker_v/logs:/logs -v /opt/docker_v/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 8804:3306 mysql:5.7.30


    ###进入mysql容器,并登陆mysql

    docker exec -it mysqlserver bash
    mysql -uroot -p

    ###开启远程访问权限
    use mysql;
    select host,user from user;
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
    flush privileges;

    ##https://dev.mysql.com/doc/refman/5.7/en/docker-mysql-more-topics.html
    ##官方文档,重要
    #持久化数据和配置更改。
    #Docker容器原则上是短暂的,如果容器被删除或损坏,预计任何数据或配置都会丢失(请参阅此处的讨论)。
    #但是,Docker卷提供了一种机制来持久化在Docker容器内创建的数据。在初始化时,MySQL Server容器会为服务器数据目录
    #创建一个Docker卷。
    #在容器上运行docker specect命令的JSON输出有一个mount键,该键的值提供有关数据目录卷的信息

    ##检查配置文件
    ##有映射关系的绑定。
    docker inspect mysqlserver
    "Mounts": [
    {
    "Type": "bind",
    "Source": "/opt/docker_v/mysql/conf",
    "Destination": "/etc/mysql/conf.d",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
    },
    {
    "Type": "bind",
    "Source": "/opt/docker_v/logs",
    "Destination": "/logs",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
    },
    {
    "Type": "bind",
    "Source": "/opt/docker_v/data",
    "Destination": "/var/lib/mysql",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
    }

    ##没有映射关系的绑定
    docker inspect mysql1

    "Mounts": [
    {
    "Type": "volume",
    "Name": "54403f8b9e25c30c7271aa43a853ba4af36532f0c4f91ecaeaac02eb6fa9a44a",
    "Source": "/mnt/sda1/var/lib/docker/volumes/54403f8b9e25c30c7271aa43a853ba4af36532f0c4f91ecaeaac02eb6fa9a44a/_data",
    "Destination": "/var/lib/mysql",
    "Driver": "local",
    "Mode": "",
    "RW": true,
    "Propagation": ""


    $ docker exec -it 8d19af511af7 /bin/bash


    ##如果要允许多一个docker 互相访问,需要设置要给network ,如下所示
    #通过设置一个Docker网络,您可以允许多个多个容器之间进行通信,以便在另一个Docker容器中的客户机应用程序可以访问服务器容器中的mysql服务器。首先创建一个基于网络的网络:

    docker network create my-custom-net
    ##Then, when you are creating and starting the server and the client containers, use the --network option to put them on network you created. For example:

    docker run --name=mysql1 --network=my-custom-net -d mysql/mysql-server
    docker run --name=myapp1 --network=my-custom-net -d myapp
    ##The myapp1 container can then connect to the mysql1 container with the mysql1 hostname and vice versa, as Docker automatically sets up a DNS for the given container names. In the following example, we run the mysql client from inside the myapp1 container to connect to host mysql1 in its own container:

    docker exec -it myapp1 mysql --host=mysql1 --user=myuser --password
    ###For other networking techniques for containers, see the Docker container networking section in the Docker Documentation.

    https://www.cnblogs.com/liubiaos/p/9408830.html
    问题2:
    ##感谢blackx
    ######解决windows系统无法对docker容器进行端口映射的问题 https://www.cnblogs.com/hypnus-ly/p/8683215.html

    2、问题出现的原因:

    The reason you’re having this, is because on Linux, the docker daemon (and your containers) run on the Linux machine itself, so “localhost” is also the host that the container is running on, and the ports are mapped to.

    On Windows (and OS X), the docker daemon, and your containers cannot run natively, so only the docker client is running on your Windows machine, but the daemon (and your containers) run in a VirtualBox Virtual Machine, that runs Linux.

    因为docker是运行在Linux上的,在Windows中运行docker,实际上还是在Windows下先安装了一个Linux环境,然后在这个系统中运行的docker。也就是说,服务中使用的localhost指的是这个Linux环境的地址,而不是我们的宿主环境Windows。

    3、解决方法:

      通过命令

      docker-machine ip default # 其中,default 是docker-machine的name,可以通过docker-machine -ls 查看

    找到这个Linux的ip地址,一般情况下这个地址是192.168.99.100,然后在Windows的浏览器中,输入这个地址,加上服务的端口即可启用了。

    比如,首先运行一个docker 容器:

      docker run -it -p 8888:8888 conda:v1


    ##问题3
    感谢https://www.pianshen.com/article/67611411335/

    重启windows , 启动docker 报错 VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component MachineWrap, interface IMachine

    $ docker-machine start default
    Starting "default"...
    (default) Check network to re-create if needed...
    (default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
    Unable to start the VM: C:Program FilesOracleVirtualBoxVBoxManage.exe startvm default --type headless failed:
    VBoxManage.exe: error: The virtual machine 'default' has terminated unexpectedly during startup with exit code -1073741819 (0xc0000005). More details may be available in 'C:Usersdb.dockermachinemachinesdefaultdefaultLogsVBoxHardening.log'
    VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component MachineWrap, interface IMachine

    Details: 00:02:17.215142 GUI: Passing request to close Runtime UI from machine-logic to UI session.


    #https://github.com/docker/toolbox/issues/473

    #I've tried all the ways above.
    #If u couldn't find a way to solve Error: Details: code E_FAIL (0x80004005), try this.

    #Delete Docker Toolbox, Virtual Box
    #Delete everything in the folder, Users/your_user_name/Documents/.docker/ (@aliskov commented)
    #Delete (default) Found a new host-only adapter: "VirtualBox Host-Only Ethernet Adapter #2"
    #Restart your system
    #Install Docker Toolbox in ADMIN MODE!! <-- this is the way I found!!

    感谢裴大帅2020的博客
    解决方法一:
    卸载掉docker Toolbox以及VirtualBox,重新安装后这个问题就可以解决了,这个方法10%的成功率。

    解决方法二:
    卸载掉VirtualBox,然后从https://www.virtualbox.org/wiki/Downloads 更新最新版本安装,安装完成后再启动docker的客户端。

    #####问题4
    宿主机没有root 密码

    解决办法:
    sudo passwd root


    ###问题4
    docker start mysql_container 失败
    ##感谢https://www.dazhuanlan.com/2020/02/10/5e406255898fa/

    docker@default:~$ docker start 8d19af511af7
    8d19af511af7

    docker@default:~$ docker exec -it 8d19af511af7 /bin/bash
    Error response from daemon: Container 8d19af511af729d24d94a6db3aa38173e372bf76e5f6c588604539d368f6a58d is not running


    分析错误日志:

    已挂载来自主机的服务器配置文件,但该文件不包含系统变量LOG_ERROR(请参见绑定挂载服务器配置文件时保持数据和配置更改)。
    未装载主机中的服务器配置文件,但Docker环境变量MYSQL_LOG_CONSOLE为TRUE(MySQL 5.7服务器容器的变量默认状态为FALSE)。
    然后,MySQL Server的错误日志被重定向到stderr,以便错误日志进入Docker容器的日志,并且可以使用docker logs mysqld-tainer
    命令查看。


    要使MySQL Server在这两个条件之一为真时生成错误日志,请使用--log-error选项将服务器配置为在容器内的特定位置生成错误日志。
    要持久化错误日志,请按照持久化数据和配置更改中的说明,在容器内的错误日志位置装入主机文件。但是,您必须确保其容器内
    的MySQL Server对挂载的主机文件具有写入权限。

    报错如下:
    docker logs mysql1
    2020-08-25 02:36:59+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
    2020-08-25 02:36:59+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
    command was: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_col --verbose --help
    2020-08-25T02:36:59.919061Z 0 [ERROR] Unknown collation: 'utf8mb4_col'
    2020-08-25T02:36:59.922900Z 0 [ERROR] Aborting


    修复方法:

    1.宿主机 持久化配置文件,以方便编辑my.cnf

    2.编辑my.cnf .修复字符集编码
    [client]
    default-character-set=utf8mb4

    [mysql]
    default-character-set=utf8mb4

    [mysqld]
    character-set-server=utf8mb4
    collation-server=utf8mb4_general_ci

    https://www.dazhuanlan.com/2020/02/10/5e406255898fa/


    ###问题5
    docker start mysqlserver 报错

    检查日志:
    docker logs mysqlserver

    2020-08-25 07:59:14+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
    command was: mysqld --verbose --help
    2020-08-25T07:59:14.556572Z 0 [ERROR] unknown variable 'prompt=u@h : d >'


    解决方法:

    参考文档 https://www.jb51.net/article/27922.htm

    配置文件里加入如下:

    [mysql] 

    prompt=(\u@\h) [\d]>\_ 

  • 相关阅读:
    InnoSetup 打包代码 检测.netFramework
    PartialView中的页面重定向
    Cocos2dx 学习之引擎介绍
    30个高质量的免费jquery滑块PSD文件
    HBase Shell
    图灵百年诞辰 1912.6.232012.6.23
    常用的数据分页技术及比较
    Cocos2dx学习之windows 7的visual studo 2010开发环境安装
    C#实现简易ajax调用后台方法
    AJAX(Professional ASP.NET MVC 3
  • 原文地址:https://www.cnblogs.com/feiyun8616/p/13560577.html
Copyright © 2011-2022 走看看