How to reach localhost services from docker container with IPv4? - docker

I have this docker-compose file where I have a couple of services:
version: "3.8"
services:
app:
container_name: mobile_app_api
env_file:
- .env.development
command: npm run watch:dev
build:
context: .
target: development
dockerfile: ./Dockerfile
restart: always
volumes:
- /usr/src/app/node_modules
- ./:/usr/src/app
depends_on:
- postgres
- redis
extra_hosts:
- host-machine:host-gateway
network_mode: host
redis:
image: "redis:alpine"
container_name: mobile_app_cache
postgres:
image: postgres
container_name: mobile_app_database
restart: "unless-stopped"
environment:
TZ: 'GMT'
PGTZ: 'GMT'
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mobile_app_database
volumes:
- /:/data/postgres
network_mode: host
Api is supposed to work on localhost:3333, but when I start this with docker-compose up --build in terminal I get this:
mobile_app_api | server running on http://[::1]:3333
And here is 2 things.
Why does it work on IPv6?
How can I change host and make it work on localhost?
PS. My computer is MacOS, but in settings I have turned off IPv6.
I was trying to use host.docker.internal (and gateway.docker.internal) in my compose file, but it didn't work:
extra_hosts:
- host.docker.internal:host-gateway

Related

Need to up my docker file in ubuntu "docker-compose up -d"

services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
nginx:
container_name: 'lh-nginx'
build: ./nginx
depends_on:
- php-fpm
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: 'lh-php'
image: docker.io/bitnami/php-fpm:8.0
user: '1000:1000'
build:
context: ./php-fpm
args:
- PHP_ENV= development
depends_on:
- postgres
- redis
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services: 'postgres'
getting this error
I think you are missing some ENV vars.
This is our docker-compose.yml for Postgres
version: '3.9'
services:
db:
image: postgres:latest
restart: "no"
container_name: db
volumes:
- ./database:/var/lib/postgresql/data
ports:
- "8002:5432"
environment:
POSTGRES_PASSWORD: verySecurePassword34058
POSTGRES_USER: root
POSTGRES_DB: myDatabase
networks:
default:
external: true
name: our-network
Other parts of the application, (like Redis, the NodeJS App, etc) are in other docker-compose.yml files, But since they share the same network, they talk to each other.
You have not mentioned version in docker-composer.yml
version: '2'
services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
You should include your docker and docker-compose version in the querstion to help us answer you.
It would also be wise to define the version: 'x' element at the top of your compose file.
You may be suffering from an old version of the cli, akin to this question:
docker-compose : Unsupported config option for services service: 'web'

Can't access docker container over public ip

So I am creating an application that is running in a docker container on port 3000 of my local machine. I can open the app at localhost:3000, I can also open it on another device but on the same network by using 192.168.68.100:3000. However, I can't access the app by visiting my_public_ip:3000. I added port forwarding on my router for port 3000. I am running the app using docker-compose up and this is my docker-compose file:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- db
db:
container_name: db
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_DATABASE: "..."
MYSQL_USER: "..."
MYSQL_PASSWORD: "..."
MYSQL_ROOT_PASSWORD: "..."
adminer:
container_name: adminer
image: adminer
restart: always
ports:
- 8080:8080
networks:
webnet:
Any suggestions what am I missing?

Preventing development containers from running in production

I have a docker-compose.yml file that includes a container for API mocks as well as phpmyadmin and mongo-express containers none of which should be started in my production environment.
I already have seperate .env files for production and development. Is it possible to use variables from the active .env file to disable a container?
Here is my docker-compose.yml:
services:
mysql:
build: ./docker/mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
entrypoint: sh -c "/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USERNAME}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_ONETIME_PASSWORD=yes
ports:
- 3306:3306
restart: unless-stopped
volumes:
- ./data/mysql:/var/lib/mysql
phpmyadmin:
container_name: phpmyadmin
environment:
- PMA_HOST=mysql
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
mongo:
build: ./docker/mongo
container_name: mongo
environment:
- MONGO_INITDB_DATABASE=${MONGO_DATABASE}
- MONGO_INITDB_ROOT_USERNAME=${MONGO_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
ports:
- 27017:27017
restart: unless-stopped
volumes:
- ./data/mongo:/data/db
mongo-express:
build: ./docker/mongo-express
container_name: mongo-express
depends_on:
- mongo
environment:
- ME_CONFIG_BASICAUTH_PASSWORD=redacted
- ME_CONFIG_BASICAUTH_USERNAME=username
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_USERNAME}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_PASSWORD}
ports:
- 8081:8081
redis:
build: ./docker/redis
container_name: redis
ports:
- 6379:6379
restart: unless-stopped
volumes:
- ./data/redis:/data
mock-apis:
build: ./docker/mock-apis
container_name: mock-apis
command: >
/initscript.bash
ports:
- 81:80
volumes:
- ./mock-apis:/home/nodejs
php-fpm:
build:
context: ./docker/php-fpm
args:
HOST_UID: ${HOST_UID}
command: >
/initscript.bash
container_name: php-fpm
restart: unless-stopped
depends_on:
- mongo
- mysql
- redis
volumes:
- ./laravel:/var/www/
nginx:
build: ./docker/nginx
container_name: nginx
depends_on:
- php-fpm
ports:
- 80:80
restart: unless-stopped
volumes:
- ./laravel:/var/www/
version: "3"
I'm using profiles to scope my services. If I want to use PhpMyAdmin only in dev I add this profile to the service:
phpmyadmin:
container_name: phpmyadmin
environment:
- PMA_HOST=mysql
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
profiles: ["dev"]
So now I have to tell to docker compose if I want to use the dev profile. Else it will not start.
You can use one of these command (with this way you have to type --profile your_profil for each profile):
$ docker-compose --profile dev up -d
$ docker-compose --profile dev --profile profil2 up -d <- for multiple profiles
Or the cleaner way you can separate your services with a comma:
$ COMPOSE_PROFILES=dev,profil2 docker-compose up -d
Services without a profiles attribute will always be enabled.
Care about when you stop your services you have to specify the profile too:
$ COMPOSE_PROFILES=dev docker-compose down

How to access docker container localhost

I executed docker-compose up and my services worked as well but unfortunately when i hit the url " http://localhost:3000 " it gives me an error I'll paste the docker-compose.yml here.
version: "3.7"
services:
db:
image: postgres
container_name: psql
environment:
POSTGRES_DB: ${DB_DATABASE}
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
- ./postgresql/conf:/etc/postgresql/
restart: always
ports:
- '5432:5432'
api:
build: .
container_name: api
restart: always
command: yarn start
depends_on:
- db
ports:
- "3000:3000"
volumes:
- .:/usr/app
volumes:
postgres:
When I get into the container and execute curl localhost:3000 it works fine.
My problem was solved after I changed the host in my node api to '0.0.0.0' it was 'localhost' so I think the application was running only inside the container and after that change it was visible from all the created network.

docker-compose redis and redis commander

I use the windows docker toolbox and I am confused what I am missing. I want to use redis commander (https://www.npmjs.com/package/redis-commander) with a docker image redis from the docker hub.
I used the docker-compose.yml from above link:
version: '3'
services:
redis:
container_name: redis
hostname: redis
image: redis
redis-commander:
container_name: redis-commander
hostname: redis-commander
image: rediscommander/redis-commander:latest
build: .
restart: always
environment:
- REDIS_HOSTS=local:redis:6379
ports:
- 8081:8081
Now I can start the app with the toolbox IP on port 8081
Ther it says undefined redis server: local:redis:6379:0
Since I am using the toolbox I assume I have to put some IP correct in the compose file.
Using redis alone with $ docker run --name some-redis -d redis
works and I can reach the server und er local:6379
But what means REDIS_HOSTS=local:redis:6379
Any help to set this up correctly?
For fix that you need link redis and redis-commander like that:
version: "3.9"
services:
redis:
image: redis:6.2.5
command: redis-server --requirepass ${REDIS_PASSWORD}
volumes:
- redis:/var/lib/redis
- redis-config:/usr/local/etc/redis/redis.conf
ports:
- ${REDIS_PORT}:6379
networks:
- redis-network
redis-commander:
image: rediscommander/redis-commander:latest
restart: always
environment:
REDIS_HOSTS: redis
REDIS_HOST: redis
REDIS_PORT: redis:6379
REDIS_PASSWORD: ${REDIS_PASSWORD}
HTTP_USER: root
HTTP_PASSWORD: root
ports:
- 8081:8081
networks:
- redis-network
volumes:
redis:
redis-config:
networks:
redis-network:
driver: bridge
or that:
version: "3.9"
services:
redis:
image: redis:6.2.5
command: redis-server --requirepass ${REDIS_PASSWORD}
volumes:
- redis:/var/lib/redis
- redis-config:/usr/local/etc/redis/redis.conf
ports:
- ${REDIS_PORT}:6379
links:
- redis-commander
redis-commander:
image: rediscommander/redis-commander:latest
restart: always
environment:
REDIS_HOSTS: redis
REDIS_HOST: redis
REDIS_PORT: redis:6379
REDIS_PASSWORD: ${REDIS_PASSWORD}
HTTP_USER: root
HTTP_PASSWORD: root
ports:
- 8081:8081
volumes:
redis:
redis-config:
i think you missed to link your 2 containers.
the redis container needs a port + link and the redis-commander the correct environment.
you can only use the container name for the link/environment.
version: '3'
services:
redis:
container_name: redis
hostname: redis
image: redis
ports:
- "6379:6379"
links: redis-commander
redis-commander:
container_name: redis-commander
hostname: redis-commander
image: rediscommander/redis-commander:latest
build: .
restart: always
environment:
- REDIS_HOSTS=redis
ports:
- "8081:8081"
REDIS_HOSTS=local:redis:6379 means it will create config file to connect to docker container with the hostname redis on port 6379 and will have the connection name or label as local.
REDIS_HOSTS is used when you want to have multiple connection, separated by comma.
There are some ways to write REDIS_HOSTS as mentioned in the documentation.
hostname
label:hostname
label:hostname:port
label:hostname:port:dbIndex
label:hostname:port:dbIndex:password
Example:
Let say you want to use redis for two app called app1 and app2. app1 will have the db index 1, app2 will have the db index 2. The REDIS_HOSTS would look like:
REDIS_HOSTS=app1:redis:6379:1,app2:redis:6379:2
The working docker-compose.yml you could use (add network):
version: '3'
services:
redis:
container_name: redis
hostname: redis
image: redis
networks:
- redis_network
redis-commander:
container_name: redis-commander
hostname: redis-commander
image: rediscommander/redis-commander:latest
build: .
restart: always
environment:
- REDIS_HOSTS=local:redis:6379
ports:
- 8081:8081
networks:
- redis_network
networks:
redis_network:
driver: bridge

Resources