docker-compose port not exposing - docker

When i try to access my lambda endpoint from outside i receive this error:
curl -XPOST 127.0.0.1:3000/create-loan
Recv failure: Connection reset by peer
But inside docker the endpoint work, the port 3000 does not working from outside.
Any help?
Name Command State Ports
billing_db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
billing_lambda_1 /usr/local/bin/sam local s ... Up 0.0.0.0:3000->3000/tcp
docker-compose.yml
version: '3'
services:
lambda:
build: .
volumes:
- ./:/app
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- db
environment:
- PYTHONPATH=${PWD}/billing
ports:
- "3001:3000"
db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_USER=${BILLING_USER}
- POSTGRES_PASSWORD=${BILLING_PASSWORD}
- POSTGRES_DB=${BILLING_DB}
- POSTGRES_HOST=${BILLING_HOST}
volumes:
db-data:
driver: local
My DockerFile:
FROM python:3.7
RUN pip3 install aws-sam-cli
EXPOSE 3000
ENTRYPOINT ["/usr/local/bin/sam"]
RUN apt-get install curl
RUN pip3 install pipenv
WORKDIR /app
RUN pipenv install --dev
CMD ["local", "start-api"]
Solved
CMD ["local","start-api","--host","0.0.0.0"]

By the compose file you've exposed the port 3001
ports:
- "3001:3000"
But connecting to 3000
curl -XPOST 127.0.0.1:3000/create-loan

Related

Docker: Cannot connect to other container

docker-compose.yml
version: '3'
volumes:
wp-assets:
services:
mariadb:
build: ./requirements/mariadb
environment:
- MYSQL_ROOT_HOST=${MYSQL_ROOT_HOST}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "127.0.0.1:3306:3306"
- "127.0.0.1:9999:9999" # test
wordpress:
environment:
- WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX}
- WORDPRESS_AUTH_KEY=${WORDPRESS_AUTH_KEY}
- WORDPRESS_SECURE_AUTH_KEY=${WORDPRESS_SECURE_AUTH_KEY}
- WORDPRESS_LOGGED_IN_KEY=${WORDPRESS_LOGGED_IN_KEY}
- WORDPRESS_NONCE_KEY=${WORDPRESS_NONCE_KEY}
- WORDPRESS_AUTH_SALT=${WORDPRESS_AUTH_SALT}
- WORDPRESS_SECURE_AUTH_SALT=${WORDPRESS_SECURE_AUTH_SALT}
- WORDPRESS_LOGGED_IN_SALT=${WORDPRESS_LOGGED_IN_SALT}
- WORDPRESS_NONCE_SALT=${WORDPRESS_NONCE_SALT}
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/wordpress
ports:
# host_port == 127.0.0.1:9000, allow only localhost
- "127.0.0.1:9000:9000"
nginx:
# image: nginx:latest
depends_on:
- wordpress
volumes:
- wp-assets:/var/wp-assets
build: ./requirements/nginx
ports:
# host_port == 0.0.0.0:8080, allow all interfaces
- "8080:80"
mariadb/Dockerfile
FROM debian:buster
# install mariadb-server
RUN apt update && apt install -y mariadb-server
# allow connection from wordpress (host name)
RUN sed -e 's/127.0.0.1/wordpress/' \
-i '/etc/mysql/mariadb.conf.d/50-server.cnf'
# used for socket
RUN mkdir -p /var/run/mysqld && \
chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && \
chmod 777 /var/run/mysqld && \
touch /var/run/mysqld/mysqld.sock
# init db here
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
#ENTRYPOINT ["/docker-entrypoint.sh"]
ENTRYPOINT ["tail", "-f"]
I was trying to connect mariadb with wordpress (mariadb-client), and got an error :
Can't connect to MySQL server on 'mariadb'
So I tested ports are good. But while other ports like nginx:80 or wordpress:9000 can be accessed by the other containers, ports of mariadb refuses connections.
I couldn't figure out what is difference between mariadb container and the others. What's the problem?
The ENTRYPOINT of your MariaDB Dockerfile is
ENTRYPOINT ["tail", "-f"]
so it doesn't actually run MariaDB.
You probably need to comment that out and comment back in
ENTRYPOINT ["/docker-entrypoint.sh"]
I was confused about ports. I thought that each containers could communicate by just opening ports, but there was no LISTENing ports. Docker-compose ports: just binds ports and has nothing to do with LISTEN.

'Could not find rake-13.0.3 in any of the sources (Bundler::GemNotFound)' while creating my api service

docker-compose.yml
version: "3.7"
services:
courseshine_redis:
container_name: courseshine_redis
image: redis:latest
command: redis-server --requirepass ${POSTGRES_PASSWORD}
restart: always
env_file: .env
stdin_open: true
ports:
- ${REDIS_PORT}:${REDIS_PORT}
volumes:
- courseshine_redis_data:/data
networks:
- internal
courseshine_db:
container_name: courseshine_db
build:
context: ../..
dockerfile: courseshine_docker/development/courseshine_db/Dockerfile
restart: always
env_file: .env
environment:
- POSTGRES_MULTIPLE_DATABASES=${POSTGRES_DEV_DB},${POSTGRES_TEST_DB}
ports:
- ${COURSESHINE_DB_PORT}:${COURSESHINE_DB_PORT}
volumes:
- courseshine_postgres_data:/var/lib/postgresql/data
- ./courseshine_db:/dockerfile-entrypoint-initdb.d
networks:
- internal
courseshine_pgadmin:
container_name: courseshine_pgadmin
image: dpage/pgadmin4:4.21
restart: unless-stopped
env_file: .env
environment:
- PGADMIN_DEFAULT_EMAIL=${POSTGRES_USER}
- PGADMIN_DEFAULT_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- pgadmin:/var/lib/pgadmin
- courseshine_postgres_data:/var/lib/postgresql/data
depends_on:
- courseshine_db
networks:
- internal
courseshine_api: &api_base
container_name: courseshine_api
build:
context: ../..
dockerfile: courseshine_docker/development/courseshine_api/Dockerfile
env_file: .env
stdin_open: true
volumes:
- ../../courseshine_api:/var/www/courseshine/courseshine_api
- /var/run/docker.sock:/var/run/docker.sock
- bundle_cache:/usr/local/bundle
depends_on:
- courseshine_redis
- courseshine_db
networks:
- internal
courseshine_ui:
container_name: courseshine_ui
build:
context: ../../
dockerfile: courseshine_docker/development/courseshine_ui/Dockerfile
env_file: .env
stdin_open: true
volumes:
- ../../courseshine_ui:/var/www/courseshine_ui
depends_on:
- courseshine_api
networks:
- internal
networks:
internal:
volumes:
courseshine_redis_data:
courseshine_postgres_data:
pgadmin:
bundle_cache:
my docerfile for courseshine_api service
FROM ruby:2.7.1-slim-buster
RUN apt-get update -qq && apt-get install -y build-essential nodejs libpq-dev postgresql-client && rm -rf /var/lib/apt/lists/*
ENV APP_HOME /var/www/courseshine/courseshine_api
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY ./courseshine_api/Gemfile $APP_HOME/Gemfile
COPY ./courseshine_api/Gemfile.lock $APP_HOME/Gemfile.lock
RUN bundle install --path vendor/cache
# Copy the main application.
COPY ./courseshine_api $APP_HOME
# Add a script to be executed every time the container starts.
COPY ./courseshine_docker/development/courseshine_api/entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000
# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["rails","server","-b","0.0.0.0"]
entrypoint.sh
set -e
rm -f $APP_HOME/tmp/pids/server.pid
exec "$#"
when i hit docker-compose up, the courseshine_api service is not stand and throw Could not find rake-13.0.3 in any of the sources (Bundler::GemNotFound). Why this problem occur and how to fix this ..

Unable to start and connect 3 docker services - Redis, Python 3.7 Slim (Running DRF) and Celery

I am trying to containerise my application which is developed using technologise like DRF, Celery and Redis (as a broker).
I want to prepare docker-compose which will start all the three services (DRF, Celery and Redis (as a broker).
I also want to prepare the Dockerfile.prod for deloyment.
Here is what I have done so far -
version: "3"
services:
redis:
container_name: Redis-Container
image: "redis:latest"
ports:
- "6379:6379"
expose:
- "6379"
command: "redis-server"
dropoff-backend:
container_name: Dropoff-Backend
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/logistics_backend
ports:
- "8080:8080"
expose:
- "8080"
restart: always
command: "python manage.py runserver 0.0.0.0:8080"
links:
- redis
depends_on:
- redis
# - celery
celery:
container_name: celery-container
build: .
command: "celery -A logistics_project worker -l INFO"
volumes:
- .:/code
links:
- redis
Dockerfile(Not for deployment)
FROM python:3.7-slim
# FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN apt-get update &&\
apt-get install python3-dev default-libmysqlclient-dev gcc -y &&\
mkdir /logistics_backend
WORKDIR /logistics_backend
COPY ./requirements.txt /requirements.txt
COPY . /logistics_backend
EXPOSE 80
RUN pip install -r /requirements.txt
RUN pip install -U "celery[redis]"
RUN python manage.py makemigrations &&\
python manage.py migrate
RUN python manage.py loaddata roles businesses route_status route_type order_status service_city payment_status
CMD [ "python", "manage.py", "runserver", "0.0.0.0:80"]
The problem with the existing docker-compose is it returns the error as stated below -
celery-container | [2020-10-08 16:59:25,843: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**#127.0.0.1:5672//: [Errno 111] Connection refused.
celery-container | Trying again in 32.00 seconds... (16/100)
In Setting.py I have defined this for radis connection
REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
I don't know how should I extend my Dockerfile which is current used for the development, to form a Dockerfile.prod which could be deployable.
All of my three containers are working -
You need to change your REDIS_HOST in your Setting.py to be “redis” instead of “localhost”.

Docker Error: Cannot cerate container for service <container name>: status code not OK but 500

I used docker-compose to create docker containers in my React, Node.js, and Postgres structured project.
After I created Dockerfile and docker-compose.yml, I did docker up 'docker-compose up --build.'
Then, I wasn't be able to create containers, and get an errors.
I get Error says:
Error 1
Error 2
Error 3
How can I fix it and successfully build containers?
Here is a docker-compose.yml file in './'
version: '3'
services:
server:
container_name: mylivingcity_server
build: ./server
expose:
- 3001
ports:
- 3001:3001
volumes:
- ./server/config:/usr/src/app/server/config
- ./server/controllers:/usr/src/app/server/controllers
- ./server/db:/usr/src/app/server/db
command: npm run start
postgres:
image: postgres:12
container_name: mylivingcity_postgres
ports:
- 5432:5432
volumes:
- ./postgres/data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=mylivingcity
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
frontend:
container_name: mylivingcity_frontend
build: ./frontend
expose:
- 3000
ports:
- 3000:3000
volumes:
- ./frontend/src:/usr/src/app/frontend/src
- ./frontend/public:/usr/src/app/frontend/public
command: npm start
stdin_open: true
Here is a Dockerfile in './frontend'
FROM node:12
# Create frontend directory
RUN mkdir -p /usr/src/app/frontend/
WORKDIR /usr/src/app/frontend/
# Install dependencies
COPY package*.json /usr/src/app/frontend/
RUN npm install
COPY . /usr/src/app/frontend/
CMD [ "npm" , "start" ]
Here is a Dockerfile in './server'
FROM node:12
# Create server directory
RUN mkdir -p /usr/src/app/server/
WORKDIR /usr/src/app/server/
# Install dependencies
COPY package*.json /usr/src/app/server/
RUN npm install
COPY . /usr/src/app/server/
CMD [ "npm" , "run" , "start" ]

Docker Postgres Ruby on Rails unable to connect

I am following this tutorial from docker Docker Rails and I have created a folder and added this code below in my docker file.
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
And my docker compose file code is:
version: '3'
services:
db:
image: postgres
volumes:
- .data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
I am following tutorial when I am running docker compose up I can just see this error:
Could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
What is wrong here I don't know how to inspect and detect error how to fix this.
You need environment variables within your web container so that it knows how to connect to the db container.
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=
volumes:
- ./data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
- PGHOST=db
- PGUSER=postgres
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Please go to your database.yml and add host set to db , then username and password and run the command again.

Resources