简体   繁体   中英

Run bash command before running container

I want to run a pre-existing Docker image like so:

docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium

So there is no Dockerfile that I control for this image. However, I would like to copy some files into this container.

If I did control the Dockerfile, I would like to run these commands:

RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data

Is there a way to run those commands in the same line as the Docker run command above?

I tried this:

docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
docker exec cdt-selenium mkdir -p /root/cdt-tests/csv-data
docker cp cdt-selenium:/root/cdt-tests/csv-data ./csv-data

but I get a permissions error on the docker exec line

All images have a FROM line, and that can be any other image. So you can make a Dockerfile with:

FROM selenium/standalone-firefox:3.4.0-chromium
USER root
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
USER seluser

that will build your own image with your commands run.

You'd build it and create your own tag:

docker build -t alexander/selenium:3.4.0-chromium .

And then run it:

docker run -d --name cdt-selenium alexander/selenium:3.4.0-chromium

Edit: the exec command you ran failed because docker runs this container as a different user. You can see that in their Dockerfile . To solve that, run the exec with the root user option ( -u root ):

docker exec -u root cdt-selenium mkdir -p /root/cdt-tests/csv-data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM