借鉴链接:https://blog.csdn.net/guigenyi/article/details/126692747
创建自定义 Docker 网络
docker network create nextcloud-network
创建mysql的账号密码都是root 并将其连接到自定义网络
docker run --name mysql-container --network nextcloud-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextcloud -p 3306:3306 -d mysql:5.7
24e22bc12512eb5d11d8720f880b674e189f4c7af0b0b96bcced0b41d2d9dcce
创建nextcloud 并将其连接到 nextcloud-network网络
docker run --name nextcloud-container-test --network nextcloud-network -p 8080:80 -d nextcloud
进入mysql
docker exec -it mysql-container bash
bash-4.2# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
创建数据库
create database nextcloud charset=utf8;
数据库名
1. 检查容器网络配置
首先,确认 Nextcloud
容器和 MySQL
容器是否在同一个 Docker 网络中。如果它们不在同一个网络中,Nextcloud
容器将无法解析 mysql-container
。
你可以通过以下命令检查容器的网络配置:
bash复制代码
docker network inspect bridge # 查看默认网络配置
docker inspect nextcloud-container-test | grep "Networks" # 查看 Nextcloud 容器的网络配置
docker inspect mysql-container | grep "Networks" # 查看 MySQL 容器的网络配置
2. 创建自定义 Docker 网络
为了确保这两个容器可以互相通信,你可以创建一个自定义的 Docker 网络,然后将这两个容器连接到这个网络。
-
创建自定义网络:
bash复制代码 docker network create nextcloud-network
-
重新启动 MySQL 容器并将其连接到自定义网络:
停止并删除原来的 MySQL 容器:
bash复制代码 docker stop mysql-container docker rm mysql-container
启动 MySQL 容器并将其连接到
nextcloud-network
网络:bash复制代码 docker run --name mysql-container --network nextcloud-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextcloud -p 3306:3306 -d mysql:5.7
-
重新启动 Nextcloud 容器并将其连接到相同的自定义网络:
停止并删除原来的 Nextcloud 容器:
bash复制代码 docker stop nextcloud-container-test docker rm nextcloud-container-test
启动 Nextcloud 容器并将其连接到
nextcloud-network
网络:bash复制代码 docker run --name nextcloud-container-test --network nextcloud-network -p 8080:80 -d nextcloud
3.整体过程
Last login: Sat Dec 7 11:31:25 on ttys000
(base) lfzxmw@lius-MacBook-Air ~ % ssh -p 22 root@
root@'s password:
Last failed login: Sun Dec 8 21:07:00 CST 2024 from on ssh:notty
There were 239 failed login attempts since the last successful login.
Last login: Sun Dec 8 20:45:15 2024 from
[root@VM-4-10-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45ccf987fbe7 mysql “docker-entrypoint.s…” 12 minutes ago Up 12 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-
9301d1c9a45b nextcloud “/entrypoint.sh apac…” 7 days ago Up 7 days 0.0.0.0:8080->80/tcp, :::8080->80/tcp nextcloud-container-test
a865c26dd399 a68a19f689c3 “/sbin/entrypoint.sh” 3 years ago Up 8 days 0.0.0.0:3128->3128/tcp, :::3128->3128/tcp squid
[root@VM-4-10-centos ~]# docker network inspect bridge
[
{
“Name”: “bridge”,
“Id”: “41c87195c930aa99b4d7631f45a52391bf7b8aa304baaed7d17572da02162344”,
“Created”: “2024-11-29T21:54:49.75255255+08:00”,
“Scope”: “local”,
“Driver”: “bridge”,
“EnableIPv6”: false,
“IPAM”: {
“Driver”: “default”,
“Options”: null,
“Config”: [
{
“Subnet”: “172.17.0.0/16”,
“Gateway”: “172.17.0.1”
}
]
},
“Internal”: false,
“Attachable”: false,
“Ingress”: false,
“ConfigFrom”: {
“Network”: “”
},
“ConfigOnly”: false,
“Containers”: {
“45ccf987fbe7a55efe35b3f2fe5ac9b3f29e75498b8ada0a5429495a81d53e2f”: {
“Name”: “mysql-”,
“EndpointID”: “8bc6e58055b37944fd723f7ef858f33e98be24081268b3fbfeb37559b52968c1”,
“MacAddress”: “02:42:ac:11:00:04”,
“IPv4Address”: “172.17.0.4/16”,
“IPv6Address”: “”
},
“9301d1c9a45b9d5fab7fd82c996eff9064fd74ce1ac40ef5cb393450167e67f1”: {
“Name”: “nextcloud-container-test”,
“EndpointID”: “9c5b61802e2d797f17f77cd75ea42c7bed30fc8e7a9701286cf4047818e6a22a”,
“MacAddress”: “02:42:ac:11:00:03”,
“IPv4Address”: “172.17.0.3/16”,
“IPv6Address”: “”
},
“a865c26dd399e95967abf7ae078c8e5a4a27df3c0d023e597ceef0123be55631”: {
“Name”: “squid”,
“EndpointID”: “1c3b6f4bf8bf6e20be6a39f7631717ca54344de5fb8e9bc09e04dcd6c22ae0ee”,
“MacAddress”: “02:42:ac:11:00:02”,
“IPv4Address”: “172.17.0.2/16”,
“IPv6Address”: “”
}
},
“Options”: {
“com.docker.network.bridge.default_bridge”: “true”,
“com.docker.network.bridge.enable_icc”: “true”,
“com.docker.network.bridge.enable_ip_masquerade”: “true”,
“com.docker.network.bridge.host_binding_ipv4”: “0.0.0.0”,
“com.docker.network.bridge.name”: “docker0”,
“com.docker.network.driver.mtu”: “1500”
},
“Labels”: {}
}
]
[root@VM-4-10-centos ~]# docker inspect nextcloud-container-test | grep “Networks”
“Networks”: {
[root@VM-4-10-centos ~]# docker inspect mysql-container | grep “Networks”
“Networks”: {
[root@VM-4-10-centos ~]# docker network create nextcloud-network
d10d829da489a9615c0f45e41efcb27e91daf9d4c41e12b473cbb8655019e988
[root@VM-4-10-centos ~]# docker stop mysql-container
mysql-container
[root@VM-4-10-centos ~]# docker rm mysql-container
mysql-container
[root@VM-4-10-centos ~]# docker run --name mysql-container --network nextcloud-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextcloud -p 3306:3306 -d mysql:5.7
be8cda11dcf687089e4b508309edb28e9fe620470c7d9518963221350659ef08
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql-container (ec397d8234d802f3931bc48424ce9ca9237ae503df02a39995e28220c2c97981): Bind for 0.0.0.0:3306 failed: port is already allocated.
[root@VM-4-10-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45ccf987fbe7 mysql “docker-entrypoint.s…” 12 minutes ago Up 12 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-
9301d1c9a45b nextcloud “/entrypoint.sh apac…” 7 days ago Up 7 days 0.0.0.0:8080->80/tcp, :::8080->80/tcp nextcloud-container-test
a865c26dd399 a68a19f689c3 “/sbin/entrypoint.sh” 3 years ago Up 8 days 0.0.0.0:3128->3128/tcp, :::3128->3128/tcp squid
[root@VM-4-10-centos ~]# docker stop 45ccf987fbe7
45ccf987fbe7
[root@VM-4-10-centos ~]# docker rm 45ccf987fbe7
45ccf987fbe7
[root@VM-4-10-centos ~]# docker run --name mysql-container --network nextcloud-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextcloud -p 3306:3306 -d mysql:5.7
docker: Error response from daemon: Conflict. The container name “/mysql-container” is already in use by container “be8cda11dcf687089e4b508309edb28e9fe620470c7d9518963221350659ef08”. You have to remove (or rename) that container to be able to reuse that name.
See ‘docker run --help’.
[root@VM-4-10-centos ~]# docker rm mysql-container
mysql-container
[root@VM-4-10-centos ~]# docker run --name mysql-container --network nextcloud-network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=nextcloud -p 3306:3306 -d mysql:5.7
24e22bc12512eb5d11d8720f880b674e189f4c7af0b0b96bcced0b41d2d9dcce
[root@VM-4-10-centos ~]# docker stop nextcloud-container-test
nextcloud-container-test
[root@VM-4-10-centos ~]# docker rm nextcloud-container-test
nextcloud-container-test
[root@VM-4-10-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24e22bc12512 mysql:5.7 “docker-entrypoint.s…” 13 seconds ago Up 12 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-container
a865c26dd399 a68a19f689c3 “/sbin/entrypoint.sh” 3 years ago Up 8 days 0.0.0.0:3128->3128/tcp, :::3128->3128/tcp squid
[root@VM-4-10-centos ~]# docker run --name nextcloud-container-test --network nextcloud-network -p 8080:80 -d nextcloud
3f549a6ee359493bcb22e7b62322564eb9e2cc9f9a213e2ff4cbd5caecdef3a5
[root@VM-4-10-centos ~]# docker exec -it nextcloud-container-test bash
root@3f549a6ee359:/var/www/html# cat /var/www/html/config/config.php
cat: /var/www/html/config/config.php: No such file or directory
root@3f549a6ee359:/var/www/html# cat /var/www/html/config/config.php
cat: /var/www/html/config/config.php: No such file or directory
root@3f549a6ee359:/var/www/html# ls
3rdparty composer.json index.php public.php
AUTHORS composer.lock nextcloud-init-sync.lock remote.php
COPYING console.php occ robots.txt
LICENSES cron.php package-lock.json status.php
apps index.html package.json
root@3f549a6ee359:/var/www/html# exitl
bash: exitl: command not found
root@3f549a6ee359:/var/www/html# exit;
exit
[root@VM-4-10-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f549a6ee359 nextcloud “/entrypoint.sh apac…” About a minute ago Up About a minute 0.0.0.0:8080->80/tcp, :::8080->80/tcp nextcloud-container-test
24e22bc12512 mysql:5.7 “docker-entrypoint.s…” About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql-container
a865c26dd399 a68a19f689c3 “/sbin/entrypoint.sh” 3 years ago Up 8 days 0.0.0.0:3128->3128/tcp, :::3128->3128/tcp squid
[root@VM-4-10-centos ~]# docker exec -it mysql-container bash
bash-4.2# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
bash-4.2# 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.44 MySQL Community Server (GPL)
Copyright © 2000, 2023, Oracle and/or its affiliates.
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> sudo docker update --restart=always mysql
->
-> create database nextcloud charset=utf8;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sudo docker update --restart=always mysql
create database nextcloud charset=utf’ at line 1
mysql> create database nextcloud charset=utf8;
ERROR 1007 (HY000): Can’t create database ‘nextcloud’; database exists
mysql>