Search code examples
dockerdockerfile

How to convert Docker run flags into a Dockerfile


I have this docker run command:

docker run -d \
    --name node1 \
    --network host \
    -v $(pwd)/genesis.json:/opt/besu/genesis.json \
    -v $(pwd)/keys/validator1/key:/opt/besu/key \
    --env-file $(pwd)/common.env \
    --env-file $(pwd)/bootnodes.env \
    node1:latest

This works fine.

But I want to put the flags inside the Dockerfile, to just run docker run -d node:latest,

I've tried to use the CMD[] inside Dockerfile, but didn't work.

The container runs but after 1-3 seconds it dies.


Solution

  • You can't get access to host resources from inside a Dockerfile. It's very deliberate that those access rights have to be given at run-time. Otherwise it would be easy to distribute malicious Docker images that could access resources on the host machine without the knowledge of the user owning the host.

    What you can do, is create a Docker Compose file called docker-compose.yml containing

    version: '3'
    
    services:
      myservice:
        container_name: node1
        image: node1:latest
        volumes:
          - ./genesis.json:/opt/besu/genesis.json
          - ./keys/validator1/key:/opt/besu/key
        env_file: 
          - ./common.env
          - ./bootnodes.env
        network_mode: host
    

    That'll let you run the container using a simple

    docker compose up -d
    

    command, so you don't have to remember all the options.