0

I have deployed and initialised locally a mongo container with docker-compose:

services:
  mongo-db:
    image: mongo:latest
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: '${DB_USER}'
      MONGO_INITDB_ROOT_PASSWORD: '${DB_PASSWORD}'
    ports:
      - '27017:27017'
    networks:
      - backend-network
    volumes:
      - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
      - mongodb-data-local:/data
volumes:
   mongodb-data-local: 

Now I am trying to deploy the same container in ECS with this task definition:

- name: Create task definition
  community.aws.ecs_taskdefinition:
    containers:
    - name: mongo-db
      essential: true
      image: "mongo"
      # cpu: 128
      # memory: 340
      environment: 
        - name: MONGO_INITDB_ROOT_USERNAME
          value: "{{ lookup('env', 'DB_USER') }}"
        - name: MONGO_INITDB_ROOT_PASSWORD
          value: "{{ lookup('env', 'DB_PASSWORD') }}"
      mountPoints:
        - containerPath: ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
          sourceVolume: mongodb-data-local
      portMappings:
      - containerPort: 27017
        hostPort: 27017
    volumes:
      - name: mongodb-data-local

the file init-mongo.js is in my local machine.

The ECS container stops with the following error:

Status reason CannotCreateContainerError: Error response from daemon: invalid volume specification: 'ecs-backend-task-22-mongodb-data-local-a8d2c78eb3eae18e9501:./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro'

Is it possible to initialise this container in the task definition? What am I missing? Thanks for any answer!

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Domenico
  • 29
  • 6
  • 1
    Hi Domenico welcome to SO. As the error specifies, `containerPath:` is wrong, and should be _one location_, not the `--volume ${src}:${dest}:ro` syntax you have copied over from docker. [The fine manual](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-volumes.html) offers the gory details, including how to mimic that `:ro` via `readOnly: true` – mdaniel May 21 '22 at 23:09
  • Thanks @Zeitounator, I solved my problem and your answer definitely put me in the right direction. I defined the mount point in the continer like this: `mountPoints: - containerPath: "/docker-entrypoint-initdb.d/init-mongo.js" sourceVolume: db-init readOnly: true` and then I defined the volume like this: `volumes: - name: db-init host: sourcePath: "/home/ec2-user/init-mongo.js"` – Domenico May 25 '22 at 07:28

0 Answers0