skip to Main Content

High level- i have front end web application which runs on one docker container and i made second container for database mysql.
I picked a environment variable mysqldb i need to set that variable to ip address of Docker MySQL container. Part two- i got web application which has to know what ip address is running on( mysql container whatever its going to be because the ip of the container will change) so it has to read that environment variable that was set. So my question do i set a variable so when i run the program mysql container runs and shows that database i set up is working

Dockerfile

FROM golang:1.19-bullseye AS build     
WORKDIR /app
COPY ./ ./
RUN go build -o main ./

FROM debian:bullseye
COPY --from=build /app/main /usr/local/bin/main
#CMD[apt-get install mysql-clientmy]
CMD ["/usr/local/bin/main"]

makefile

build:
    go build -o bin/main main.go
    
run:
    go run main.go
runcontainer:
    docker run -d -p  9008:8080 tiny


compile:
    echo "Compiling for every OS and Platform"
    GOOS=linux GOARCH=arm go build -o bin/main-linux-arm main.go
    GOOS=linux GOARCH=arm64 go build -o bin/main-linux-arm64 main.go
    GOOS=freebsd GOARCH=386 go build -o bin/main-freebsd-386 main.go    

part of my go program

func main() {

    linkList = map[string]string{}

    http.HandleFunc("/link", addLink)
    http.HandleFunc("/hpe/", getLink)
    http.HandleFunc("/", Home)

    ip := flag.String("i", "0.0.0.0", "")
    port := flag.String("p", "8080", "")
    flag.Parse()
    fmt.Printf("Listening on %s n", net.JoinHostPort(*ip, *port))
    log.Fatal(http.ListenAndServe(net.JoinHostPort(*ip, *port), nil))
}

2

Answers


  1. Yes, you can achieved this by using env variable in Dockerfile or Docker compose file. By the way don’t use IP of db container. Always use hostname. Hostname is static but every recreation of container IP will be get changed.

    Login or Signup to reply.
  2. You can do it the following way in the Dockerfile itself(The example itself if from the Docker documentation):

    FROM busybox
    ENV FOO=/bar
    WORKDIR ${FOO}   # WORKDIR /bar
    ADD . $FOO       # ADD . /bar
    COPY $FOO /quux # COPY $FOO /quux
    

    In case of a Docker-compose file, you can try to do the following:

    version:'3'
    services:
      mysqldb:
        container_name: mydb
        restart: always
        env_file:
          - db.env
    

    You should change it according to your requirements.

    Addendum:

    As far as I can understand what you’re trying to achieve, and you can solve this problem by using a db_env in your go program structure as well as your docker-compose file.

    Try to do the following:

    • create a .env file in your go project structure,
    • add HOST=172.0.0.1
    • Read the variable in your go program using either a third party package like viper or simply using os.Getenv("HOST")

    create a docker-compose file and add both the services you’re supposed to create.
    You can look at the example that I provided earlier and then create services accordingly by specifying the same db_env below the docker-compose env_file flag.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search