Skip to content

Docker Golang

Erik Palencik edited this page Oct 19, 2026 · 3 revisions

Golang docker file

Basic configuration

Building basic container

This is basic command for building docker container.

FROM golang:1.24.2-alpine

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

Copy . .

EXPOSE 8080

CMD ["go", "run", "main.go"]

# Or you can do

CMD sh -c "go run main.go" # This is wrapped in shell!

To test build docker image use:

docker build -t <IMAGE_NAME>:<IMAGE_VERSION> .

Running built container

You can run container with command:

docker run -d -p 80:8080 --name <RUNNING_INSTANCE_NAME> <IMAGE_NAME>:<IMAGE_VERSION>

Where:

  • 80:8080 - <LOCALHOST_POST>:<CONTAINER_EXPOSED_PORT>
  • name is custom name you want to define, if not defined docker pickes his own name like "bold_roentgen"

After running this command your app should be visible on port 80 via HTTP (if you are doing HTTP applicaton).

You can run multiple containers of same image, all you need is to define new ports like 81, 82,...

To check running containers use:

docker container ls

Output looks following:

CONTAINER ID   IMAGE             COMMAND            CREATED         STATUS         PORTS                                         NAMES
25d9f36bc479   helloword:1.0.0   "go run main.go"   8 minutes ago   Up 2 minutes   0.0.0.0:8082->8080/tcp, [::]:8082->8080/tcp   bold_roentgen

To see all containers (running or not) use:

docker container ls -a

output looks following:

$ docker container ls -a
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS                          PORTS     NAMES
25d9f36bc479   helloword:1.0.0   "go run main.go"         6 minutes ago   Exited (2) About a minute ago             bold_roentgen
47acd6cb7791   helloword:1.0.0   "go run main.go"         7 minutes ago   Exited (2) 4 minutes ago                  divokyjano
0a3c368f29b8   helloword:1.0.0   "go run main.go"         9 minutes ago   Exited (2) 5 minutes ago                  divokyerik
1d1a36699c30   postgres:16       "docker-entrypoint.s…"   10 days ago     Exited (0) 10 days ago                    odoo-postgres

Controlling running instances

To control instances there are 2 ways, both are simmilar to Linux processes commands:

# Old way
docker container stop [CONAINER_NAME]
docker container stop [CONAINER_ID]

# New way
docker stop [CONAINER_NAME]
docker stop [CONAINER_ID]

#New way xample
docker container stop 1d1a36699c30
docker container stop bold_roentgen

In actual version of docker, when doing some command over docker container as successfull result there is container name returned to console.

Another commands are same as Unix process:

docker stop [CONAINER_NAME]
docker start [CONAINER_NAME]
docker restart [CONAINER_NAME]

Adding environment variables

Now let's create env file for MongoDB:

MONGO_DB_CONN_STR="mongodb://host.docker.internal:27017"

Everything what runs outside docker world you have to access via host.docker.internal !

To test run of container can use:

docker run -d -p 80:8080 --name <RUNNING_INSTANCE_NAME> --env-file .env <IMAGE_NAME>:<IMAGE_VERSION>
  • --env-file means adding external file of environment config you can use, it in dotenv environmnent format.

Advanced methods

Docker compose file

Docker compose serves to run multiple docker files.

docker-compose.yaml

Exmpale:

services:
  web:
    depends_on:
      - api
  api:
    container_name: helloapi
    port:
       - 8080:8080
    environment:
       MONGODB_CONN_STR: "mongodb://host.docker.internal:27017"

After you have finished your docker compose file you can:

docker compose up -d

Distro less golang

FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app .

FROM gcr.io/distroless/static
COPY --from=build /app /app
CMD ["/app"]

Clone this wiki locally