1

I would like to use the existing container multiple times by providing different arguments. I have a docker-compose.yml file with entrypoint: ["/bin/bash", "entrypoint.sh"].

To run the container I use the command docker-compose run foo --database=foo --schema=boo --tables=puah. It works perfect. Container does the job.

Here is the docker-compose.yml

version: "3"
services:
   bcp:
      image: ubuntu:18.04
      restart: always
      tty: true
      entrypoint: ["/bin/bash", "/ingestion/bcp-entrypoint.sh"]
      volumes:
          - ./services/bcp:/ingestion/services/bcp
          - ./bcp-entrypoint.sh:/ingestion/bcp-entrypoint.sh

Here is the bcp-entrypoint.sh

#!/bin/bash

INGESTION_DIR=/ingestion
TMP_DIR=/tmp/ingestion

apt-get update
apt-get upgrade -y
apt-get clean -y
apt-get install -y python3-pip
apt-get install -y curl

curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | tee /etc/apt/sources.list.d/msprod.list

apt-get update
ACCEPT_EULA=Y apt-get install -y mssql-tools unixodbc-dev

cd ${INGESTION_DIR}
mkdir ${TMP_DIR}

python3 -m --database $database --schmema $schema --tables ${TABLES}

My problems:

  1. The container restarts all the time and keeps retrieving the data with the same arguments provided in docker-compose run bcp ...?
  2. I would like to use one container and overwrite the arguments, so I can skip costly installation.

Maybe there is a combination of entrypoint & command in docker-compose.yml? So, basically, I would like to execute python3 -m --database ... --schema ... --tables .... Ideally I would do it purely in docker-compose without a dockerfile.

2 Answers 2

2

I would like to use the existing container multiple times by providing different arguments.

If you want to change entrypoint or cmd of an already existing container, you can't. Once a container is created, most of its configuration cannot be changed (see docker update for updatable container configs)

Keep in mind:

  • docker-compose run will create and start a container with given arguments (you can then override entrypoint or cmd)
  • docker-compose exec run a command in a running container. It won't work in a stopped container, and won't create a new container.
  • docker start start a stopped container, the container will start with already defined cmd and entrypoint. You won't be able to change that.

You can do something like docker-compose run --entrypoint 'sleep 9999' foo which will start your container and ensure it's running for 9999 seconds, then execute commands with docker-compose exec such as

# similar to what would happen with 'docker-compose run foo --database=foo --schema=boo' considering entrypoint is ["/bin/bash", "entrypoint.sh"]'
# '--database=foo --schema=boo' would be passed as argument of entrypoint.sh
docker-compose exec "/bin/bash -c 'entrypoint.sh --database=foo --schema=boo'"
docker-compose exec "/bin/bash -c 'entrypoint.sh --database=blah --schema=wooow'"
Sign up to request clarification or add additional context in comments.

2 Comments

Can I use tty: true instead? In the docker-compose.yml. Unfortunately the command does not work.
Thanks for the reply! I have updated the question. Now, you have more transparency! Maybe that will help!
0

docker-compose run is using an image to create a container with the provided arguments.

Use docker-compose run foo --name foo1 --[other arguments] to end up with a container for each set of arguments.

If you don't want to keep the container once the job is done then include the -rm option to remove the container on close.

3 Comments

Thanks for the reply. The point is, I do want to use one container once created after run command, since there is a lot packages to be installed in the container as per entrypoint.sh. Thus, if I docker-compose run ... every time, it will take a lot of time to install all these packages to run a single command. To avoid this, I want to use existing container with different arguments - i.e., no need to create another container from the image.
If you're installing packages, you probably want to do it via a Dockerfile, so it will get done just once. Once you've done that, creating new containers from the image should be fairly cheap.
I take the image of ubuntu and then do apt-get install ... in entrypoint.sh. If I do docker-compose run -service- then new container is created and all packages are installed another time (since a new container). That is why I avoid --rm while creating a container.

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.