skip to Main Content

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.

2

Answers


  1. To achieve the behavior you desire, where you can simply run docker run -d node:latest without specifying the flags each time, you need to properly set up your Dockerfile. The key is to use the ENTRYPOINT and CMD instructions effectively.

    Here’s a step-by-step guide to modify your Dockerfile:

    1. Use ENTRYPOINT for the Fixed Command:
      The ENTRYPOINT instruction in a Dockerfile allows you to configure a container that will run as an executable. In your case, the fixed part of the command is docker run, so you can set your entry point to the binary inside the container that needs to be executed.

    2. Use CMD for Default Parameters:
      The CMD instruction provides default arguments for the ENTRYPOINT command. These can be overridden when the container starts. In your case, these are the flags and parameters you usually pass to the docker run command.

    3. Dockerfile Example:
      Here’s an example of how your Dockerfile might look:

      FROM some-base-image
      
      # Copy necessary files
      COPY genesis.json /opt/besu/genesis.json
      COPY keys/validator1/key /opt/besu/key
      COPY common.env /path/to/common.env
      COPY bootnodes.env /path/to/bootnodes.env
      
      # Set environment variables if needed
      ENV SOME_ENV_VARIABLE=value
      
      # Set the entry point to the application you want to run
      ENTRYPOINT ["executable_inside_container"]
      
      # Set default command and parameters
      CMD ["--name", "node1", "--network", "host", "-v", "/opt/besu/genesis.json", "-v", "/opt/besu/key", "--env-file", "/path/to/common.env", "--env-file", "/path/to/bootnodes.env"]
      
    4. Building and Running the Container:
      After modifying your Dockerfile, build the image with docker build -t node:latest .. Then, you can run your container with the simplified command docker run -d node:latest.

    5. Debugging Container Exit:
      If your container is still exiting early, check the container logs using docker logs [container_id]. This can provide insights into why it’s not staying up.

    Remember, the exact syntax for the ENTRYPOINT and CMD instructions might vary depending on the base image and the application you are running inside the container. Adjust the paths and executable names as necessary for your specific setup.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search