How test unit of project in docker compose

Hello

I’m having a problem with my code tests. I’m using a microservices architecture, so I’m using Docker Compose, but when I want to run my project’s tests in Docker, I use the following command:

docker-compose exec <service> <shell> 

I’m not sure whether I should create another Docker Compose file to handle the tests for each service, or what the best practice would be

Thank for the helping

What is the problem exactly? Did you just want to ask if you are doing it right already?

Since you want unit testing, I don’t see why you would execute a command in a runnig container. I assume you have the source code somewhere outside the container. The container is for running the program. Depending on what the program is, you could also need to build it first. If it is PHP or other scrpinting language that doesn’t require building, you will still want to test the code, not the runtime environment where you could need different dependencies. So you may want to create a different image foir testing and mount the source code or create a testing image based on the original image.

It is hard to say more without knowing what exactly you are testing, but I’m not even sure that you need docker compose for testing.

.(I also hope you are using Docker Compose v2 at least or even v5. docker-compose is the syntax of v1 discontinued long time ago and could work as an alias in newer versions as well)

hello, this error
OCI runtime exec failed:
exec failed: unable to start container process:
exec: “go”: executable file not found in $PATH

That error seems clear, but what it means I can only guess without knowing what you did exactly. BAsed on your previous message, I assume you wanted to run “go” inside the container and its parent folder is not in the PATH env variable. So you either know where that go binary is and use absolute path to execute it like

/opt/go/bin/go $args

or you recreate the container with a modified PATH. I would probably do it in a Dockerfile though and recreate the base image.

ENV PATH="$PATH:/opt/go/bin"

Using the correct path of course, not what I used as an example.

Or maybe there is no “go” binary in the container at all. If you can share more details about your project, what you want to test, why you think the go binary should be there in the container, someone can help even more.

I am with @rimelek. If you want to run unit tests in the final image, then none of the build tools should be available, at least not if the image was done right.

Either you run your unit test as part of the build stage of a multi-stage build and store the test reports somewhere in the filesystem, and then create a container from the final image to docker cp the report from the container filesystem to the docker cli client’s filesystem. Or you run your builds and unit tests in jobs inside your CI pipeline.

If you don’t want the CI pipeline route, then you would run your unit tests locally, and use docker compose to provide 3rd party services your unit tests needs as a container (which should be zero container if you run component tests, and one or more if you run integration tests against 3rd party services).

You could also use devcontainers to run your tooling container, and all 3rd party service containers, if you don’t want to install the tools on your host system. IDE’s like VSCode or JetBrain IDEs have a solid devcontainers integration.

@meyay @rimelek Thank you very much for your help; that really was the problem I was having, and your advice was a great help.