[英]Accessing Docker (MySQL) container without docker-compose?
I'm new to Docker and might be confusing things but let's say I have multiple projects and I want to use the same docker MySQL database with them. 我是Docker的新手,可能会感到困惑,但可以说我有多个项目,并且想将同一个Docker MySQL数据库与它们一起使用。
Some of these projects also run Docker, some do not, the ones running Docker have docker-compose.yml in repos so I cannot change this file, for the others I don't want to use Docker at all (excepting the base container). 其中一些项目也运行Docker,有些则不运行,那些运行Docker的项目在存储库中包含docker-compose.yml,所以我无法更改此文件,因为其他项目我根本不想使用Docker(基本容器除外) 。
I pulled https://hub.docker.com/_/mysql/ already then run it like this: 我已经拉https://hub.docker.com/_/mysql/然后像这样运行它:
docker run --name mysql-experiment -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.21
When I run docker ps
I can see mysql-experiment
is running. 当我运行
docker ps
我可以看到mysql-experiment
正在运行。
I'm able to login to the container using docker exec -it mysql-experiment bash
. 我可以使用
docker exec -it mysql-experiment bash
登录到容器。 But I'm not good enough with MySQL to manage it through Terminal. 但是我对MySQL不够好,无法通过Terminal对其进行管理。
My question is - how could I setup this container so it gets its own port, and IP and I can use it anywhere within my local network, without docker-compose? 我的问题是-如何设置此容器,使其具有自己的端口和IP,并且可以在本地网络中的任何位置使用它,而无需docker-compose?
Right now it runs on localhost on port 3306
(according to docker ps
), but when I try to access 127.0.0.1:3306 in Sequel Pro I'm getting: 现在,它在端口
3306
上的localhost上运行(根据docker ps
),但是当我尝试在Sequel Pro中访问127.0.0.1:3306时,我得到:
Unable to connect to host 127.0.0.1, or the request timed out.
Also, I'm not sure what username I should use with this container (I've tried admin/root/empty) 另外,我不确定应该在此容器中使用什么用户名(我已经尝试过admin / root / empty)
[edit] [编辑]
I know there are many questions like this one, but all of them involve docker-compose. 我知道有很多这样的问题,但是它们都涉及docker-compose。
At the moment, the mysql process runs on port 3306
in the container itself. 目前,mysql进程在容器本身的端口
3306
上运行。 You can verify that the process is running by ssh-ing into the container using docker exec
, like what you did, and do the following: 您可以像使用操作一样,通过使用
docker exec
到容器中来验证该进程是否正在运行,并执行以下操作:
mysql -u root -p root -h localhost
The localhost here belongs to the mysql container. 这里的本地主机属于mysql容器。 In order to access your mysql process through your local environment (which I assume is the docker host in this case), you need to publish the port.
为了通过您的本地环境(在这种情况下,我假设是docker主机)访问mysql进程,您需要发布端口。 This can be done using the
-p
flag in docker run
: 这可以在
docker run
使用-p
标志来完成:
docker run --name mysql-experiment -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.21
Here, we are binding the host's 3306 port to the container's 3306 port. 在这里,我们将主机的3306端口绑定到容器的3306端口。 If you want to access mysql using a different port on your localhost, say 3307, you can do
-p 3307:3306
. 如果要使用本地主机上的其他端口(例如3307)访问mysql,则可以执行
-p 3307:3306
。
Ref: https://docs.docker.com/engine/reference/run/#expose-incoming-ports 参考: https : //docs.docker.com/engine/reference/run/#expose-incoming-ports
I would suggest you always use clean mysql db in all your docker-compose services. 我建议您在所有docker-compose服务中始终使用干净的mysql db。 Your services will be more independent and it is easy to manage access in one docker-compose.yml
您的服务将更加独立,并且可以在一个docker-compose.yml中轻松管理访问
But if you want to use just one mysql i would recommend 但是,如果您只想使用一个mysql,我建议
create volume to store data, you should never write to container filesystem in longterm 创建用于存储数据的卷 ,您永远不应长期写入容器文件系统
docker volume create my_mysql_data
run mysql and map the port to host with -p
运行mysql并使用
-p
将端口映射到主机
docker run -d -p 3306:3306 --restart always \ # port and restart policy
-v my_mysql_data:/var/lib/mysql \ # use the volume created before
--name mysql-experiment -e MYSQL_ROOT_PASSWORD=root \
mysql:5.7.21
access mysql from other docker container using host IP or use this --link
option allows this "my_web to access mysql container on address mysql
使用主机IP从其他
--link
容器访问mysql或使用此--link
选项允许此“ my_web访问地址mysql
上的mysql容器
docker run -d --name my_web \
--link mysql-experiment:mysql \
debian
if you are on mac and in docker-compose contianer you can get the IP of the host (your mac) with docker.for.mac.host.internal
as it is in this answer 如果您在Mac上并且在docker-compose
docker.for.mac.host.internal
,则可以使用docker.for.mac.host.internal
获取主机(您的Mac)的IP,因为在此答案中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.