概述
在vmware的centos7上安装了docker,通过docker来部署几个应用,记录一下部署过程,方便以后查看。
Docker的作用
我们写的代码会接触到好几个环境:开发环境(开发人员使用)、测试环境(测试人员使用)以及生产环境(运维人员使用),环境与环境之间可能有差别,如jdk版本的不同。
docker是一种容器技术,解决环境迁移问题,即把环境也一起打包进容器。
端口映射
外部机器是无法直接访问到容器的。当容器中的网络服务需要被外部机器访问时,可以将容器中提供服务的端口映射到宿主机的端口上。外部机器访问宿主机的端口,从而间接访问容器的服务。
如图所示,容器的3306端口映射到宿主机的3307端口,外部机器访问宿主机的3307端口就能访问容器。

数据卷
数据卷是宿主机中的一个目录或文件,当容器目录和数据卷目录绑定后,对方的修改会立即同步。一个数据卷可以被多个容器同时挂载;一个容器也可以被挂载多个数据卷。
作用:
- 容器数据持久化
- 外部机器和容器间接通信
- 容器之间数据交换
MySQL
步骤
- 搜索mysql镜像
- 拉取mysql镜像
- 创建容器,设置端口映射、目录映射(在/root目录下创建mysql目录用于存储mysql数据信息)
[root@centos7 mysql]# docker run -id \
> -p 3307:3306 \
> --name=c_mysql \
> -v $PWD/conf:/etc/mysql/conf.d \
> -v $PWD/logs:/logs \
> -v $PWD/data:/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD=123456 \
> mysql:5.6
- -p 3307:3306:将容器的 3306 端口映射到宿主机的 3307 端口。
- -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的
/etc/mysql/my.cnf。配置目录 - -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。日志目录
- -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。数据目录
- -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码
- 进入容器,操作mysql
- 使用外部机器连接容器中的mysql
[czn@centos7 ~]$ su root
Password:
[root@centos7 czn]# cd /
[root@centos7 /]# cd /etc/docker
[root@centos7 docker]# docker images
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@centos7 docker]# systemctl start docker
[root@centos7 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 5.0 5a3c8e192943 10 days ago 98.4MB
centos 7 8652b9f0cb4c 5 weeks ago 204MB
[root@centos7 docker]# docker pull mysql:5.6
5.6: Pulling from library/mysql
e50c3c9ef5a2: Pull complete
94069ccb8535: Pull complete
ca1ed35f7ae1: Pull complete
b8d6304f5ee0: Pull complete
5df200debe65: Pull complete
0c86983a5f50: Pull complete
b7c8ecf8cbd3: Pull complete
a1fa953f1f76: Pull complete
a49eac7c4004: Pull complete
2d0ede289384: Pull complete
eb1351cc5fcd: Pull complete
Digest: sha256:51d59639d9a864ec302008c81d6909c532eff8b946993ef6448c086ba7917e82
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
[root@centos7 docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 0ebb5600241d 6 hours ago 302MB
redis 5.0 5a3c8e192943 10 days ago 98.4MB
centos 7 8652b9f0cb4c 5 weeks ago 204MB
[root@centos7 docker]# pwd
/etc/docker
[root@centos7 docker]# mkdir ~/mysql
[root@centos7 docker]# cd ~/mysql
[root@centos7 mysql]# pwd
/root/mysql
[root@centos7 mysql]# docker run -id \
> -p 3307:3306 \
> --name=c_mysql \
> -v $PWD/conf:/etc/mysql/conf.d \
> -v $PWD/logs:/logs \
> -v $PWD/data:/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD=123456 \
> mysql:5.6
d53cc831ceea85498e8e3348e006bd07c6f9ef473337855b4b7313eb824ff294
[root@centos7 mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d53cc831ceea mysql:5.6 "docker-entrypoint.s…" 19 seconds ago Up 17 seconds 0.0.0.0:3307->3306/tcp c_mysql
[root@centos7 mysql]# docker exec -it c_mysql
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
[root@centos7 mysql]# docker exec -it c_mysql /bin/bash
root@d53cc831ceea:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
在虚拟机上部署好了过后,我们用自己的电脑上的navicat来连接一下虚拟机上的mysql。
如何查看虚拟机的ip地址?输入指令:ifconfig

连接成功后,就能通过navicat很方便地操作虚拟机上的数据库啦!
Tomcat
步骤
- 搜索tomcat镜像
- 拉取tomcat镜像
- 创建容器,设置端口映射、目录映射(在/root目录下创建tomcat目录用于存储tomcat数据信息)
[root@centos7 tomcat]# docker run -id --name=c_tomcat \
> -p 8080:8080 \
> -v $PWD:/usr/local/tomcat/webapps \
> tomcat
- -p 8080:8080:将容器的8080端口映射到主机的8080端口
- -v $PWD:/usr/local/tomcat/webapps:将主机中当前目录挂载到容器的webapps
- 使用外部机器访问tomcat
[root@centos7 docker]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
6c33745f49b4: Pull complete
ef072fc32a84: Pull complete
c0afb8e68e0b: Pull complete
d599c07d28e6: Pull complete
e8a829023b97: Pull complete
d04be46a31d1: Pull complete
db6007c69c35: Pull complete
e4ad4c894bce: Pull complete
248895fda357: Pull complete
277059b4cba2: Pull complete
Digest: sha256:57dae7dfb9b62a413cde65334c8a18893795cac70afc3be589c8336d8244655d
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@centos7 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d53cc831ceea mysql:5.6 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3307->3306/tcp c_mysql
[root@centos7 docker]# mkdir ~/tomcat
[root@centos7 docker]# cd ~/tomcat
[root@centos7 tomcat]# docker run -id --name=c_tomcat \
> -p 8080:8080 \
> -v $PWD:/usr/local/tomcat/webapps \
> tomcat
27da1b0ca5b7d227bf3c5bc77770223f36a5dc3e7cca2d2d1226e6b57e978cf6
[root@centos7 tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27da1b0ca5b7 tomcat "catalina.sh run" 9 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp c_tomcat
d53cc831ceea mysql:5.6 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3307->3306/tcp c_mysql
在tomcat文件夹下,创建一个test文件夹,在test中创建index.html。(此时容器中的/usr/local/tomcat/webapps也有了test文件夹,里边有index.html)
[root@centos7 tomcat]# mkdir test
[root@centos7 tomcat]# cd test
[root@centos7 test]# vim index.html
然后通过浏览器访问:http://虚拟机ip地址:8080/test/index.html

Nginx
创建nginx文件夹,底下创建conf文件夹,该文件夹下放nginx.conf,该配置文件的内容如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
[root@centos7 ~]# mkdir nginx
[root@centos7 ~]# cd nginx
[root@centos7 nginx]# mkdir conf
[root@centos7 nginx]# cd conf
[root@centos7 conf]# vim nginx.conf
[root@centos7 conf]# cd ..
[root@centos7 nginx]# ll
total 0
drwxr-xr-x. 2 root root 24 Dec 22 13:47 conf
[root@centos7 nginx]# docker run -id --name=c_nginx \
> -p 80:80 \
> -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
> -v $PWD/logs:/var/log/nginx \
> -v $PWD/html:/usr/share/nginx/html \
> nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
6ec7b7d162b2: Already exists
cb420a90068e: Pull complete
2766c0bf2b07: Pull complete
e05167b6a99d: Pull complete
70ac9d795e79: Pull complete
Digest: sha256:4cf620a5c81390ee209398ecc18e5fb9dd0f5155cd82adcbae532fec94006fb9
Status: Downloaded newer image for nginx:latest
441d45f40c80c45100e32c49c3f7f732c087d48718774b4609e84ca44ee30d27
[root@centos7 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
441d45f40c80 nginx "/docker-entrypoint.…" 11 seconds ago Up 8 seconds 0.0.0.0:80->80/tcp c_nginx
27da1b0ca5b7 tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:8080->8080/tcp c_tomcat
d53cc831ceea mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:3307->3306/tcp c_mysql
此时浏览器访问:虚拟机ip:80,可以看到如下页面

在html文件夹下创建index.html。
[root@centos7 nginx]# ll
total 0
drwxr-xr-x. 2 root root 24 Dec 22 13:47 conf
drwxr-xr-x. 2 root root 6 Dec 22 13:53 html
drwxr-xr-x. 2 root root 41 Dec 22 13:53 logs
[root@centos7 nginx]# cd html
[root@centos7 html]# vim index.html
再次访问:虚拟机ip:80

Redis
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 0ebb5600241d 10 hours ago 302MB
tomcat latest feba8d001e3f 3 days ago 649MB
nginx latest ae2feff98a0c 6 days ago 133MB
redis 5.0 5a3c8e192943 10 days ago 98.4MB
centos 7 8652b9f0cb4c 5 weeks ago 204MB
[root@centos7 ~]# docker run -id --name=c_redis \
> -p 6379:6379 \
> redis:5.0
18226856a8405d8b2580f8077baa62b1265c86f3cd6f2997d71557b6afebde34
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18226856a840 redis:5.0 "docker-entrypoint.s…" 13 seconds ago Up 10 seconds 0.0.0.0:6379->6379/tcp c_redis
441d45f40c80 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 0.0.0.0:80->80/tcp c_nginx
27da1b0ca5b7 tomcat "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp c_tomcat
d53cc831ceea mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:3307->3306/tcp c_mysql
本文介绍在CentOS7上使用Docker部署MySQL、Tomcat、Nginx和Redis的过程。涵盖Docker的基本概念、端口映射及数据卷的使用,并详细记录了每个应用的部署步骤。

165

被折叠的 条评论
为什么被折叠?



