Docker安装MySQL
操作步骤
- 拉取镜像
命令说明:
# 拉取MySQL5.7镜像 docker pull centos/mysql-57-centos7
操作示例:
[root@cxypa hello]# docker pull centos/mysql-57-centos7 Using default tag: latest latest: Pulling from centos/mysql-57-centos7 d8d02d457314: Pull complete a11069b6e245: Pull complete 596303fb1aa3: Pull complete a29499e779a7: Pull complete 17d1a52c2e00: Pull complete ed24591227fe: Pull complete de0ad46e3ed9: Pull complete c62e4a984a9c: Pull complete 01d54c6bda68: Pull complete Digest: sha256:e08ee4d43b7356607685b69bde6335e27cf20c020f345b6c6c59400183882764 Status: Downloaded newer image for centos/mysql-57-centos7:latest docker.io/centos/mysql-57-centos7:latest [root@cxypa hello]#
- 创建容器
命令说明:
# 使用centos/mysql-57-centos7镜像创建cmysql5.7容器 docker run -di --name cmysql5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7 # 参数说明 -p 表示端口映射,格式为 宿主机端口:容器端口 -e 表示添加环境变量 MYSQL_ROOT_PASSWORD 是root用户的远程登陆密码(如果是在容器中使用root登录的话,那么其密码为空)
操作示例:
[root@cxypa hello]# docker run -di --name cmysql5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root centos/mysql-57-centos7 f6441e025239dce4604ca493bdcca4eb2db23c8e3729e70b1b9217a1af7d05bf [root@cxypa hello]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6441e025239 centos/mysql-57-centos7 "container-entrypoin…" 24 seconds ago Up 22 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp cmysql5.7 [root@cxypa hello]#
- 操作容器中的MySQL
命令说明:
# 进入cmysql5.7容器 docker exec -it cmysql5.7 /bin/bash # 登录容器里面的MySQL mysql -u root -p
操作示例:
[root@cxypa hello]# docker exec -it cmysql5.7 /bin/bash bash-4.2$ bash-4.2$ pwd /opt/app-root/src bash-4.2$ mysql -u root -p Enter password: # 默认的root账号是没有密码的,只需要再次按回车即可 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.24 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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 | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
- 远程登录容器中的MySQL
命令说明:
# 查看容器的IP,如果其他容器中要连接cmysql5.7容器的MySQL,可以使用查找到的IP docker inspect cmysql5.7 | grep -i ipaddr
操作示例:
[root@cxypa hello]# docker inspect cmysql5.7 | grep -i ipaddr "SecondaryIPAddresses": null, "IPAddress": "172.17.0.4", "IPAddress": "172.17.0.4", [root@cxypa hello]# # 其他容器中要连接cmysql5.7容器的MySQL,就使用172.17.0.4这个IP
Windows要远程连接cmysql5.7容器的MySQL
需要在Windows中访问宿主机的IP加容器映射的端口。效果如下:
Docker安装Tomcat
操作步骤
- 拉取镜像
命令说明:
# 拉取Tomcat最新镜像 docker pull tomcat
操作示例:
[root@cxypa hello]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 6e3729cf69e0: Pull complete 4d8d923227d8: Pull complete eda8241fd25f: Pull complete 35dccabde73d: Pull complete 978c906bcdda: Pull complete 45999e75f51e: Pull complete ecd30916ffc4: Pull complete Digest: sha256:dd920d167352e9e21b297dbe08c54ca35c445c100f38bf9be9a8fa85c9196e7f Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest [root@cxypa hello]#
- 创建容器
命令说明:
# 创建ctomcat容器,并挂载宿主机的/tmp/tomcat/webapps目录到容器的/usr/local/tomcat/webapps docker run -di --name ctomcat -p 8080:8080 -v /tmp/tomcat/webapps:/usr/local/tomcat/webapps tomcat # 查看Tomcat运行日志 docker logs -f ctomcat # 如果出现 WARNING: IPv4 forwarding is disabled. Networking will not work. # 执行如下操作 # 1.编辑 sysctl.conf vi /etc/sysctl.conf # 2.在上述打开的文件中后面添加 net.ipv4.ip_forward=1 # 3.重启network systemctl restart network
操作示例:
[root@cxypa ~]# mkdir /tmp/tomcat/webapps/testweb # 创建Tomcat项目部署目录 [root@cxypa webapps]# [root@cxypa webapps]# cd /tmp/tomcat/webapps/testweb [root@cxypa testweb]# echo "<h1>Hello Docker Tomcat</h1>" >> index.html # 在testweb目录中创建index.html [root@cxypa testweb]# [root@cxypa ~]# docker run -di --name ctomcat -p 8080:8080 -v /tmp/tomcat/webapps:/usr/local/tomcat/webapps tomcat 创建ctomcat容器 efbfc66a5c7a839a7557305730c4939c8c00f31384dea7a6c67851719cd33d82 [root@cxypa ~]# [root@cxypa ~]# [root@cxypa ~]# docker logs -f ctomcat # 查看Tomcat运行日志 03-Jan-2023 07:19:36.294 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/10.1.4 # 省略其他 03-Jan-2023 07:19:36.797 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 03-Jan-2023 07:19:36.816 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [82] milliseconds
- 宿主机访问Tomcat
在宿主机浏览器中访问:http://192.168.100.133:8080/testweb
Docker安装Nginx
操作步骤
- 拉取镜像
命令说明:
# 拉取Nginx镜像 docker pull nginx
操作示例:
[root@cxypa ~]# docker pull nginx # 拉取Nginx镜像 Using default tag: latest latest: Pulling from library/nginx 3f4ca61aafcd: Pull complete 50c68654b16f: Pull complete 3ed295c083ec: Pull complete 40b838968eea: Pull complete 88d3ab68332d: Pull complete 5f63362a3fa3: Pull complete Digest: sha256:0047b729188a15da49380d9506d65959cce6d40291ccfb4e039f5dc7efd33286 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
- 创建容器
命令说明:
# 创建Nginx容器 docker run -di --name cnginx -p 80:80 nginx # 查看日志 docker logs -f cnginx # log 日志文件目录:/var/log/nginx # config 配置文件目录: /etc/nginx # web 资源存放目录: /usr/share/nginx/html
操作示例:
[root@cxypa ~]# docker run -di --name cnginx -p 80:80 nginx # 创建Nginx容器 9de2fb8d33e4d0d3db5fde0c76dbe2980e4fe46d9ac4982f44f0d4acaa46f31e [root@cxypa ~]# docker logs -f cnginx # 查看日志 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2023/01/03 07:31:57 [notice] 1#1: using the "epoll" event method 2023/01/03 07:31:57 [notice] 1#1: nginx/1.23.3 2023/01/03 07:31:57 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2023/01/03 07:31:57 [notice] 1#1: OS: Linux 3.10.0-957.el7.x86_64 2023/01/03 07:31:57 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536 2023/01/03 07:31:57 [notice] 1#1: start worker processes 2023/01/03 07:31:57 [notice] 1#1: start worker process 29 2023/01/03 07:31:57 [notice] 1#1: start worker process 30 2023/01/03 07:31:57 [notice] 1#1: start worker process 31 2023/01/03 07:31:57 [notice] 1#1: start worker process 32
- 宿主机访问Nginx
在宿主机浏览器中访问:http://192.168.100.133:80/
- 配置反向代理
上面配置的Tomcat访问端口是8080。使用Nginx给Tomcat配置反向代理。Nginx镜像的配置文件在/etc/nginx/nginx.conf
。在容器中编辑配置文件比较麻烦,先将配置文件从容器中拷贝到宿主机,在宿主机进行编辑修改后再拷贝回去容器中。
命令说明:
# 从cnginx容器拷贝配置文件到宿主机当前目录 docker cp cnginx:/etc/nginx/nginx.conf nginx.conf # 查看ctomcat容器的IP地址 docker inspect ctomcat # 配置nginx.conf server { listen 80; server_name 127.0.0.1; location / { proxy_pass http://ctomcat容器的IP地址:容器的端口; } } # 将修改后的配置文件拷贝到容器中 docker cp nginx.conf cnginx:/etc/nginx/nginx.conf # 重启cnginx容器 docker restart cnginx
操作示例:
[root@cxypa ~]# docker cp cnginx:/etc/nginx/nginx.conf nginx.conf # 从cnginx容器拷贝配置文件到宿主机当前目录 [root@cxypa ~]# ll 总用量 36 -rw-------. 1 root root 1261 7月 7 21:48 anaconda-ks.cfg -rw-r--r--. 1 root root 648 12月 14 01:26 nginx.conf [root@cxypa ~]# [root@cxypa ~]# docker inspect ctomcat 查看ctomcat容器的IP地址 # 省略其他 "SandboxKey": "/var/run/docker/netns/432c8774a16b", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "e85baa96bca412d1286c5f5732d795c4546caa5ddcd87a34b1fa9d2da0f95831", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", # ctomcat容器的IP地址 "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", # 省略其他 [root@cxypa ~]# [root@cxypa ~]# vim nginx.conf # 进入nginx.conf文件 http { include /etc/nginx/mime.types; default_type application/octet-stream; # 省略其他 # 这行必须要注释掉 # include /etc/nginx/conf.d/*.conf; # 添加反向代理到容器中的Tomcat, 172.17.0.2是ctomcat容器的ip, 8080是ctomcat容器的端口 server { listen 80; server_name 127.0.0.1; location / { proxy_pass http://172.17.0.2:8080; } } } [root@cxypa ~]# [root@cxypa ~]# docker cp nginx.conf cnginx:/etc/nginx/nginx.conf # 将修改后的配置文件拷贝到容器中 [root@cxypa ~]# docker restart cnginx # 重启cnginx容器
浏览器访问Nginx,Nginx反向代理到容器中的Tomcat。http://192.168.100.133/testweb/
Docker创建Nginx容器注意事项
使用Docker创建Nginx容器时不能挂载目录, 否则Nginx容器不会自动创建Nginx相关的配置文件。
可以参考这篇文章:https://blog.csdn.net/weixin_46244732/article/details/114315708
Docker安装Redis
操作步骤
- 拉取镜像
命令说明:
# 拉取Redis镜像 docker pull redis
操作示例:
[root@cxypa ~]# docker pull redis # 拉取Redis镜像 Using default tag: latest latest: Pulling from library/redis 3f4ca61aafcd: Already exists c3775af77098: Pull complete fa7c5c7e501c: Pull complete 4059b4d2a3ce: Pull complete 6bc523bfb16a: Pull complete 20bf15ad3c24: Pull complete Digest: sha256:8184cfe57f205ab34c62bd0e9552dffeb885d2a7f82ce4295c0df344cb6f0007 Status: Downloaded newer image for redis:latest docker.io/library/redis:latest [root@cxypa ~]# [root@cxypa ~]# [root@cxypa ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 0256c63af7db 12 days ago 117MB [root@cxypa ~]#
- 创建容器
命令说明:
# 创建Redis容器 docker run -di --name credis -p 6379:6379 redis # 查看Redis日志 docker logs -f credis
操作示例:
[root@cxypa ~]# docker run -di --name credis -p 6379:6379 redis # 创建Redis容器 73d6510c805c94e1b5278e1c6d626cefd674b3b140170df2a9f122524f825d0d [root@cxypa ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73d6510c805c redis "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp credis 91db07de1cdf nginx "/docker-entrypoint.…" 2 hours ago Up 11 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp cnginx efbfc66a5c7a tomcat "catalina.sh run" 2 hours ago Up 40 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp ctomcat [root@cxypa ~]# docker logs -f credis # 查看Redis日志 1:C 03 Jan 2023 09:45:26.468 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 03 Jan 2023 09:45:26.468 # Redis version=7.0.7, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 03 Jan 2023 09:45:26.468 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 03 Jan 2023 09:45:26.469 * monotonic clock: POSIX clock_gettime 1:M 03 Jan 2023 09:45:26.471 * Running mode=standalone, port=6379. 1:M 03 Jan 2023 09:45:26.471 # WARNING: The TCP backlog setting of 511 cannot be enforced because [root@cxypa ~]#
- 操作容器中的Redis
[root@cxypa ~]# docker exec -it credis /bin/bash # 进入credis容器 root@73d6510c805c:/data# redis-cli # 登录Redis 127.0.0.1:6379> set name cxypa # 测试Redis的使用 OK 127.0.0.1:6379> get name "cxypa" 127.0.0.1:6379>