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
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 theENTRYPOINT
andCMD
instructions effectively.Here’s a step-by-step guide to modify your Dockerfile:
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 isdocker run
, so you can set your entry point to the binary inside the container that needs to be executed.Use
CMD
for Default Parameters:The
CMD
instruction provides default arguments for theENTRYPOINT
command. These can be overridden when the container starts. In your case, these are the flags and parameters you usually pass to thedocker run
command.Dockerfile Example:
Here’s an example of how your Dockerfile might look:
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 commanddocker run -d node:latest
.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
andCMD
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.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
containingThat’ll let you run the container using a simple
command, so you don’t have to remember all the options.