microsoft / DockerTools

Tools For Docker, including Visual Studio Provisioning and Publishing
Other
173 stars 26 forks source link

How to add a pre-launch command to the project #370

Closed dazinator closed 1 year ago

dazinator commented 1 year ago

When I launch a docker-compose project in VS 2022, i'd like to do some initial prep. In other project types you can use a pre-build event. However for dcproj there isn't any such options. Is there some existing hook whereby I can run some command when the dcproj is launched (I would say or built but I am not sure whether this project type has build actions in the traditional sense).

NCarlsonMSFT commented 1 year ago

The .dcproj build has several extensibility points, including targets. Can you clarify what "initial prep" looks like? Are you trying to run a command in the container(s) or on the host?

dazinator commented 1 year ago

On the host.

I'd like to do some initial prep like to ensure an external network that is referenced in the compose file, is created using docker network create with a deterministic name. Networks owned by a compose file (e.g non external) do have the benefit that they do get auto created when the compose project is spun up, but the final network name is not deterministic as far as I can tell due to the names of networks owned by a complse file being prefixed by some gibberish value. So for my case I'm using an external network - which must exist on the docker host otherwise the compose file will fail to start, and its currently a manual task for the dev to ensure they have created this external network on their host prior to running the project. My ideal scenario would be, when the compose project is launched, I could run a script that will run some docker commands to check the external network with the required name exists and if not, create it.

NCarlsonMSFT commented 1 year ago

In that case you should be able to have a target with a BeforeTargets=DockerPrepareForLaunch that runs the command you need.

As a warning: there is a chance that the targets will change in the future. That said, it hasn't been renamed in at least 6 years, so it's proven stable so far.

dazinator commented 1 year ago

Thanks that sounds perfect. I will give that a go.