Search code examples
docker

How to copy a file to a Docker container before starting/running it?


I'm starting with a docker image that is already built. I would like to do this

  1. Create a docker container from this image. (Don't start)
  2. Copy a file to this container
  3. Start the container

How can this be achieved. It looks like if i run the following commands the file doesn't end up in the container

  1. docker create --name my_container my_image
  2. docker cp file my_container:/tmp/file
  3. docker start my_container

Any idea how this can be achieved ?


Solution

  • You will have to create a new image from a Dockerfile that inherit from the one that is already built, and the use the COPY tag:

    Dockerfile

    FROM my_image
    COPY file /tmp/file
    

    Finally, build that new Dockerfile:

    $ docker build -t new_image .