user6216601
user6216601

Reputation:

Symfony app in Docker doesn't respond in URL call

[SOLVED]

I want to add my code of Symfony application with MongoDB in a Docker image.

After I build the image of application and I recived:

PS E:\myapi> docker-compose up
Starting mongo
Starting myapi_web_server_1
Attaching to mongo, myapi_web_server_1
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=37e6234dbaf5
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] db version v3.0.14
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] git version: 08352afcca24bfc145240a0fac9d28b978ab77f3
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] build info: Linux ip-10-30-223-232 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo         | 2017-04-21T13:36:23.464+0000 I CONTROL  [initandlisten] options: { storage: { mmapv1: { smallFiles: true } } }
mongo         | 2017-04-21T13:36:23.476+0000 I JOURNAL  [initandlisten] journal dir=/data/db/journal
mongo         | 2017-04-21T13:36:23.476+0000 I JOURNAL  [initandlisten] recover : no journal files present, no recovery needed
mongo         | 2017-04-21T13:36:23.742+0000 I JOURNAL  [durability] Durability thread started
mongo         | 2017-04-21T13:36:23.742+0000 I JOURNAL  [journal writer] Journal writer thread started
mongo         | 2017-04-21T13:36:23.871+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
web_server_1  | 9-1ubuntu4.21 Development Server started at Fri Apr 21 13:36:24 2017
web_server_1  | Listening on http://0.0.0.0:8000
web_server_1  | Document root is /var/www
web_server_1  | Press Ctrl-C to quit.

But when I want to access http://172.17.0.3:8000/my_api/, where 172.17.0.3 is container's IP, I recive the message in Postman:

enter image description here

docker-compose.yml file

web_server:
    build: web_server/
    ports:
        - "8000:8000"
    links:
        - mongo
    tty: true
    environment:
        SYMFONY__MONGO_ADDRESS: mongo
        SYMFONY__MONGO_PORT: 27017

mongo:
    image: mongo:3.0
    container_name: mongo
    command: mongod --smallfiles
    expose:
        - 27017

Result for command docker-compose ps

PS E:\myapi> docker-compose ps
              Name                            Command               State           Ports
--------------------------------------------------------------------------------------------------
myapi_web_server_1   /bin/bash /entrypoint.sh         Up      0.0.0.0:8000->8000/tcp
mongo                docker-entrypoint.sh mongo ...   Up      27017/tcp

And result for command docker inspect myapi_web_server_1

"NetworkSettings": {
    "Bridge": "",
    "SandboxID": "774a7dcbdbfbf7e437ddff340aedd4ce951dffa7a80deab9afb6e6a8abc70bde",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {
        "8000/tcp": [
            {
                "HostIp": "0.0.0.0",
                "HostPort": "8000"
            }
        ]
    },
    "SandboxKey": "/var/run/docker/netns/774a7dcbdbfb",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "4c96f6e6f8a2c80dd7ea7469dd9d74760be1af81a8039a4f835145b8f1ef5fb5",
    "Gateway": "172.17.0.1",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "172.17.0.3",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "MacAddress": "02:42:ac:11:00:03",
    "Networks": {
        "bridge": {
            "IPAMConfig": null,
            "Links": null,
            "Aliases": null,
            "NetworkID": "e174576418903bf0809edd47b77d52e2fc7644d5aacafa15ec6a8f2d15458b8a",
            "EndpointID": "4c96f6e6f8a2c80dd7ea7469dd9d74760be1af81a8039a4f835145b8f1ef5fb5",
            "Gateway": "172.17.0.1",
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "MacAddress": "02:42:ac:11:00:03"
        }
    }
}

When I try to call from http://127.0.0.1:8000/my_api I recive in Postman

enter image description here

And in console:

web_server_1  | [Fri Apr 21 13:51:08 2017] 172.17.0.1:33382 [404]: /my_api - No such file or directory

Dockerfile content is

FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get install -y \
    git \
    curl \
    php5-cli \
    php5-json \
    php5-intl

RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

ADD entrypoint.sh /entrypoint.sh
ADD ./code /var/www

WORKDIR /var/www

#RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "/bin/bash", "/entrypoint.sh" ]

List routes

PS E:\myapi\web_server\code>  php bin/console debug:router
 ----------------------------------- -------- -------- ------ -----------------------------------
  Name                                Method   Scheme   Host   Path
 ----------------------------------- -------- -------- ------ -----------------------------------
  _wdt                                ANY      ANY      ANY    /_wdt/{token}
  _profiler_home                      ANY      ANY      ANY    /_profiler/
  _profiler_search                    ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar                ANY      ANY      ANY    /_profiler/search_bar
  _profiler_info                      ANY      ANY      ANY    /_profiler/info/{about}
  _profiler_phpinfo                   ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results            ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file                 ANY      ANY      ANY    /_profiler/open
  _profiler                           ANY      ANY      ANY    /_profiler/{token}
  _profiler_router                    ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception                 ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css             ANY      ANY      ANY    /_profiler/{token}/exception.css
  _twig_error_test                    ANY      ANY      ANY    /_error/{code}.{_format}
  db_transaction_postaddtransaction   POST     ANY      ANY    /my_api
  db_transaction_gettransactions      GET      ANY      ANY    /my_api/
  db_transaction_getbalance           GET      ANY      ANY    /balance/
  homepage                            ANY      ANY      ANY    /
 ----------------------------------- -------- -------- ------ -----------------------------------

Result using: list routes

{  
   "web_profiler.controller.profiler":[  
      "_wdt",
      "_profiler_home",
      "_profiler_search",
      "_profiler_search_bar",
      "_profiler_info",
      "_profiler_phpinfo",
      "_profiler_search_results",
      "_profiler_open_file",
      "_profiler"
   ],
   "web_profiler.controller.router":[  
      "_profiler_router"
   ],
   "web_profiler.controller.exception":[  
      "_profiler_exception",
      "_profiler_exception_css"
   ],
   "twig.controller.preview_error":[  
      "_twig_error_test"
   ],
   "DBBundle\\Controller\\TransactionController":[  
      "db_transaction_postaddtransaction",
      "db_transaction_gettransactions",
      "db_transaction_getbalance"
   ],
   "AppBundle\\Controller\\DefaultController":[  
      "homepage",
      "route"
   ]
}

When I start server with command php bin\console server:run all routes works.

What is wrong and how I can access the API methods?

Upvotes: 4

Views: 379

Answers (1)

user6216601
user6216601

Reputation:

I used https://phpdocker.io/ for generate a files and works.

Upvotes: 1

Related Questions