V
V
Vadim Kosolapov2019-03-24 20:30:56
go
Vadim Kosolapov, 2019-03-24 20:30:56

How to run a web page in Golang from under Docker?

Hello, I ran into such a problem that I can’t run a Golang website from under Docker.
I have docker-compose , I build golang in it

app:
    build:
      context: ./api/docker
      dockerfile: golang.dockerFile
    volumes:
      - "./api:/go"
    container_name: golang_app
    ports:
      - "9097:8080"
    tty: true
    depends_on:
      - db

I also have a Dockerfile:
FROM golang:latest
RUN apt-get update
RUN go get "github.com/go-sql-driver/mysql"
RUN go get "github.com/gorilla/mux"

When I try to run it all:
sudo docker-compose up --build -d
sudo docker exec -it golang_app bash

I open the page 127.0. will work. What do I need to do to make the site visible?
Here is the whole code: https://github.com/k0v4back/golang-web-applocation

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2019-03-25
@Ynicum_navern

Judging by the code, you are listening on port 9098 on the localhost.
At the same time, you are redirecting the docker to port 8080.

ports:
      - "9097:8080"

1. You need to listen not on the localhost, the docker will not be able to redirect the port to the localhost
2. Correct the ports to the correct ones.
This is how it should work if everything else is correct.
log.Fatal(http.ListenAndServe(":8080", r))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question