Mohan
Mohan

Reputation: 8843

Running multiple commands after docker create

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...

Upvotes: 1

Views: 1386

Answers (2)

vivekyad4v
vivekyad4v

Reputation: 14843

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

Upvotes: 2

SuicideSheep
SuicideSheep

Reputation: 5550

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

Upvotes: 1

Related Questions