当前位置: 首页 > 面试题库 >

Docker,无法使用docker标志-p 3000:3000从localhost:3000进行“ rails服务器”开发

赵河
2023-03-14
问题内容

我正在尝试将docker与rails结合使用,在一个容器中构建整个堆栈。我的最终目标是拥有一个带有runit的nginx / memcached /
unicorn / rails /
postgres堆栈作为流程管理器。到目前为止,我已经安装了我的应用程序,ruby和postgres。在这一点上,我尝试在进入nginx /
memcached / unicorn之前测试一切是否正常。
这是我的dockerfile:

FROM ubuntu:trusty

ADD . /app

# Update repos
RUN apt-get update

# General 
RUN apt-get install -y git curl software-properties-common python-software-properties ssh

# Install ruby (taken from https://gist.github.com/konklone/6662393)
RUN \curl -Lk https://get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm requirements"
RUN /bin/bash -l -c "rvm install 2.1.1"
ENV PATH /usr/local/rvm/gems/ruby-2.1.1/bin:/usr/local/rvm/rubies/ruby-2.1.1/bin:$PATH
RUN gem install bundler --no-ri --no-rdoc

# Install nodejs (rails js runtime)
RUN apt-get install -y nodejs

# Install postgresql (adapted from https://wiki.postgresql.org/wiki/Apt)
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main"
RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get update
RUN apt-get install -y postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

# Setup postgresql
RUN mkdir /pgsql && chown postgres /pgsql
USER postgres
ENV PATH $PATH:/usr/lib/postgresql/9.3/bin/
RUN initdb -E UTF8 -D /pgsql/data
USER root

# Needed for pg gem (from http://stackoverflow.com/a/20754173)
RUN apt-get install -y libpq-dev

然后我用以下命令创建一个容器

docker run -it -p 3000:3000 *dockerfile_image* /bin/bash -l

在该容器内,我将postgresql设置为后台作业并运行rails

$ sudo -u postgres /usr/lib/postgresql/9.3/bin/postgres -D /pgsql/data
^z
$ bg
$ sudo -u postgres /usr/bin/psql -c "create role *appname* login createdb"
$ cd /app
$ bundle && rake db:create db:migrate
$ rails s
=> Booting WEBrick
=> Rails 4.1.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
....

但正如标题所述,我无法从localhost:3000连接。我也尝试过

  • 尝试从0.0.0.0:3000连接
  • 离开-p 3000:3000并尝试从 container_ip :3000 连接
  • dockerfile中的EXPOSE 3000,使用docker ps查找映射的端口并尝试localhost: mapd_port

另外我不知道这是否重要,但是我在OSX上使用boot2docker。


问题答案:

这实际上是由boot2docker引起的。请参阅此页面以获取更多信息:https
:
//github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

TLDR

Portforwarding当前仅适用于boot2docker上的hack。您需要运行以下命令(并使其保持运行):

boot2docker ssh -L 8080:localhost:80

其中80是容器上的裸露端口,而8080是您要从主机上访问它的端口。



 类似资料:
  • 我正在使用CreateReact应用程序编写SPA,并使用expressjs服务器作为后端。对于开发,我在端口3000上运行前端测试服务器,在3003上运行后端expressjs测试服务器。 我使用Axios在客户端进行了许多API调用,因此为了便于开发,我定义了代理:http://localhost:3003“在我的包裹里。json 这在我的笔记本电脑上运行正常,但在我的工作站上运行时,我在访问

  • 以下docker-compose.yml文件位于两个不同的文件夹中: 运行docker-compose logs-f nginx后出错 以下是front/docker-compose.yml: API/Docker-compose.yml 运行 收到。/usr/src/nuxt-app/ 运行

  • 后端:Express服务器,带npx create-Express-api后端 Frontend: Next.js,带npx create-react-app前端 我已经在我的根文件夹中实现了这些命令,并尝试运行npm start xxx来检查它们是否还在工作。但是它们在我的http://localhost:3000中不起作用,尽管它们在此链接中起作用http://172.27.178.192:3

  • 问题内容: 我想使用Docker部署我的rails项目。因此,我使用Docker-Compose。但是我收到一个奇怪的错误消息。当运行docker-compose up(包含db-container和postgresql,redis和带有rails的Web容器)时,我得到一个 rails server -h 所以我不明白为什么会收到此消息,因为每次运行docker- compose时,都会启动新容

  • 我已经使用docker工具箱在视窗机器上安装了docker。我也在我的视窗机器上安装了mysql,服务器运行在端口3306上(localhost-127.0.0.1-在docker机器之外)。我在一个名为“micra-workq-svc”的docker网络内部的docker容器中运行一个冲刺启动应用程序,我希望在docker内部运行的应用程序连接到本地主机。我花了几个小时在谷歌上搜索,没有一个链接

  • 使用Spring Boot(后端)和React(前端)开发时,我有一个奇怪的行为。React和Spring boot都在本地主机8080上运行。 当我从React to Spring Boot向localhost:8080发送POST请求时,我在Spring Boot中遇到了以下错误: 当我从localhost:3000(React development build)向localhost:808