docker mac run debug mode

To run Go code in debug mode using Docker on a Mac, you can follow the steps below:

  1. Install Docker: If you haven't already, install Docker on your Mac by downloading and running the Docker Desktop installer from the official Docker website.

  2. Create a Dockerfile: In your project directory, create a file named "Dockerfile" (without any file extension). Open the file and add the following lines:

Dockerfile FROM golang:latest WORKDIR /app COPY . . RUN go mod download CMD ["dlv", "debug", "--headless", "--listen=:2345", "--api-version=2", "--log"]

This Dockerfile specifies that we want to use the latest version of the Go language image, sets the working directory to "/app" inside the container, copies the current directory into the container, downloads the Go module dependencies, and runs the Delve debugger in headless mode on port 2345.

  1. Build the Docker image: Open a terminal and navigate to the directory containing the Dockerfile. Run the following command to build the Docker image:

docker build -t my-go-app .

This command builds the Docker image based on the instructions in the Dockerfile and tags it with the name "my-go-app".

  1. Run the Docker container in debug mode: Once the Docker image is built, you can run a container in debug mode using the following command:

docker run -p 2345:2345 --name my-go-app-debug -d my-go-app

This command starts a Docker container named "my-go-app-debug" in detached mode, maps the container port 2345 to the host port 2345, and uses the "my-go-app" image.

  1. Attach a debugger: With the container running, you can now attach a debugger to it. You can use any IDE or text editor that supports Go debugging, such as Visual Studio Code with the Go extension. Configure your debugger to connect to "localhost:2345" to start debugging your Go code inside the Docker container.

That's it! You should now be able to run your Go code in debug mode using Docker on your Mac. Happy debugging!