Skip to main content

Troubleshooting "Command Not Found" in Multi-Container Docker Jobs

Problem

When using multiple Docker images in your job, you receive a "command not found" error for database commands like postgres, mysql, or mongo. This occurs because commands run in the primary container (first image listed), which does not include the database client tools from secondary containers.

Example error:

/bin/bash: {name_of_command}: command not found

Solutions

Understand Where Commands Execute

All job steps run in the primary container, which is the first image listed in your configuration. Secondary containers (database services) run separately and are accessible only through their exposed ports.

In this example, all commands execute in the Python container, not the PostgreSQL container:

jobs:
  build:
    docker:
      - image: cimg/python:3.6.4    # Primary - all steps run here
      - image: cimg/postgres:9.6.2   # Secondary - accessible via port 27017

Install Database Client Tools

To interact with a database service, install the corresponding client tools in your primary container.

For PostgreSQL:

- run: sudo apt-get update && apt-get install -y postgresql-client
- run: psql -V

For MySQL:

- run: sudo apt-get update && apt-get install -y mysql-client
- run: mysql --version

For MongoDB:

- run: sudo apt-get update && apt-get install -y mongodb-clients
- run: mongo --version

Use Application Database Drivers

Instead of using command-line clients, connect to databases using your application's database driver or library. For example, Python applications can use psycopg2 for PostgreSQL or pymongo for MongoDB.

Install the driver through your package manager:

- run: pip install psycopg2-binary

Outcome

After installing the required client tools or database drivers, you can successfully interact with your database service from the primary container. Database commands will execute without "command not found" errors.

Additional Notes

  • Secondary containers are accessible via localhost and their default ports

  • The primary container determines which tools and commands are available during job execution

  • Installing packages during the build adds time to your job execution

  • Client tool versions should match the database service version to avoid compatibility issues

Additional Resources

Did this answer your question?