I'm new to docker.
I'm trying to implement RESTfull api in go using echo server. My code works fine when I run main.go but i cant run it using docker.
this is my echo server:
r := router.Router()
r.Logger.Fatal(r.Start("localhost:8080"))
and this is my Dockerfile:
FROM golang:latest AS build
ENV GO111MODULE=on \
CGO_ENABLED=1
#Maintainer info
LABEL maintainer="Saman Hoseini"
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o main .
#this step is for CGO libraries
RUN ldd main | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;'
RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/
#Second stage of build
FROM alpine:latest
RUN apk update && apk --no-cache add ca-certificates \
sqlite
COPY --from=build /app ./
EXPOSE 8080
ENTRYPOINT ["./main"]
After build when i run the container i face curl failure:
$ docker run -d -p 8080:8080 my-docker
$ curl http://localhost:8080
curl: (56) Recv failure: Connection reset by peer
how can i fix this problem ?
You need to run your application on the external port of your container.
r := router.Router()
r.Logger.Fatal(r.Start(":8080"))
This happens because the EXPOSE 8080 command forwards the application port open on the external port of the container (not internal, like localhost).
After this, command docker run -d -p 8080:8080 my-docker, more precisely a parameter -p, forwards external port from your container to external port on your machine.
Related
I have been trying for almost 3 weeks to build and run a meteor app (bundle) using Docker, I have tried all the majors recommendations in Meteor forums, Stackoverflow and official documentarion with no success, first I tried to make the bundle and put inside the docker but have awful results, then I realized what I need to do is to make the bundle inside the docker and use a multistage Dockerfile, here is the one I am using right now:
FROM chneau/meteor:alpine as meteor
USER 0
RUN mkdir -p /build /app
RUN chown 1000 -R /build /app
WORKDIR /app
COPY --chown=1000:1000 ./app .
COPY --chown=1000:1000 ./app/packages.json ./packages/
RUN rm -rf node_modules/*
RUN rm -rf .meteor/local/*
USER 1000
RUN meteor update --packages-only
RUN meteor npm ci
RUN meteor build --architecture=os.linux.x86_64 --directory /build
FROM node:lts-alpine as mid
USER 0
RUN apk add --no-cache python make g++
RUN mkdir -p /app
RUN chown 1000 -R /app
WORKDIR /app
COPY --chown=1000:1000 --from=meteor /build/bundle .
USER 1000
WORKDIR /app/programs/server
RUN rm -rf node_modules
RUN rm -f package-lock.json
RUN npm i
FROM node:lts-alpine
USER 0
ENV TZ=America/Santiago
RUN apk add -U --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir -p /app
RUN chown 1000 -R /app
WORKDIR /app
COPY --chown=1000:1000 --from=mid /app .
USER 1000
ENV LC_ALL=C.UTF-8
ENV ROOT_URL=http://localhost/
ENV MONGO_URL=mongodb://locahost:21027/meteor
ENV PORT=3000
EXPOSE 3000
ENTRYPOINT [ "/usr/local/bin/node", "/app/main.js" ]
if I build with docker build -t my-image:v1 . then run my app with docker run -d --env-file .dockerenv --publish 0.0.0.0:3000:3000 --name my-bundle my-image:v1 it exposes port:3000 but when I try to navigate with my browser to http://127.0.0.1:3000 it redirects to https://localhost, if i do docker exec -u 0 -it my-bundle sh, then apk add curl, then curl 127.0.0.1:3000 I can see the meteor app running inside docker.
Has anyone had this issue before, maybe I am missing some configuration? my bundle also works fine outside docker with node main.js in the bundle folder and can visit http://127.0.0.1:3000 with my browser.
Thanks in advance
I have a web app in python3. I'am trying to access the web app from another Machine on my network (cloud environment). The web app works.
The Dockerfile is as follows:
FROM python:3.8.0-slim as builder
RUN apt-get update \
&& apt-get install gcc -y \
&& apt-get clean
COPY app/requirements.txt /app/requirements.txt
WORKDIR app
RUN pip install --upgrade pip && pip3 install --user -r requirements.txt
COPY ./app /app
# Production image
FROM python:3.8.0-slim as app
COPY --from=builder /root/.local /root/.local
COPY --from=builder /app /app
WORKDIR app
ENV PATH=/root/.local/bin:$PATH
EXPOSE 5000
ENTRYPOINT gunicorn main:app -b 0.0.0.0:5000
$docker build -t simu .
$docker run --name simu --detach -p 5000:5000 -it simu:v1
11a8e9e2f7f2d70d39cac2b3e2a14c25ae2bf9536db005dee1d473aa588139ae
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11a8e9e2f7f2 simu:v1 "/bin/sh -c 'gunicor…" 3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp simu
$curl -X POST {} http://localhost:5000
curl: (3) <url> malformed
curl: (56) Recv failure: Connection reset by peer
Why I receive that error?
UPDATE
If I run the container as
/usr/bin/docker run --name simu --detach --network host -e REGISTRAR_URL=http://localhost:8500 -it simu:v1
The service is accessible from the same machine:
curl -X POST {} http://localhost:5000
curl: (3) <url> malformed
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
But it is not from another machine on the same LAN:
$curl -X POST http://algo:5000/generate-prod
curl: (7) Failed connect to algo:5000; Connection refused
I have stopped firewalld also.
If I run the application without the container everything works, so I think there must be something in the neworking with docker which is not set correctly.
You expose the port but you do not publish the port. Check this post on the difference. You need to add -p 5000:5000 to the docker run command.
I have a multi stage Dockerfile
# Base Build
FROM alpine:3.7 AS base
RUN apk add --no-cache nodejs
WORKDIR /root/app
COPY . .
ARG TARGET_ENV
COPY .env.$TARGET_ENV .env
RUN rm .env.*
RUN npm set progress=false && npm config set depth 0
RUN npm install --only=production
RUN cp -R node_modules prod_node_modules
RUN npm install
RUN npm run build
# Prod Build
FROM base AS release
COPY --from=base /root/app/prod_node_modules ./node_modules
COPY --from=base /root/app/package.json .
COPY --from=base /root/app/package-lock.json .
COPY --from=base /root/app/dist .
CMD npm start
EXPOSE 3000
I'd like to build my container and then run it locally.
It builds just fine but when I run it a hash is output, but the container is not running.
docker build --build-arg TARGET_ENV=local -t express-app .
docker run -d -p 3000:3000 -it express-app
Your container could be crashing on start.
Check the output of $ docker run -p 3000:3000 -it express-app for error messages.
I am building my Go application. I am getting error like below
# cd .; git clone https://github.com/julienschmidt/httprouter /go/src/github.com/julienschmidt/httprouter Cloning into
'/go/src/github.com/julienschmidt/httprouter'... fatal: unable to
access 'https://github.com/julienschmidt/httprouter/': Could not
resolve host: github.com package github.com/julienschmidt/httprouter:
exit status 128
The command '/bin/sh -c go get ./' returned a non-zero code: 1
docker file.
# install dependencies first
RUN go get ./
RUN go build
CMD if [ ${APP_ENV} = production ]; \
then \
main; \
else \
go get github.com/pilu/fresh && \
fresh; \
fi
EXPOSE 8080
build command:
docker image build -t test:1.0 .
Thanks. Any help please?
Check DNS server
Check internet connection
You can clone the repository with a script in a folder, and then copy the folder to the docker and build the project. Example
FROM golang:1.9 as builder
RUN mkdir -p /go/src/folder
WORKDIR /go/src/folder
COPY src/ .
RUN go get -d
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .
FROM alpine:latest
WORKDIR /
COPY --from=builder /go/src/folder/app .
CMD ["/app"]
I have a simple webproject which I want to "Dockerize" but I keep failing at exposing the webapp to host.
My Dockerfile looks like:
FROM debian:jessie
RUN apt-get update -y && \
apt-get install -y python-pip python-dev curl && \
pip install --upgrade pip setuptools
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app/web
And requirements.txt looks like:
PasteScript==2.0.2
Pylons==1.0.2
The web directory was built using:
paster create --template=pylons web
And finally start_server.sh:
#!/bin/bash
paster serve --daemon development.ini start
Now I am able to build with :
docker build -t webapp .
And then run command:
docker run -it -p 5000:5000 --name app webapp:latest /bin/bash
And then inside the docker container I run:
bash start_server.sh
which successfully starts the webapp on port 5000 and if I curl inside docker container I get expected response. Also the container is up and running with the correct port mappings:
bc6511d584ae webapp:latest "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:5000->5000/tcp app
Now if I run docker port app it looks fine:
5000/tcp -> 0.0.0.0:5000
However I cannot get any response from server from host with :
curl localhost:5000
I have probably misunderstood something here but it seems fine to me.
In your dockerfile you need to add EXPOSE 5000 your port mapping is correct think of it as opening the port on your container and then you map it with localhost with the -p
Answer in the comment
when you make_server bind to 0.0.0.0 instead of localhost