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?
Related
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
I've created docker-compose.yml file with the following content. My docker service runs ok, docker run hello-world works just fine and after running command
docker-compose build && docker-compose up -d
I see no errors but in the browser I'm getting an error Unable to connect when I go to localhost:8000
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8000:80"
depends_on:
- php
- mysql
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
mysql:
image: mysql:8.0
container_name: mysql
restart: unless-stopped
ports:
- "3306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root
SERVICE_NAME: mysql
When I run command
docker-compose exec php ls
I can see the list of files and folders
i've installed docker (windows 10) with wsl2 (ubuntu distro) and added my docker-compose.yml
version: '3'
services:
web:
image: nginx:1.20.1
container_name: web
restart: always
ports:
- "80:80"
volumes:
- ./nginx.d.conf:/etc/nginx/conf.d/nginx.conf
- ./nginx.conf:/etc/nginx/nginx.conf
- ./www/my-app:/app
php:
build:
context: .
dockerfile: myphp.dockerFile
container_name: php
restart: always
depends_on:
- web
volumes:
- ./www/my-app:/app
mysql:
image: mariadb:10.3.28
container_name: mysql
restart: always
depends_on:
- php
environment:
MYSQL_ROOT_PASSWORD: '******'
MYSQL_USER: 'root'
MYSQL_PASSWORD: '******'
MYSQL_DATABASE: 'my-database'
command: ["--default-authentication-plugin=mysql_native_password"]
volumes:
- mysqldata:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
ports:
- 3306:3306
cache:
image: redis:5.0.3
container_name: cache
restart: always
ports:
- 6379:6379
networks:
- my-network
volumes:
- ./cache:/cache
volumes:
mysqldata: {}
networks:
my-network:
driver: "bridge"
So my symfony code is in the /www/my-app window's folder. This includes the /www/my-app/vendor too.
My application is running extremely slow (50-70 seconds). If i'm correct it's because the vendor folder is huge (80MB) and docker creates an image of it every time. Other discussions mentioned that vendor folder sould be moved into a new volume, and here i'm stuck with it. How to move and mount that in this case, and how should the docker-compose.yml look like after it?
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.
i have a problem with docker container.
That's my docker-compose file with 5 services
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- mysql
- php
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
redis:
image: redis:5.0.0-alpine
restart: always
container_name: redis
ports:
- "6379:6379"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
tty: true
working_dir: /var/www/html
networks:
- laravel
then i run
docker-compose up -d
and then
docker-compose ps
to see my container and i always get the composer contaier down with code 0. that's the screenshot
:
can someone explain me why i can't put this container up. Thanks a lot
composer isn't a program that stays alive. It's a program that does specific some work and then exits.
There's not much purpose in keeping it "up", since it's not going to do anything like the other processes do (nginx intercepts web traffic and writes response, mysql accepts database connections and reads/writes from a database, php serves web content, redis can be connected to as a cache).