Jerome
Jerome

Reputation: 1192

How to use docker run with a Meteor image?

I have 2 containers mgmt-app who is a Meteor container and mgmt-mongo who is the MongoDB.

CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
7b65be4ac454        gitlab-lab:5005/dfc/mongo:latest          "/entrypoint.sh mongo"   About an hour ago   Up About an hour    27017/tcp                mgmt-mongo
dff0b3c69c5f        gitlab-lab:5005/dfc/mgmt-docker-gui:lab   "/bin/sh -c 'sh $METE"   About an hour ago   Up 42 minutes       0.0.0.0:80->80/tcp       mgmt-app

From my Docker host I want to run docker run gitlab-lab:5005/dfc/mgmt-docker-gui:lab ls -al

but I have this error:

=> Starting meteor app on port:80
/app/programs/server/node_modules/fibers/future.js:280
                        throw(ex);
                        ^
Error: MONGO_URL must be set in environment

So I tried:

docker run -e "MONGO_URL=mongodb://mgmt-mongo:27017/meteor" gitlab-lab:5005/dfc/mgmt-docker-gui:lab ls -al

and then the error was:

/app/programs/server/node_modules/fibers/future.js:313
                        throw(ex);
                        ^
MongoError: failed to connect to server [mgmt-mongo:27017] on first connect

I really don't understand because when I do a docker-compose up -d with this file:

mgmt-app:
    image: gitlab-lab:5005/dfc/mgmt-docker-gui:latest
    container_name: mgmt-app
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - $HOME/.docker:/root/.docker
      - /home/dockeradm/compose/area:/home/dockeradm/compose/area
    environment:
      - ROOT_URL=http://localhost:80
      - MONGO_URL=mongodb://mgmt-mongo:27017/meteor
    ports:
      - 80:80
    restart: always

  mgmt-mongo:
    image: gitlab-lab:5005/dfc/mongo:latest
    container_name: mgmt-mongo
    volumes:
      - mgmt_mongo_data_config:/data/configdb
      - mgmt_mongo_data_db:/data/db
    restart: always

everything go well.

So my request is, how should I do my docker run to execute my command ? (the command is not a simple ls -al but it's ok for the demo)

Upvotes: 0

Views: 128

Answers (1)

Vince Bowdren
Vince Bowdren

Reputation: 9208

When you run the containers separately with docker run, they are not linked on the same docker network so the mongo container is not accessible from the app container. To remedy this, you should use either:

  1. --link to mark the app container as linked to the mongo container. This works, but is deprecated.
  2. a defined docker network for both containers to be linked by; this is more complex, but is the recommended architecture

By contrast, docker-compose automatically adds both containers to the same docker network, so they are immediately connectable without any extra configuration required:

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

Upvotes: 1

Related Questions