3

How can I change the port dynamodb starts on through the Amazon Docker image?

According to this answer, the -port option can be used when executing the dynamodb java file.

However, when running the docker image with this command: docker run -p 8000:8000 amazon/dynamodb-local I do not have the option of specifying the port dynamodb listens to, just the port connected between my host and the container.

Would I have to make my own Dockerfile, specifying the OS and installing dynamodb and whatnot, so I can run the java command and specify my port?

Dylan Landry
  • 1,150
  • 11
  • 27

3 Answers3

8

You don't need to rebuild the image. Doing

docker inspect amazon/dynamodb-local

shows that the Entrypoint is set as

  "Entrypoint": [
   "java"
 ],

So running the below command gives an error:

 $ docker run -p 8001:8001 amazon/dynamodb-local -port 8001
 Unrecognized option: -port
 Error: Could not create the Java Virtual Machine.
 Error: A fatal exception has occurred. Program will exit.

because we are trying to pass -port argument to java but we need to pass it to DynamoDBLocal.jar.

Once we know that, we can add the jar to the docker run and the following works:

$ docker run -p 8001:8001 amazon/dynamodb-local -jar DynamoDBLocal.jar -port 8001

Initializing DynamoDB Local with the following configuration:
Port:   8001
InMemory:       false
DbPath: null
SharedDb:       false
shouldDelayTransientStatuses:   false
CorsParams:     *

I would raise it as a bug but https://hub.docker.com/r/amazon/dynamodb-local/ doesn't mention a public github repo to raise the issue.

Saurabh Hirani
  • 1,198
  • 14
  • 21
  • 7
    for those using `docker-compose`, simply add a `command` under the dynamodb service. E.g. `command: -jar DynamoDBLocal.jar -port 8001 # pass in custom port to dynamoDB jar file` – Geoff Sep 09 '19 at 22:41
  • adding -port option to command fixed my problem thanks. – M_K Jul 04 '23 at 16:20
3

For some reason docker-compose does not map ports to the host for dynamodb. The next configuration will not work for the dynamodb-local image:

    ports:
    - "9000:8000"

The workaround is to put the port directly to the command, -port 9000 is the same as ports: - "9000:9000" in docker-compose.

version: "3.8"

services:
  service-dynamodb:
    image: amazon/dynamodb-local
    image: "amazon/dynamodb-local:latest"
    working_dir: /home/dynamodblocal
    # -port {number} is the same as 
    # ports: - {number}:{number} in Docker
    command: "-jar DynamoDBLocal.jar -port 9000 -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
    volumes:
      - ./data:/home/dynamodblocal/data
Artur A
  • 7,115
  • 57
  • 60
2

I tried the official image to override entry point but there was some unknown error, But you can good to go with this approach. Just create a New Docker image from amazon/dynamodb-local as a base image. Build.

docker build -t mydb .

and run it

docker run -it --rm -p 8001:8001 mydb

Below is the Dockerfile

FROM amazon/dynamodb-local
WORKDIR /home/dynamodblocal
ENTRYPOINT ["java", "-jar", "DynamoDBLocal.jar", "-port", "8003"]

As you will see the port.

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • I was wondering how you found out to change the `WORKDIR` to `/home/dynamodblocal/`, though I understood when I changed the `ENTRYPOINT` to `["/bin/sh"]` and was then able to use a shell to see the image's directory structure. – Dylan Landry Jul 04 '19 at 10:32
  • 1
    i did it using `docker inspect amazon/dynamodb-local` – Adiii Jul 04 '19 at 10:34