Question

How to deploy Go app alongside Nginx using Docker compose?

Posted March 20, 2021 453 views
NginxDockerGo

Hi,

I’m trying to follow this guide to deploy a Go app and Nginx using Docker Compose: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-go-web-application-with-docker-and-nginx-on-ubuntu-18-04

The problem I am having is that my Go application is stuck restarting indefinitely - it doesn’t seem to launch successfully. Nginx and Proxy Companion are running successfully, however. Furthermore,when I go to my domain, which is specified in the docker-compose file for the Go app, I am able to get to an https secured 502 bad gateway. I think this means the docker container for the Go app is being created successfully, but the actual web server isn’t able to listen on port 80. I created all of the Docker/Docker-Compose files to match the tutorial exactly so I’m not sure what the issue is. If I build the Docker file for my go application in isolation, without docker-compose, I can build and run the server. It’s just when I try to to use docker-compose it gets stuck in the restarting status. Any thoughts?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

In case that you have another service listening on port 80 it could prevent the Nginx container from starting.

You can check that with the following command:

netstat -plant | grep 80

If you have Nginx installed on your host, you would need to stop it and disable it before you run the docker-compose up command.

Regards,
Bobby

  • Hi Bobby,

    Thanks for the response. I don’t think this is the issue because I can run the Nginx container successfully. It’s the Go application that is stuck restarting.

    To be sure, I ran the command before and after running the Nginx container.

    After stopping the Nginx container, I receive no output from the command. After starting the Nginx container, I received the following output:

    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -
    tcp        0      0 134.209.118.10:52546    104.21.71.15:80         TIME_WAIT   -
    
    • Hi there,

      Yes indeed, you are absolutely right.

      What I could suggest, is checking the logs of the Go container to see why exactly it is being restarted. You can do that with the following commands:

      • First find the container ID:
      docker ps -a
      
      • Then check the logs:
      docker logs -f go_container_id
      

      Alternatively, you could use the docker-compose logs -f command to check the logs of all containers.

      Regards,
      Bobby

      • Oddly enough
        docker logs -f go_container_id
        produces no output.

        docker-compose logs -f
        

        does produce output, but it’s not very helpful to me.

        Attaching to go-web-app_1
        go-web-app_1 exited with code 0
        

        Not sure why this would happen when I’m able to run the go-web-app just using plain old Docker run.

        • Hi there,

          It looks like that the container is exiting with code 0 which means success. Have you tried adding the -d flag when running the docker-compose up command? This should keep the container running.

          Regards,
          Bobby

          • I do run the container in detached mode. It’s strange that the container is building successfully yet it’s stuck restarting.

            There must be something obvious that I’m just missing in all of this. Regardless, I felt that I was spending too much time on this so I decided to just go ahead and set up Nginx on my server, forgoing the Nginx Docker approach entirely. With this approach, I can still deploy my Go app in Docker so I’m happy. Plus, it took a short amount of time to setup.

            Thanks for taking the time to work through this with me, Bobby!