1

I want to make a script run a series of commands in a Docker container and then copy a file out. If I use docker run to do this, I don't get back the container ID, which I would need for the docker cp. (I could try and hack it out of docker ps, but that seems risky.)

It seems that I should be able to

  1. Create the container with docker create (which returns the container ID).
  2. Run the commands.
  3. Copy the file out.

But I don't know how to get step 2. to work. docker exec only works on running containers...

2 Answers 2

2

If i understood your question correctly, all you need is docker "run exec & cp" -

For example -

  1. Create container with a name --name with docker run -

    $ docker run --name bang -dit alpine

  2. Run few commands using exec -

    $ docker exec -it bang sh -c "ls -l"

  3. Copy a file using docker cp -

    $ docker cp bang:/etc/hosts ./

  4. Stop the container using docker stop -

    $ docker stop bang

Sign up to request clarification or add additional context in comments.

1 Comment

It was the --name that I missed.
1

All you really need is Dockerfile and then build the image from it and run the container using the newly built image. For more information u can refer to this

A "standard" content of a dockerfile might be something like below:

#Download base image ubuntu 16.04
FROM ubuntu:16.04

# Update Ubuntu Software repository
RUN apt-get update

# Install nginx, php-fpm and supervisord from ubuntu repository
RUN apt-get install -y nginx php7.0-fpm supervisor && \
rm -rf /var/lib/apt/lists/*

#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.0/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf

#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}


# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]

EXPOSE 80 443

2 Comments

I don't have any trouble building my image or running it. The problem is getting the container ID.
you can specify a name for the container before-hand docker run --name compassionate_darwin nginx and using the specified named on docker cp compassionate_darwin:/tmp/foo/myfile.txt instead of using the docker container id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.