I am trying to run my restful api in docker but having issue with my golang executable it is always not found. Here is my Dockerfile
# Start from golang base image
FROM golang:1.15.2
#Set ENV
ENV DB_HOST=fullstack-mysql
DB_DRIVER=mysql
DB_USER=root
DB_PASSWORD=root
DB_NAME=link_aja
DB_PORT=3306
APP_NAME=golang-linkaja
CGO_ENABLED=0
# Copy the source from the current directory to the working Directory inside the container
COPY . /usr/src/${APP_NAME}
# Move to working directory
WORKDIR /usr/src/${APP_NAME}
#install depedencies
RUN go mod download
# Build the application
RUN go build -o ${APP_NAME}
# Expose port 3000 to the outside world
EXPOSE 3000
#Command to run the executable
CMD ${APP_NAME}
And here is my docker-compose.yml
version: '3'
services:
app:
container_name: golang-linkaja
build: .
ports:
- 3000:3000
restart: on-failure
volumes:
- api:/usr/src/${APP_NAME}
depends_on:
- fullstack-mysql
networks:
- fullstack
fullstack-mysql:
image: mysql:5.7
container_name: full_db_mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_HOST=${DB_HOST}
- MYSQL_USER=${DB_USER}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- database_mysql:/var/lib/mysql
networks:
- fullstack
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_container
depends_on:
- fullstack-mysql
environment:
- PMA_HOST=fullstack-mysql #DB_HOST env must be the same with this
- PMA_USER=${DB_USER}
- PMA_PORT=${DB_PORT}
- PMA_PASSWORD=${DB_PASSWORD}
ports:
- 9090:80
restart: always
networks:
- fullstack
volumes:
api:
database_mysql:
# Networks to be created to facilitate communication between containers
networks:
fullstack:
driver: bridge
Everything works correctly except for the Go app itself, here is the error that I get
golang-linkaja | /bin/sh: 1: golang-linkaja: not found
Could i get any help please? i’m new and still learning docker
Thanks in advance!
Update: here are the other things i’ve tried:
1.Changing CMD to CMD ["./usr/src/${APP_NAME}/${APP_NAME}"]
Return error golang-linkaja | sh: 1: /usr/src/golang-linkaja/golang-linkaja: not found
2.Changing to CMD [ "./golang-linkaja" ]
and CMD [ "./${APP_NAME}" ]
Return error ERROR: for golang-linkaja Cannot start service app: OCI runtime create failed: container_linux.go:370: starting container process caused: exec: "./golang-linkaja": stat ./golang-linkaja: no such file or directory: unknown
2
Answers
You need to remove the volume
- api:/usr/src/${APP_NAME}
from your compose. You have already copied what you need in yourDockerfile
. The volume(defined in compose) is overwriting all your data and hence your built binary is not found.Just remove the volume and try to rebuild and start the container again …. and change your cmd to
CMD [ "./${APP_NAME}" ]
In your Dockerfile, you try to run your executables without using
./
prefix, So OS search executable on system folders and can not find it. Add./
beginning of your CMD or use the absolute path of the executable.or