Docker Community Forums

Share and learn in the Docker community.

Unable to reach my database after pulling my image from distant server


(Jaeger767) #1

I’m slowly getting used to Docker but I still have lots of struggle to understand how things work.

I also asked this question on Stack Overflow but it didn’t get much answers, so I’m trying my luck here!

I have a simple Laravel website that I dockerized to (try to) make my deployments easier. Here is my docker-compose.yml

version: '3'
services:
  web:
    build: .
    env_file:
      - .env.docker
    depends_on:
      - db
    ports:
      - '8000:80'
  db:
    image: mariadb:latest
    restart: always
    env_file:
      - .env.mysql
    ports:
      - 3306

With this configuration, when I run docker-compose up --build, my website is available at localhost:8000. (I chose this because this isn’t my main website, and want to set this website in a subdomain by using Apache’s reverse proxy, everything works here)

So, on my local machine, when running docker-compose up --build and waiting for the image to get built, after running docker ps, I always get something like this:

![docker%20ps|690x29](upload://plqaGi701RDworNv8sSMoIuQEdL.png

After this, I push this image to Docker Hub, then pull it from a distant server and run it by doing the following:

docker run -d -p 8000:80 myusername/stadium

When running docker ps on this distant server, here is what i get:

But when trying to run the migrations after running the container, I get the following error:

SQLSTATE[HY000] [2002] Connection refused

And when logging into my image (docker exec -ut <random_given_name> bash -l), it returns me that the mysql command isn’t known.

My questions are:

  • What’s happening here?
  • Wasn’t the mariadb image supposed to be “inserted” into my final image when it got built?
  • Do I need some more actions from the distant server when pulling the image?
  • Is my database actually here but I’m doing something wrong?

“of course”, my database credentials are good, I checked them countless times and got some issues with them while setting everything up and finally got to the point where I could save things up locally in my running docker image.

Thank you in advance


(David Maze) #2

No. Your docker-compose.yml file declares two containers, one running the database and one running your application. They don’t get baked together into a single image; if you ran docker ps when you had them running locally you’d see both containers running.

Probably the easiest way to run this setup “somewhere else” is to copy the docker-compose.yml file, but change the build: . line to image: myusername/stadium (matching the name and tag of the image you pushed to Docker Hub). Then that YAML file, and the referenced environment files, are enough to run this set of containers anywhere, without needing the source from the rest of it.


(Jaeger767) #3

Thank you for your answer, I’m starting to understand better what’s going on, and managed to make my project run after a few days of struggle!