Vincent
Vincent

Reputation: 5249

equivalent docker run command for working docker-compose (postgres)

If have a docker-compose file for postgres that works as expected and I'm able to access it from R. See relevant content below. However, I also need an equivalent "docker run" command but for some reason cannot get this to work. As far as I can tell the commands / setup are equivalent. Any suggestions?

  postgres:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      PGDATA: /var/lib/postgresql/data
    ports:
      - 5432:5432
    restart: always
    volumes:
       - ~/postgresql/data:/var/lib/postgresql/data

The docker run command I'm using is:

    docker run -p 5432:5432 \
      --name postgres \
      -e POSTGRES_USER=postgres \
      -e POSTGRES_PASSWORD=postgres \
      -e PGDATA=/var/lib/postgresql/data \
      -v ~/postgresql/data:/var/lib/postgresql/data \
      -d postgres

EDIT 1: In both settings I'm trying to connect from another docker container/service. In the docker-compose setting the different services are described in one and the same yml file

EDIT 2: David's answer provided all the information I needed. Create a docker network and reference that network in each docker run call. For those interested in a shell script that uses this setup to connect postgres, pgadmin4, and a data science container with R and Python see the link below:

https://github.com/radiant-rstats/docker/blob/master/launch-rsm-msba-pg.sh

Upvotes: 1

Views: 783

Answers (1)

David Maze
David Maze

Reputation: 158908

Docker Compose will automatically create a Docker network for you (per Compose file). For inter-container DNS to work, you can't use the default Docker network but any named network will work. So you need to add that bit of setup:

docker network create some-name  # default options are fine
docker run --net some-name --name postgres ...
  # will be accessible as "postgres" from other containers on
  # the "some-name" network

Upvotes: 1

Related Questions