I am new to docker so sorry in advance if this question is going to be stupid or something.
I have a docker-compose.yaml which looks like this:
version: "3.5"
services:
platform:
image: memgraph/memgraph-platform:2.0.0
container_name: memgraph_container
restart: unless-stopped
ports:
- "7687:7687"
my_app:
build:
context: .
dockerfile: Dockerfile
container_name: something
restart: unless-stopped
command: python main.py
depends_on:
- platform
environment:
HOST: platform
PORT: 7687
My code which is responsible to connect to the database is looking like this:
host = os.getenv("HOST")
port = os.getenv("PORT")
conn = mgclient.connect(host=host, port=port)
What those 2 are the commands that I am running
docker-compose up -d
docker-compose exect my_app
So far as I understand this is what it should be done in order for the service my_app to be able to connect to the memgraph database. But something is wrong and I don't get it what.
Here are the logs run by docker-compose logs:
something | Traceback (most recent call last):
something | File "/MemgraphProject/DataBase.py", line 12, in __init__
something | self._connection: mgclient.Connection = mgclient.connect(host=host, port=port)
something | mgclient.OperationalError: couldn't connect to host: Connection refused
My whole component is stuck in a Restarting State for some reason and is not getting out of there:
memgraph_container /bin/sh -c /usr/bin/superv ... Restarting
something python main.py Restarting
Sometimes if I keep running docker-compose ps I will get memgraph_container State as Up, but is not going to keep that state for long.
#########################################################################################
On the end of the day all what I want to do is to run successfuly my application with docker-compose exec my_app -d but is not working.
If I will run docker-compose run my_app to initiate a new container I will get the error message regarding the connection to the database.
But if I will run docker-compose exec my_app -d I will receive this error message cause of the Restarting State
Error response from daemon: Container 6fd406ab0b4249424aabd3674aa4572380ac9ddf4f81d756d075bb692c768303 is restarting, wait until the container is running
#########################################################################################
Dose any of you what I am doing wrong and how can I fix this?
Related
I created an API using node.js
I converted it to an image by running the command docker build -t droneapi:1.0
using this Dockerfile.
FROM node:19-alpine
ENV MONGO_DB_USERNAME = admin \
MONGO_DB_PWD=password
RUN mkdir -p /home/droneAPI
COPY . /Users/styles/Programming/DroneAPI2
CMD ["node", "/Users/styles/Programming/DroneAPI2/Drones/server.js"]
I ran docker run droneapi:1.0 to create a container to talk to my mongodb container but I received the error: getaddrinfo ENOTFOUND mongodb
I’m using mongoose to try and communicate with the db
onst connectDB = async () => {
try{
const conn = await mongoose.connect("mongodb://admin:password#mongodb:27017", {dbName: 'drobedb'})
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline)
}catch (error){
console.log(`Error: ${error.message}`.red.underline.bold)
process.exit(1)
}
}
I have tried to replace the 'mongodb' in the connection string with localhost and I receive Error: connect ECONNREFUSED 127.0.0.1:27017
Here is my mongo.yaml file
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
I'm new to docker so please any assistance will be appreciated
When we start a container stack through a compose-file, a network for this compose stack is created for us, and all containers are attached to this network. When we start a container through docker run ..., it is attached to the default network. Containers in different networks cannot communicate with each other. My recommendation would be to add the dronapi-container to the compose file:
version: '3'
services:
...
drone-api:
bulid:
context: .
dockerfile: path/to/Dockerfile
...
depends_on:
- mongodb # to start this container after mongodb has been started
If we want to start the stack, we can run docker compose up -d. Notice that if the image was built before, it will not be automatically rebuilt. To rebuild the image, we can run docker compose up --build -d.
As an aside: I would recommend following the 12 factors for cloud-native applications (12factors.net). In particular, I would recommend to externalize the database configuration (12factors.net).
I have 3 services: webapp (app), database (db), and redis (rd) that share the same network.
Randomly, both connections to the services (database and redis) hangs about 5 seconds.
It's not only when I run my webserver that connects to the database, but even when I browse a link on a web page. At some point it freezes and need to wait for about 5 seconds.
I'm not sure but it looks like the connection is automatically closed at some point and need to be re-established.
I don't know I'm supposed to debug this:
Running telnet db 5432 or telnet rd 5432 in my app container will always work instantly.
Running docker ps -a in my host machine display this:
Here is my docker-compose file:
version: "3"
services:
app:
depends_on:
- db
- rd
build:
context: ..
dockerfile: .devcontainer/service_app/Dockerfile
volumes:
- ..:/workspace:delegated
# Avoid having the container shut down if the default container command fails or exits
command: /bin/sh -c "while sleep 1000; do :; done"
db:
env_file: service_db/.env.local
build:
context: ..
dockerfile: .devcontainer/service_db/Dockerfile
volumes:
- "pgdata:/var/lib/postgresql/data"
rd:
image: redis:7
volumes:
- "rddata:/data"
volumes:
pgdata:
rddata:
I don't understand what could cause this. Thanks.
Try to replace your sleep loop with tail -f /dev/null
I am trying to get a simple docker-compose file working on windows.
version: "2"
volumes:
db_data: {}
services:
db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: test123
volumes:
- db_data:/var/lib/mysql/data
I need to persist the db data. I've created the directory db_data and I found this solution from a github issue: https://github.com/docker-library/mysql/issues/69. I had previously been using mysql 5.6. I'm simply running
docker-compose up -d
when I check
docker ps
I do not get any running processes. Any help with this would be greatly appreciated. I've added the output from running the command below:
PS D:\test-exercise> docker-compose up -d
Starting test-exercise_db_1 ... done
I'm having an issue trying to dockerize my webdriverIO testing framework and running the tests on the existing selenium/standalone-chrome image with docker-compose.
I have a simple dockerfile to build the app such as this:
FROM node:12
ADD . /app
WORKDIR /app
RUN npm install
Then I have a docker-compose.yaml to build the stack and run the tests:
version: "3"
services:
app:
build: .
command: npm test -- --host selenium
links:
- selenium
selenium:
network_mode: host
image: selenium/standalone-chrome
volumes:
- /dev/shm:/dev/shm
ports:
- "4444:4444"
My conf.js file sets the host and port like so:
exports.config = {
host: 'selenium',
port: 4444,
No matter what I try, I continue to get the following error after docker-compose build and docker-compose up:
[0-9] Error: Failed to create session.
app_1 | Unable to connect to "http://127.0.0.1:4444/", make sure browser driver is running on that address.
app_1 | If you use services like chromedriver see initialiseServices logs above or in wdio.log file as the service might had problems to start the driver.
Any helpful information on why I would see this type of error would be helpful. Thanks in advance.
I'm trying to dockerize an existing Rails app that uses Postgresql 9.5 as its database. In my docker-compose.yml. After a successful "docker-compose build" I can run the "docker-compose up" command and see the connection but when I navigate to localhost I get the following error.
PG::ConnectionBad
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"?
Here is what is in my docker-compose.yml
version: '2'
services:
db:
image: postgres:9.5
restart: always
volumes:
- ./tmp/db:/var/lib/postgresql/9.5/main
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: hardware_development
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
From what I've seen I need to do some specification somewhere in my dockerfile or the docker-compose.yml but I either don't see a change or end up back at the same error.
I've been able to use Docker's own docs to use Docker Compose to create a new rails app with postgres where I see the "yay you're on rails!" page but now with my own code I can't see anything. Running the app outside of docker shows me the test page as well so its not the code within my rails app or the Postgres evnironment outside of Docker.
Your db docker-compose entry isn't exposing any ports. It needs to expose 5432. Add a ports line for that just like you have for web.
Edit: also I don't know why you added restart: always to your database container, but I wouldn't recommend that for rails or pretty much anything.