前面我们大致搞清楚了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 docker.io
启动守护进程
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://hub.docker.com/
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吧。