Initialize a Go Project with Docker Init
In this step, we will initialize a new Go project and use docker init
to generate the necessary Dockerfile and docker-compose.yml files. This will provide a starting point for containerizing our Go application.
First, let's create a new directory for our Go project. Open a terminal in the LabEx VM and execute the following commands:
mkdir my-go-app
cd my-go-app
These commands create a new directory named my-go-app
in the ~/project
directory and then change the current directory to my-go-app
.
Next, we need to initialize a Go module. This will create a go.mod
file that tracks the dependencies of our project.
Execute the following command in the terminal:
go mod init my-go-app
This command initializes a new Go module named my-go-app
. You should see output similar to:
go: creating new go.mod: module my-go-app
go: to add module requirements and sums:
go mod tidy
Now, let's create a simple Go application. Create a new file named main.go
in the my-go-app
directory using the nano
editor:
nano main.go
Add the following code to the main.go
file:
package main
import "fmt"
func main() {
fmt.Println("Hello, Docker!")
}
This is a simple Go program that prints "Hello, Docker!" to the console.
Save the file and exit the nano
editor by pressing Ctrl+X
, then Y
, and then Enter
.
Now, we will use docker init
to generate the Dockerfile and docker-compose.yml files. However, the LabEx VM does not have Docker Compose installed by default. We need to install it first.
Download the latest version of Docker Compose using curl
:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Make the Docker Compose binary executable:
sudo chmod +x /usr/local/bin/docker-compose
Verify the installation by checking the Docker Compose version:
docker-compose --version
You should see output similar to:
docker-compose version 1.29.2, build xxxxxxx
Now that Docker Compose is installed, we can proceed with docker init
.
Execute the following command in the terminal:
docker init
docker init
will ask a series of questions to configure the Dockerfile and docker-compose.yml files. Here are the recommended answers for this lab:
- What do you want to call this application?
my-go-app
- What port is this app listening on?
3000
(This is a placeholder, we won't actually use a port in this simple example)
- What is the main file to execute?
main.go
- Would you like to include the Docker Compose configuration?
Yes
- Please choose a Docker Compose version:
2.0
After answering these questions, docker init
will generate a Dockerfile
and a docker-compose.yml
file in the my-go-app
directory.
You can view the contents of the generated files using the cat
command:
cat Dockerfile
cat docker-compose.yml
These files provide a basic configuration for building and running our Go application in a Docker container. In the next step, we will customize these files to better suit our needs.