入职必会-开发环境搭建48-Docker必会软件安装

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 Tair(兼容Redis),内存型 2GB
简介: 本文介绍了Docker安装MySQL,Docker安装Tomcat,Docker安装Nginx,Docker安装Redis

Docker安装MySQL

操作步骤

  1. 拉取镜像

命令说明:

# 拉取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]#


  1. 创建容器

命令说明:

# 使用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]#


  1. 操作容器中的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>


  1. 远程登录容器中的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

操作步骤

  1. 拉取镜像

命令说明:

# 拉取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]#


  1. 创建容器

命令说明:

# 创建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


  1. 宿主机访问Tomcat
    在宿主机浏览器中访问:http://192.168.100.133:8080/testweb

Docker安装Nginx

操作步骤

  1. 拉取镜像

命令说明:

# 拉取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


  1. 创建容器

命令说明:

# 创建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


  1. 宿主机访问Nginx
    在宿主机浏览器中访问:http://192.168.100.133:80/
  2. 配置反向代理
    上面配置的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

操作步骤

  1. 拉取镜像

命令说明:

# 拉取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 ~]#


  1. 创建容器

命令说明:

# 创建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 ~]#


  1. 操作容器中的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>
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
C++ Docker Python
M1 Pro 利用docker 搭建pytho2的开发环境,以vscode连接开发为例
M1 Pro 利用docker 搭建pytho2的开发环境,以vscode连接开发为例
243 0
|
3月前
|
Java Shell Linux
入职必会-开发环境搭建49-Docker必会构建镜像
前面一直都是从Docker仓库中下载镜像然后使用。我们项目中可以通过Dockerfile构建自己的镜像。 Dockerfile是由一系列命令和参数构成的文本文件,Docker可以读取Dockerfile文件并根据Dockerfile文件的描述来构建镜像。Dockerfile文件内容一般分为4部分 ● 基础镜像信息 ● 维护者信息 ● 镜像操作指令 ● 容器启动时执行的指令
|
8天前
|
存储 Ubuntu JavaScript
如何使用Docker优化你的开发环境配置
如何使用Docker优化你的开发环境配置
|
9天前
|
Docker 容器
利用Docker Compose优化开发环境的配置
在现代软件开发中,环境一致性至关重要。开发人员常需在不同机器间复制环境配置,而Docker Compose提供了一种简便有效的方法来定义和运行多容器Docker应用程序,确保开发、测试和生产环境一致,简化团队协作,提高开发效率。通过YAML文件配置服务、网络和卷,使用简单命令即可启动和停止服务。本文将介绍Docker Compose的核心优势、基本使用方法及高级功能,帮助你更好地管理和优化开发环境。
|
3月前
|
存储 Docker 容器
入职必会-开发环境搭建50-Docker必会搭建Docker私有仓库
Docker官方的Docker hub(https://hub.docker.com)是一个用于管理公共镜像的仓库,我们可以从上面拉取镜像到本地也可以把我们自己的镜像推送上去。但是有时候我们的服务器无法访问互联网或者不希望将自己的镜像放到公网当中,那么我们就需要搭建自己的Docker私有仓库来存储和管理自己的Docker镜像。
入职必会-开发环境搭建50-Docker必会搭建Docker私有仓库
|
3月前
|
存储 Linux 应用服务中间件
入职必会-开发环境搭建47-Docker必会命令
本文介绍了Docker使用频率最高的30个命令
入职必会-开发环境搭建47-Docker必会命令
|
2月前
|
安全 开发者 Docker
使用Docker进行本地开发环境设置:高效、可重复与隔离的利器
【8月更文挑战第8天】使用Docker进行本地开发环境设置不仅提高了开发效率,还保证了开发环境的一致性和可重复性。通过简单的Dockerfile和Docker命令,开发者可以轻松地创建、运行和管理自己的开发环境。随着Docker的普及和生态的不断发展,相信它将在未来的软件开发中发挥更加重要的作用。
|
3月前
|
安全 Linux 持续交付
入职必会-开发环境搭建46-Docker下载和安装
Docker是一种开源的容器化平台,用于构建、部署和运行应用程序。它通过容器的方式将应用程序及其相关依赖项打包在一起,形成一个独立、可移植的运行环境。 以下是Docker的主要特点和优势。
|
jenkins 持续交付 开发工具
如何基于Docker搭建jenkins集成开发环境?建议收藏
如何基于Docker搭建jenkins集成开发环境?建议收藏
129 1
|
Python Go 编译器