Docker Deployment
This guide explains how to deploy the Go REST API using Docker.
Prerequisites
- Docker installed on your machine
- Basic knowledge of Docker commands
Files
The following file in the /deployments/docker
directory is used for the Docker deployment:
Dockerfile
: Contains instructions for building the Docker image
Deployment Steps
-
Navigate to the project root directory:
cd go-rest-api
-
Build the Docker image:
docker build -t go-rest-api:latest -f deployments/docker/Dockerfile .
-
Run the Docker container:
docker run -p 8080:8080 go-rest-api:latest
This command will start the container and map port 8080 from the container to port 8080 on your host machine.
-
The API should now be accessible at
http://localhost:8080
Dockerfile Explanation
The Dockerfile
uses a multi-stage build process:
-
Build stage:
- Uses the official Go image as the base
- Sets the working directory
- Copies the Go module files and downloads dependencies
- Copies the source code and builds the application
-
Final stage:
- Uses a minimal Alpine Linux image
- Copies the built binary from the build stage
- Sets the entry point to run the application
This approach results in a smaller final image that contains only the necessary components to run the application.
Customization
To customize the Docker deployment:
- Modify the
Dockerfile
if you need to change the build process or add additional dependencies. - Adjust the exposed port in both the
Dockerfile
and thedocker run
command if you want to use a different port.
Additional Docker Commands
-
To stop the running container:
docker stop $(docker ps -q --filter ancestor=go-rest-api:latest)
-
To remove the Docker image:
docker rmi go-rest-api:latest
-
To view logs of the running container:
docker logs $(docker ps -q --filter ancestor=go-rest-api:latest)
Docker Compose (Optional)
If you want to use Docker Compose for easier management, especially when integrating with other services, you can create a docker-compose.yml
file in the project root: