0

This is my Dockerfile

# Start by selecting the base image for our service
FROM golang:alpine

# Specifying app as our work directory in which
# futher instructions should run into
WORKDIR /go/src/github.com/urlshortener

# Copy everything from project into filesystem of container
COPY . . 

# Download all neededed project dependencies
RUN go mod download

# Build the project executable binary
RUN go build -o main .

# Run/Starts the app executable binary
CMD ["./main"]

This is my directory structure

go.mod
go.sum
Dockerfile
README.md
docker-compose.yml
redis.conf
handlers
 |_ handlers.go
shortener
 |_ shorteners.go
 |_ shorteners_test.go
storage
 |_ storage_service.go
 |_ storage_service_test.go

I have this in my docker-compose.yml:

version: "3.8"
services:
  redis:
    container_name: "redis"
    image: redis:alpine
     # Specify the redis.conf file to use and add a password.
    command: redis-server /usr/local/etc/redis/redis.conf --requirepass mypassword
    ports: 
      - "6379:6379"
    # map the volumes to that redis has the custom conf file from this project.
    volumes: 
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf

  go:
    container_name: "redisapi"
    build:
      # build the image using the Dockerfile we have in this project. Can use an image instead.
      context: .
    ports:
      - "8080:8080"

When I run docker-compose up -d and then docker-compose ps
I can see one of the containers is in exit 2 state

urlshortener git:(main) ✗ docker-compose ps   
  Name                Command               State            Ports         
---------------------------------------------------------------------------
redis      docker-entrypoint.sh redis ...   Up       0.0.0.0:6379->6379/tcp
redisapi   ./main                           Exit 2      

Any advice/tips for debugging would be appreciated.

  • Option 1: You can check the container log, easiest way is via docker desktop on your local machine. Option 2: Try creating the binary in the same way as in docker file and verify if the binary runs for you. Something that I am finding weird is, in your directory structure, where would your main function be? – Nasir Rabbani Oct 17 '22 at 08:27
  • As you appear to be working from a command prompt run [docker compose logs](https://docs.docker.com/engine/reference/commandline/compose_logs/) to view container logs. Another option is to start a [shell in the container](https://stackoverflow.com/q/36249744/11810946). – Brits Oct 17 '22 at 18:33

0 Answers0