当前位置:首页 > CN2资讯 > 正文内容

docker linux 实现 docker 运行linux

9小时前CN2资讯


前面我们大致搞清楚了docker的形态、基本构成,使用的基本流程。接下来我们需要练个手,在实际使用中体会Docker,使我们对它的认识更全面。

  一般在实际项目应用中都是在Linux系统上部署Docker的,所以我们在linux上练习。目前几乎所有Linux系统(RHEL/Centeros、Debian/Ubuntu、gentoo、arch linux)都支持Docker。以Linux内核的,用到Linux内核3.8以上才支持Docker。除了RHEL/Centeros,内核为2.6.32-431的RHEL/Centeros6.5可以支持Docker。

在接下来的练习中,使用的是博主的Azure虚拟机Linux (ubuntu 18.04)。

一、安装Docker

先查看linux内核

uname -r

使用apt命令,先更新下apt

sudo apt-get update

然后安装docker

sudo apt-get install

启动守护进程

sudo service docker start docker start/running. process 3050

运行helloworld程序

sudo docker run hello-world

查看docker是否安装成功

docker version

为了不用每次执行docker都要加sudo,可以将用户名加到docker组中,例如,yangyoushanPC0是我的用户名。

yangyoushanPC0@yangyoushanVPC001:~$ sudo group add docker [sudo] password for yangyoushanPC0: sudo: group: command not found yangyoushanPC0@yangyoushanVPC001:~$ sudo groupadd docker groupadd: group 'docker' already exists yangyoushanPC0@yangyoushanVPC001:~$ sudo gpasswd -a yangyoushanPC0 docker Adding user yangyoushanPC0 to group docker yangyoushanPC0@yangyoushanVPC001:~$ newgrp docker

测试一下,运行hello-world,不用加sudo了,

yangyoushanPC0@yangyoushanVPC001:~$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https:/// For more examples and ideas, visit: https://docs.docker.com/get-started/

查看镜像 docker images

yangyoushanPC0@yangyoushanVPC001:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 11 months ago 1.84kB

 

二、测试搭建WordPress

WordPress是一款php开发的博客平台,数据库mysql。提供了很多第三方模板共用户使用。一般演示云计算PaaS平台时,比较喜欢用这个项目做例子。这里我们不研究他的技术结构,只当作一个例子使用。

拉取wordpress镜像

docker pull wordpress

yangyoushanPC0@yangyoushanVPC001:~$ docker pull wordpress Using default tag: latest latest: Pulling from library/wordpress 000eee12ec04: Pull complete 8ae4f9fcfeea: Pull complete 60f22fbbd07a: Pull complete ccc7a63ad75f: Pull complete a2427b8dd6e7: Pull complete 91cac3b30184: Pull complete d6e40015fc10: Pull complete 3951eb02eb9d: Pull complete a30c5ce4d825: Pull complete 1d2fc8e19e4d: Pull complete 8c746235d858: Pull complete 887411e72bcf: Pull complete eba633920d44: Pull complete 9b7007daa55e: Pull complete 4d20187eeb14: Pull complete 3e1b35074ec2: Pull complete a13668f53ada: Pull complete 38760061f7fb: Pull complete 9448b0eeecae: Pull complete e75a2465ca33: Pull complete e8661141192e: Pull complete Digest: sha256:add5816d1c04fdf1509e298af0ec16f8485cd165292bd4245ffdbb9a1db87429 Status: Downloaded newer image for wordpress:latest

查看镜像

docker images

yangyoushanPC0@yangyoushanVPC001:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE wordpress latest b9db6e8f3175 3 days ago 539MB hello-world latest fce289e99eb9 11 months ago 1.84kB

前面我们知道,下载的完整文件系统和程序包,叫做镜像;运行程序包叫做容器。

开始以这个镜像启动一个容器,让这个容器运行在8080端口

docker run -it --name yysWordpress -p 8080:80 -d wordpress

然后查看已运行的容器

docker ps -a

yangyoushanPC0@yangyoushanVPC001:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 82c909c79d8c wordpress "docker-entrypoint.s鈥 51 seconds ago Up 49 seconds 0.0.0.0:8080->80/tcp yysWordpress

如下在浏览器输入:http://52.179.83.77:8080/,52.179.83.77是我的虚拟机公网IP。

如果访问不通,而且部署使用的是云虚拟机,先检查云虚拟机配置的安全组的入站规则,是否开通了协议和端口,例如,我开通了8080的访问权限。

打开网页成功。

选择 简体中文,继续,

程序部署成功。

 

接下来就要配置数据库,

拉取mysql镜像,指定版本号

docker pull mysql:5.7

yangyoushanPC0@yangyoushanVPC001:~$ docker pull mysql:5.7 5.7: Pulling from library/mysql d599a449871e: Pull complete f287049d3170: Pull complete 08947732a1b0: Pull complete 96f3056887f2: Pull complete 871f7f65f017: Pull complete 1dd50c4b99cb: Extracting [==============================> ] 7.471MB/12.11MB 5bcbdf508448: Download complete 02a97db830bd: Download complete c09912a99bce: Download complete 08a981fc6a89: Download complete 818a84239152: Download complete

查看镜像

docker images

yangyoushanPC0@yangyoushanVPC001:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE wordpress latest b9db6e8f3175 3 days ago 539MB hello-world latest fce289e99eb9 11 months ago 1.84kB

镜像准备ok,启动容器

docker run -it --name=mysql5.7 -p 6060:6061 -e MYSQL_ROOT_PASSWORD=你自己的密码 -e serverTimezone=Asia/Shanghai -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci  --lower_case_table_names=1

设置端口,设置密码,设置时区

yangyoushanPC0@yangyoushanVPC001:~$ docker run -it --name=mysql5.7 -p 6060:6061 -e MYSQL_ROOT_PASSWORD=9527 -e serverTimezone=Asia/Shanghai -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci --lower_case_table_names=1 Unable to find image 'mysql:5.7' locally 5.7: Pulling from library/mysql d599a449871e: Pull complete f287049d3170: Pull complete 08947732a1b0: Pull complete 96f3056887f2: Pull complete 871f7f65f017: Pull complete 1dd50c4b99cb: Pull complete 5bcbdf508448: Pull complete 02a97db830bd: Pull complete c09912a99bce: Pull complete 08a981fc6a89: Pull complete 818a84239152: Pull complete Digest: sha256:5779c71a4730da36f013a23a437b5831198e68e634575f487d37a0639470e3a8 Status: Downloaded newer image for mysql:5.7 c5ee950e7b67fb87246fc898e5c7a985a8a05bbb26551b2a998840388f04c773

查看容器

docker ps -a

yangyoushanPC0@yangyoushanVPC001:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fd6daa9f1b2a mysql:5.7 "docker-entrypoint.s鈥 34 seconds ago Up 30 seconds 33060/tcp, 0.0.0.0:3305->3306/tcp mysql5.7 82c909c79d8c wordpress "docker-entrypoint.s鈥 About an hour ago Up About an hour 0.0.0.0:8080->80/tcp

修改mysql设置,允许外部访问

yangyoushanPC0@yangyoushanVPC001:~$ docker exec -it mysql5.7 bash root@fd6daa9f1b2a:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.28 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; Query OK, 0 rows affected (0.01 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)

虚拟机上的,或者说服务器上mysql都安装好了,我们需要一个客户端管理,查看mysql数据库。可以在本地PC安装一个mysql管理工具,我一般使用navicat。

navicat上连接上刚才配置的mysql数据库。

连接上后,创建一个database

返回wordpress页面,配置数据库信息

配置OK,点击提交。

现在可以在网站上继续安装wordpress了,

根据提示填写你的信息

安装成功后返回页面登陆进去后

再测试一下发布文章

 再看数据库,已经自动创建了表

个人网站成功创建了。大家也可以直接访问http://52.179.83.77:8080/查看测试的结果(注意:博主的虚拟机大约一年左右,如果到期就看不了啦!)。

这样我们使用现有的镜像,启用了容器,部署了一个个人博客网站。是不是很有趣,接下来我们就详细学习Docker吧。

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/25610.html

    分享给朋友:

    “docker linux 实现 docker 运行linux” 的相关文章

    DMIT VPS评测:高性能与稳定性的完美结合

    在了解DMIT VPS之前,我想先分享一下我对这家公司的最初印象。记得第一次听到DMIT时,它的名字总是在VPS领域中流传。人们提到它时,无一不带着敬仰,增加了我对它的好奇心。自从它在2017年成立以来,DMIT便以其优秀的服务和产品迅速吸引了不少用户。我也开始关注起它背后的故事。 DMIT的崛起显...

    水牛VPS:高性能虚拟专用服务器的最佳选择与比较

    水牛城VPS,顾名思义,是在美国纽约州布法罗市托管的虚拟专用服务器。这种服务器因其独特的地理位置和优越的技术配置,吸引了众多用户,特别是需要高性能和灵活性的网站和应用程序。这类服务的定义非常简单,但其特点却非常丰富。通常来说,水牛城VPS提供了良好的网络带宽、灵活的存储选项,以及能够根据用户需求进行...

    Digital-VM优惠码:解锁超值VPS主机服务的最佳选择

    Digital-VM成立于2019年初,专注于为用户提供基于KVM架构的VPS主机服务。在这短短的几年中,它已经迅速崛起,成为业界的一颗新星。作为一个技术驱动的品牌,Digital-VM不断创新,以满足各种客户需求,提供高性能、灵活性和可靠性的VPS解决方案。 我觉得Digital-VM的成长路程相...

    提升跨境业务体验:CN2GIA全球互联网接入服务解析

    在当今数字化时代,网络服务的质量直接影响着企业的发展。CN2GIA,即“Global Internet Access”,是中国电信为了提升国际网络服务而推出的一项高端业务。为了满足不断增长的国际市场需求,CN2GIA 的出现标志着中国电信在建设下一代网络上的重要一步。它的目标是为用户提供更优质的国际...

    便宜日本VPS的选择与比较:性价比高的供应商推荐

    在当今数字化时代,VPS(虚拟专用服务器)正成为许多企业和个人的首选解决方案,尤其是在日本市场。对于那些需要可靠网络服务的用户来说,这无疑是一个非常实用的选择。日本的VPS服务以其高性能和稳定性著称,深受开发者、小型企业、网站管理员等用户的喜爱。 日本VPS的定义并不复杂,简单来说,它是一种虚拟化技...

    物语云:为游戏行业提供高效安全的云计算解决方案

    物语云概述 物语云是北京物语云联网络科技有限公司旗下的云计算品牌。这一品牌专注于为游戏行业提供一系列数据中心基础服务和互联网业务解决方案,其产品线包括专属服务器租用、云服务器、虚拟主机、服务器托管及带宽租用等。物语云的目标在于为客户提供高效、安全、经济的云计算服务,从而帮助他们在激烈的市场竞争中立足...