Running a script using docker compose

docker
circle.yml
bash

#1

I managed to replicate my development environment using docker-compose in circleci using a docker/docker-compose image. It really works great :slight_smile:

version: 2
jobs:
 build:
   docker:
     # using custom image, see .circleci/images/primary/Dockerfile
     - image: docker/compose:1.16.1
   working_directory: ~/tfm

   environment:
     TEST_RESULTS: /tmp/test-results

   steps:
     - checkout

     - setup_remote_docker

     - run:
         name: Build docker-compose containers
         command: |
           docker-compose build

     - run:
         name: Start containers
         command: |
           docker-compose up -d

     - run:
         name: Verify it's working
         command: |
           docker-compose run app ./script.sh

But I have hit a wall when trying to execute a script inside one of the containers.

I have tried running a command using docker-compose.yml specifying the command option for a specific container using

comand: ./script.sh

But I get the follwing error:

ERROR: Cannot start service app: oci runtime error: container_linux.go:265: starting container process caused "exec: \"./script.sh\": stat ./script.sh: no such file or directory"

If I try running the script in .circleci/config.yml (as in the code above)

  - run:
      name: Verify it's working
      command: |
        docker-compose run app ./script.sh

I still get the same error (even when I specify the full path to the script).

Any ideas on how to execute a script inside a container using docker-compose?


#2

Yep. Docker Compose will select predictable container names when starting up your container set, so you can just use one of those (see docker ps to get the names).

For example if you have a container called app_widget_server then you can use:

docker exec -it app_widget_server /path/to/script.sh

The -it is to get a console, so you can interact with it if necessary, but experiment here.

Or if you prefer using service names rather than container names, look at docker-compose exec, which is pretty much the same thing.


#3

Hi @halfer, thank you for your reply.

I did check and I did get the name correctly, by executing this command:

docker-compose config --services`

The problems seems to be that the files from the repository are not visible in the app container.

This is a bit strange as on the development environment the files are accessible in the app container, but not on CircleCI.

On the docker-compose.yml file the volumes are set this way (this works in the development environment):

app:
  build: .
  volumes:
    - '.:/var/www/rails'
  working_dir: /var/www/rails 

Any suggestions on how to properly mount the repository files on the docker-compose service app using the CircleCI environment?


#4

I suspect you’re bumping into the Docker volume problem:

If you just want to do a checkout in /var/www/rails inside that container, then one option is to build that container in another CircleCI build process, and then pull the image from your registry before starting DC.

Also, you could try mounting a container volume instead of an on-host volume - I think I have that working somewhere :smile_cat:


#5

Thank you for your quick reply :slight_smile:

Could you give me a bit more guidance on how to mount a container volume instead of a on-host volume?


#6

I’m happy to help if you get stuck, but do please search first! I just typed “How to mount a container volume in Docker?” into a search engine, and the Docker docs on volumes were the second result.


#7

No problem, thanks, I will keep looking. If I get stuck I will let you know :slight_smile:


#8

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.