DenCowboy
DenCowboy

Reputation: 15076

Automatically run command inside docker container after starting up + volume mount

I have created my simple own image from .

FROM python:2.7.11
RUN mkdir /extra/later/ \
  && mkdir /yyy

Now I'm able to perform the following steps:

docker run -d -v xxx:/yyy myimage:latest 

So now my volume is mounted inside the container. I'm going to access and I'm able to perform commands on that mounted volume inside my container:

docker exec -it container_id bash
bash# tar -cvpzf /mybackup.tar -C /yyy/ .

Is there a way to automate this steps in your Dockerfile or describing the commands in your docker run command?

Upvotes: 1

Views: 3877

Answers (2)

BMitch
BMitch

Reputation: 263617

The commands executed in the Dockerfile build the image, and the volume is attached to a running container, so you will not be able to run your commands inside of the Dockerfile itself and affect the volume.

Instead, you should create a startup script that is the command run by your container (via CMD or ENTRYPOINT in your Dockerfile). Place the logic inside of your startup script to detect that it needs to initialize the volume, and it will run when the container is launched. If you run the script with CMD you will be able to override running that script with any command you pass to docker run which may or may not be a good thing depending on your situation.

Upvotes: 1

lamirap
lamirap

Reputation: 520

Try using the CMD option in the Dockerfile to run the tar command

CMD tar -cvpzf /mybackup.tar -C /yyy/ .

or

CMD ["tar", "-cvpzf", "/mybackup.tar", "-C", "/yyy/", "."]

Upvotes: 0

Related Questions