I’m thinking of using Docker to build my dependencies on a Continuous Integration (CI) server, so that I don’t have to install all the runtimes and libraries on the agents themselves.
To achieve this I would need to copy the build artifacts that are built inside the container back into the host. Is that possible?
Yes, it’s possible to copy build artifacts from a Docker container back to the host machine. You can use the docker cp command to copy files or directories between a container and the host.
docker cp
Here’s a basic example:
Assume you have a Dockerfile that builds your dependencies and creates some artifacts. Build the Docker image and run a container:
docker build -t my-build-image . docker run --name my-build-container my-build-image
Use the docker cp command to copy artifacts from the container to the host. In the following example, we are copying the /app/artifacts directory from the container to the current directory on the host:
/app/artifacts
docker cp my-build-container:/app/artifacts .
Replace my-build-container with the actual name or ID of your running container.
my-build-container
Now, the artifacts from the container are available on the host machine.
Keep in mind that if your build process involves creating files in specific locations inside the container, you’ll need to know those locations to copy the files correctly.
Additionally, another approach is to use volumes to share data between the host and the container. You can mount a volume to a specific directory inside the container where your artifacts are stored. This way, the artifacts will be available on the host without explicitly copying them.