会用Docker的人都别装了,这多简单呐

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"学术又官方的说法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"没用过的人能看懂这段话? "}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用过的人为什么要看这段话? "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"还是要自己动手。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"试用完感受"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"试用了一下,就是下载运行官方制作的MySQL镜像,没自建镜像仓库,没制作镜像,用完觉得功能像是yum外加微型vmware。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"docker就是安装自带ENV软件包的yum"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d9/d9f9d43cbd0a2b93590f9fa11e93642b.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"左边的MySQL启动后,和服务器环境互相作用,像把一个新生儿放在了混合病房。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"右边的MySQL启动后,自带一套环境变量,和服务器环境互不影响,像把一个新生儿放进了保温箱,保温箱有自己的温度湿度氧气,保温箱再放进病房里。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"像yum"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们想安装某个软件,直接yum install 软件,如果yum源服务器有安装包,自动就安装上了,然后启动软件开始工作。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"docker方式类似的,我们想使用某个软件并且要求它以docker方式运行,那么先要docker search命令看docker仓库(官方仓库有大量软件镜像可以直接使用)是否有该软件的镜像, "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/16/1650af06bc02d81a3f66433a9d51c836.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有的话先将镜像pull到本地,然后run镜像,软件就开始工作了。这些镜像具有Build once,Run anyWhere的优势,和java机制很像。run起来的镜像被称作容器container,查看镜像用docker images命令,查看容器用docker ps命令。 "}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"像vmware"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"微型vmware是指,docker启动某个软件的镜像后,就相当于启动的这个软件,不同的是这个软件仿佛运行在一个和操作系统隔离的环境中,这个环境有自己的环境变量,并且它的环境变量不影响操作系统,只对本镜像内的软件有效,就好像每个软件都运行在各自的vmware虚拟机中,虚拟机之间互不影响。例如同一操作系统中有docker MySQL(docker启动的MySQL镜像)和docker nginx两个进程在工作,两个进程完全互不干涉,MySQL运行在给它配置好的环境里,nginx运行在给它配置好的环境里,一个的死活和变化和另一个无关,也和操作系统无关。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"说容易,我做一遍试试"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"安装Docker"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"下载libcgroup "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"搜索“libcgroup rpm” 。 RedHat系统就选择centos的安装包,下载 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"libcgroup-0.41-21.el7.x86_64.rpm。ftp上传到Redhat服务器。 "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0c/0c31545252acd62b005d6db9baadfc3c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安装libcgroup "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@bogon dockerab]# rpm -ivh libcgroup-0.41-21.el7.x86_64.rpm \nPreparing... ################################# [100%]\nUpdating / installing...\n1:libcgroup-0.41-21.el7 ################################# [100%]"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"下载docker包 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# wget https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm\n--2020-06-17 08:34:52-- https://get.docker.com/rpm/1.7.1/centos-6/RPMS/x86_64/docker-engine-1.7.1-1.el6.x86_64.rpm\nResolving get.docker.com... 99.86.193.96, 99.86.193.12, 99.86.193.128, ...\nConnecting to get.docker.com|99.86.193.96|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 4762280 (4.5M) [binary/octet-stream]\nSaving to: docker-engine-1.7.1-1.el6.x86_64.rpm\n\n16% [============> ] 801,777 35.5K/s eta 1m 49s "}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"安装docker "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# rpm -ivh docker-engine-1.7.1-1.el6.x86_64.rpm\nPreparing... ########################################### [100%]\n 1:docker-engine ########################################### [100%]"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"启动docker服务"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# service docker start\nStarting cgconfig service: [ OK ]\nStarting docker: [ OK ]"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"起个MySQL"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Docker启动了,相当于有了yum命令而已。yum当然要用来安装各种软件啦。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"查看仓库是否有MySQL "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"类似yum search命令,"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# docker search mysql\nNAME DESCRIPTION STARS OFFICIAL AUTOMATED\nmysql MySQL is a widely used, open-source relati... 9634 [OK] \nmariadb MariaDB is a community-developed fork of M... 3500 [OK]"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"下载MySQL镜像到本地 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# docker pull mysql\nlatest: Pulling from mysql\n8011c293a399: Downloading [> ] 277.6 kB/27.1 MB\n2455a1403f59: Download complete \n2f535805fb57: Download complete \nebf295599c35: Downloading [=========> ] 768.5 kB/4.178 MB"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果网络不好,需要等待时间较长,并且报错退出的话要反复执行docker pull mysql,直至下载成功。成功后会打印下面语句。 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"Digest: sha256:0648b8559643d01be3bfb0210ead5fdcb9a99ff7a6a1d5cb881bbd1f7f7b11bc\nStatus: Downloaded newer image for mysql:latest"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"查看镜像 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# docker images\nREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE\nmysql latest 9cd0a11a66df 7 days ago 540.6 MB"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到MySQL镜像下载成功。 "}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"启动MySQL镜像"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"启动命令各个参数含义略。 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@bogon ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql\n54fc423d7b42db8151e8fe916e33e6e0b0f5fd62fc4a455950f26d6a956a97e3"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"查看容器 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"理解成ps -ef | grep mysqld吧"}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@bogon ~]# docker ps\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\n54fc423d7b42 mysql \"docker-entrypoint.s 8 seconds ago Up 6 seconds 33060/tcp, 0.0.0.0:3306->3306/tcp backstabbing_ptolemy "}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"执行sql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"redhat没安装连MySQL的工具,没法测试。如果有,不用进入容器,在redhat环境就可以访问数据库。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在只能容器内试试了。docker exec进入容器,需要用到上面的CONTAINER ID,进入后可以查看容器的文件系统和环境变量。 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@bogon ~]# docker exec -it 54fc423d7b42 bash"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"成功进入MySQL 容器,连接数据库试试 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"root@54fc423d7b42:/# mysql -uroot -p123456\nmysql: [Warning] Using a password on the command line interface can be insecure.\nWelcome to the MySQL monitor. Commands end with ; or \\g.\nYour MySQL connection id is 8\nServer version: 8.0.20 MySQL Community Server - GPL\n\nCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.\n\nOracle is a registered trademark of Oracle Corporation and/or its\naffiliates. Other names may be trademarks of their respective\nowners.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nmysql> show databases;\n+--------------------+\n| Database |\n+--------------------+\n| information_schema |\n| mysql |\n| performance_schema |\n| sys |\n+--------------------+\n4 rows in set (0.00 sec)\n\nmysql>"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到此就能使用MySQL了。如果直接安装MySQL遇到的问题可能很多,最顺利的情况也就和刚才一样吧!并且每台服务器都要解决不一样的问题,现在就能初步感受到docker的方便之处了。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"容器内外环境对比"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/de/dec19cadec528f47b4d858206489967e.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看看容器内环境变量,变量很少 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"root@54fc423d7b42:/# env \nMYSQL_MAJOR=8.0\nHOSTNAME=54fc423d7b42\nPWD=/\nMYSQL_ROOT_PASSWORD=123456\nHOME=/root\nMYSQL_VERSION=8.0.20-1debian10\nGOSU_VERSION=1.12\nSHLVL=1\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n_=/usr/bin/env"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看文件系统就ls cd就可以了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"退出容器,回到操作系统,查看环境变量 "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"root@54fc423d7b42:/# exit\nexit\n[root@bogon ~]# env #操作系统的环境变量\nXDG_SESSION_ID=1\nHOSTNAME=bogon\nSELINUX_ROLE_REQUESTED=\nTERM=vt100\nSHELL=/bin/bash\nHISTSIZE=1000\nSSH_CLIENT=192.168.56.1 49661 22\nSELINUX_USE_CURRENT_RANGE=\nSSH_TTY=/dev/pts/0\nUSER=root\nLS_COLORS=rs=0:di=01;34:....\nSSH_AUTH_SOCK=/tmp/ssh-gSRfCMVd4x/agent.3732\nMAIL=/var/spool/mail/root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin\nPWD=/root\nLANG=en_US.UTF-8\nSELINUX_LEVEL_REQUESTED=\nHISTCONTROL=ignoredups\nSHLVL=1\nHOME=/root\nLOGNAME=root\nSSH_CONNECTION=192.168.56.1 49661 192.168.56.106 22\nLESSOPEN=||/usr/bin/lesspipe.sh %s\nXDG_RUNTIME_DIR=/run/user/0\n_=/usr/bin/env"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完全是两个世界,主环境没有一点安装过MySQL的痕迹,而这个服务器却实实在在的有MySQL的功能。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一些坑"}]},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":" 遇到报错"}]}]}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"Cannot connect to the Docker daemon. Is 'docker -d' running on this host?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":",重启docker "}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# service docker stop 直接restart也可\n[root@basicserver dockertest]# service docker start"}]},{"type":"numberedlist","attrs":{"start":"2","normalizeStart":"2"},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":" radhat7需要先安装libcgroup,然后安装docker的rpm包才能成功。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"3","normalizeStart":"3"},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":" redhat6无法运行本文版本的docker,当docker run时报错。所以如果安装本文版本的docker,Redhat版本要7以及更高。 "}]}]}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[root@basicserver dockertest]# docker run mysql #Redhat6 run不起来\nFATAL: kernel too old"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"谁再说不会用docker,非要一步一步配环境,装软件,就问他,“你会用yum吗?” "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"话说回来,刚开始用总感觉不踏实,和系统隔了一层,不如直接安装的东西手感好。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章