Gitlab-ci wit symfony : php_network_getaddresses: getaddrinfo failed: Name or service not known - docker-compose

I created gitlab-ci.yml with docker and symfony for the first time. I learn.
I don't undertand whay it didn't work. I tried to change the name of the database. perhaps I forgot some things. I'm losing.
I need you for help to me.
Thanks you a lot
This is error
$ php bin/console doctrine:database:drop --if-exists --force -e test
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
In PDOConnection.php line 27:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
This is gitlab-ci file
default:
image: php:7.2-fpm
services:
- mysql:latest
cache:
paths:
- vendor/
variables:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: sf4-reims
stages: # Ici on déclare toutes nos étapes
- build
- test
job:build:
stage: build
script:
- apt-get update
- curl -sSk https://getcomposer.org/installer | php -- --disable-tls &&
- mv composer.phar /usr/local/bin/composer
- composer install --prefer-dist
- echo $CI_JOB_STAGE
job:test:
stage: test
script:
- apt-get update
- apt-get install -y git zip zip unzip
- docker-php-ext-install pdo pdo_mysql
- cp .env.test .env
- php bin/console doctrine:database:drop --if-exists --force -e test
- bin/console doctrine:database:create --if-not-exists -e test
- bin/console doctrine:schema:drop --force --no-interaction -e test
- bin/console doctrine:schema:create --no-interaction -e test
- bin/console doctrine:fixtures:load --no-interaction --purge-with-truncate -vvv -e test
- echo $CI_JOB_STAGE
This is file .env
APP_ENV=test
DATABASE_URL=mysql://root:root#database:3306/sf4-reims
This is file docker-compose.yml
version: '3'
services:
apache:
container_name: ${CONTAINER_NAME}_apache
image: httpd:2.4.41
ports:
- ${APACHE_PORT}:80
- ${APACHE_PORT_SSL}:443
volumes:
- ./${DIRECTORY_PROJECT}:/var/www/html
- .docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
- .docker/httpd/logs:/var/log/httpd
depends_on:
- php
networks:
- front-network
- back-network
database:
image: mysql
command: "--default-authentication-plugin=mysql_native_password"
container_name: ${CONTAINER_NAME}_database
volumes:
- .docker/data/db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
- back-network
php:
build: .docker/php
container_name: ${CONTAINER_NAME}_php
volumes:
- ./${DIRECTORY_PROJECT}:/var/www/html
- .docker/php/php.ini:/usr/local/etc/php/php.ini
environment:
- maildev_host=${CONTAINER_NAME}_maildev
depends_on:
# - maildev
- database
networks:
- back-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${CONTAINER_NAME}_phpmyadmin
environment:
PMA_HOST: database
PMA_PORT: ${MYSQL_PORT}
ports:
- ${PHPMYADMIN_PORT}:80
links:
- database
networks:
- back-network

I know this was posted 9 months ago, but I believe I've just encountered the same problem, but could get it working by adding alias to the mysql service in the gitlab-ci.yml.
I can see you "index" your mysql container database in the docker-compose.yml, so I believe your application references the mysql host as database in your application configuration (config/packages/doctrine.yaml doctrine.dbal.host). In this case, you should write
default:
image: php:7.2-fpm
services:
- name: mysql:latest
alias: database
instead of
default:
image: php:7.2-fpm
services:
- mysql:latest
in your gitlab-ci.yml. In case you use another name in your host settings, then you should use that as an alias.

Related

fastapi & psycopg2 : password authentication failed for user "root"

I am getting this issue on
`docker-compose up`
This is my configuration files:-
Dockerfile:
FROM python:3.9
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "uvicorn", "app.main:app","--host","0.0.0.0","--port","8000 " ]
docker-compose.yaml:
version: '3'
services:
api:
build: .
depends_on:
- postgres
ports:
- 8000:8000
volumes:
- ./:/usr/src/app:ro
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
# env_file:
# - ./.env
environment:
- DATABASE_HOST_NAME=postgres
- DATABASE_PORT=${DATABASE_PORT}
- DATABASE_NAME=${DATABASE_NAME}
- DATABASE_USERNAME=${DATABASE_USERNAME}
- DATABASE_PASSWORD=${DATABASE_PASSWORD}
- SECRET_KEY=${SECRET_KEY}
- ALGORITHM=${ALGORITHM}
- ACCESS_TOKEN_EXPIRE_MINUTES=${ACCESS_TOKEN_EXPIRE_MINUTES}
postgres:
image: postgres
environment:
- POSTGRES_USER=${DATABASE_USERNAME}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_NAME}
volumes:
- postgres-db:/var/lib/postgresql/data
volumes:
postgres-db:
When i ran docker-compose build and docker-compose up -d these two commands ran succesfully but when i hit the login endpoint, it's giving the error
fastapi-api-1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "postgres" (192.168.112.2), port 5432 failed: FATAL: password authentication failed for user "root"
My DATABASE_USERNAME is root

ERROR: for rca-org2 Cannot start service rca-org2: OCI runtime create failed: container_linux.go:345:

ERROR: for rca-org2 Cannot start service rca-org2: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
ERROR: Encountered errors while bringing up the project.
I am getting this error when i try to run docker-compose file which looks like this :-
rca-org2:
container_name: rca-org2
image: hyperledger/fabric-ca:latest
command: /bin/bash -c 'fabric-ca-server start -d -b rca-org2-admin:rca-org2-adminpw --port 7055'
environment:
- FABRIC_CA_SERVER_HOME=/tmp/hyperledger/fabric-ca/crypto
- FABRIC_CA_SERVER_TLS_ENABLED=true
- FABRIC_CA_SERVER_CSR_CN=rca-org2
- FABRIC_CA_SERVER_CSR_HOSTS=0.0.0.0
- FABRIC_CA_SERVER_DEBUG=true
volumes:
- /tmp/hyperledger/org2/ca:/tmp/hyperledger/fabric-ca
networks:
- fabric-ca
ports:
- 7055:7055
-
After Research I found that the container rca-org2 doesn't have bash installed but it has sh so running the container with (replace /bin/bash with sh) worked for me. so now the new compose file looks like this.
rca-org2:
container_name: rca-org2
image: hyperledger/fabric-ca:latest
command: sh -c 'fabric-ca-server start -d -b rca-org2-admin:rca-org2-adminpw --port 7055'
environment:
- FABRIC_CA_SERVER_HOME=/tmp/hyperledger/fabric-ca/crypto
- FABRIC_CA_SERVER_TLS_ENABLED=true
- FABRIC_CA_SERVER_CSR_CN=rca-org2
- FABRIC_CA_SERVER_CSR_HOSTS=0.0.0.0
- FABRIC_CA_SERVER_DEBUG=true
volumes:
- /tmp/hyperledger/org2/ca:/tmp/hyperledger/fabric-ca
networks:
- fabric-ca
ports:
- 7055:7055

docker compose how to mount entrypoint and then use it

I have a service in the docker compose as follows.
nginx:
image: nginx:1.18.0-alpine
ports:
- 8000:80
volumes:
- ./nginx/localhost/conf.d:/etc/nginx/conf.d
- ./entrypoint.sh:/entrypoint.sh
entrypoint: ./entrypoint.sh
depends_on:
- webapp
networks:
- nginx_network
I get the error
Cannot start service nginx: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/entrypoint.sh": permission denied: unknown
ERROR: for nginx Cannot start service nginx: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/entrypoint.sh": permission denied: unknown
ERROR: Encountered errors while bringing up the project.
How can i do this
I have to use ["/bin/sh","/entrypoint.sh"]
nginx:
image: nginx:1.18.0-alpine
ports:
- 8000:80
volumes:
- ./nginx/localhost/conf.d:/etc/nginx/conf.d
- ./entrypoint.sh:/entrypoint.sh
entrypoint: ["/bin/sh","/entrypoint.sh"]
depends_on:
- webapp
networks:
- nginx_network
I don't have enough information about your entrypoint script, config file and the web app minimal information.
Sometimes when you create a file on your host machine it lacks some permissions to be executed by some users in your docker container, so Iwould think about checking the permissions for that file by running:
ls -l
that will give you all permissions about that file, and maybe that's why it's telling you permission is denied try adding execution permission running chmod +x ./entrypoint.sh.
I usually do that using a dockerfile when working on a script from my context.
dockerfile
FROM nginx:1.18.0-alpine
COPY /entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT [ "./entrypoint.sh" ]:
docker-compose.yaml
version: "3.8"
services:
nginx:
build:
context: ./
ports:
- 8000:80
volumes:
- ./nginx/localhost/conf.d:/etc/nginx/conf.d
depends_on:
- webapp
networks:
- nginx_network
Then try to run:
docker-compose up --build
make sure both files are in the same directory.

Why is my flask server unable to speak to the postgres database using docker-compose?

I have posted the relevant files below. Everything builds as expected, however when trying to use SQLAlchemy to make a call to the database, I invariably get the following error:
OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: Name or service not known
The string that sqlalchemy is using is (as given in .env.web.dev): postgres://postgres:postgres#db:5432/spaceofmotion.
What am I doing wrong?
docker-compose.yml:
version: '3'
services:
db:
container_name: db
ports:
- '5432:5432'
expose:
- '5432'
build:
context: ./
dockerfile: Dockerfile.postgres
networks:
- db_web
web:
container_name: web
restart: always
build:
context: ../
dockerfile: Dockerfile.web
ports:
- '5000:5000'
env_file:
- ./.env.web.dev
networks:
- db_web
depends_on:
- db
- redis
- celery
redis:
image: 'redis:5.0.7-buster'
container_name: redis
command: redis-server
ports:
- '6379:6379'
celery:
container_name: celery
build:
context: ../
dockerfile: Dockerfile.celery
env_file:
- ./.env.celery.dev
command: celery worker -A a.celery --loglevel=info
depends_on:
- redis
client:
container_name: react-app
build:
context: ../a/client
dockerfile: Dockerfile.client
volumes:
- '../a/client:/src/app'
- '/src/app/node_modules'
ports:
- '3000:3000'
depends_on:
- "web"
environment:
- NODE_ENV=development
- HOST_URL=http://localhost:5000
networks:
db_web:
driver: bridge
Dockerfile.postgres:
FROM postgres:latest
ENV POSTGRES_DB spaceofmotion
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY ./spaceofmotion-db.sql /
COPY ./docker-entrypoint-initdb.d/restore-database.sh /docker-entrypoint-initdb.d/
restore-database.sh:
file="/spaceofmotion-db.sql"
psql -U postgres spaceofmotion < "$file"
Dockerfile.web:
FROM python:3.7-slim-buster
RUN apt-get update
RUN apt-get -y install python-pip libpq-dev python-dev && \
pip install --upgrade pip && \
pip install psycopg2
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "manage.py", "runserver"]
.env.web.dev:
DATABASE_URL=postgres://postgres:postgres#db:5432/spaceofmotion
... <other config vars> ...
Is this specifically coming from your celery container?
Your db container declares
networks:
- db_web
but the celery container has no such declaration; that means that it will be on the default network Compose creates for you. Since the two containers aren't on the same network they can't connect to each other.
There's nothing wrong with using the Compose-managed default network, especially for routine Web applications, and I'd suggest deleting all of the networks: blocks in the entire file. (You also don't need to specify container_name:, since Compose will come up with reasonable names on its own.)

docker-compose: run rails db:setup and rake tasks to init data

I can't find the way to execute the following commands from a docker-compose.yml file:
rails db:setup
rails db:init_data.
I tried to do that as follows and it failed:
version: '3'
services:
web:
build: .
links:
- database
- redis
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
env_file:
- .env/development/database
- .env/development/web
command: ["rails", "db:setup"]
command: ["rails", "db:init_data"]
redis:
image: redis
database:
image: postgres
env_file:
- .env/development/database
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Any idea on what's going wrong here ? Thank you.
The code source is on the GitHub.
You can do two things in my opinion:
Change command: to the following line, because two commands are not allowed in compose file:
command:
- /bin/bash
- -c
- |
rails db:setup
rails db:init_data
Use supervisord app: supervisord web page
The solution that worked for me was to remove CMD commad from Dockerfile because using command option in docker-compose.yml would have overridden CMD command.
So, Docker file will look like that:
FROM ruby:2.5.1
LABEL maintainer="DECATHLON"
RUN apt-get update -yqq
RUN apt-get install -yqq --no-install-recommends nodejs
COPY Gemfile* /usr/src/app/
WORKDIR /usr/src/app
RUN bundle install
COPY . /usr/src/app/
Then add command option to docker-compose file:
version: '3'
services:
web:
build: .
links:
- database
- redis
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
env_file:
- .env/development/database
- .env/development/web
command:
- |
rails db:reset
rails db:init_data
rails s -p 3000 -b '0.0.0.0'
redis:
image: redis
database:
image: postgres
env_file:
- .env/development/database
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
If the above solution does not work for somebody, there is an alternative solution:
Create a shell script in the project route and name it entrypoint.sh, for example:
#!/bin/bash
set -e
bundle exec rails db:reset
bundle exec rails db:migrate
exec "$#"
Declare entrypoint option in dpcker-compose file:
v
version: '3'
services:
web:
build: .
entrypoint:
- /bin/sh
- ./entrypoint.sh
depends_on:
- database
- redis
ports:
- "3000:3000"
volumes:
- .:/usr/src/app
env_file:
- .env/development/database
- .env/development/web
command: ['./wait-for-it.sh', 'database:5432', '--', 'bundle', 'exec', 'rails', 's', '-p', '3000', '-b', '0.0.0.0']
database:
image: postgres:9.6
env_file:
- .env/development/database
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
I also user wait-for-it script to ensure the DB is started.
Hope this helps. I pushed the modifications to the Github repo. Sorry for some extra letters left in the text before code blocks, - for some unknown reasons, the code markdown didn't work, so I left them to get it working.