How to run and access web server running inside a Docker container - docker

I have trouble understanding how Docker handle things.
I am trying to run a node web server for development purpose. I have it defined in a docker-compose.yml and everything works fine when i run it from there. But when i manually run it from inside the container, it can't be reach from outside.
e.g : this is working fine
node:
image: node:10.15-stretch
tty: true
command: bash -c "./node_modules/.bin/encore dev-server --host 0.0.0.0 --public http://dev.local:8080 --port 8080 --disable-host-check --hot"
working_dir: /var/www/
volumes:
- ${PATH_SOURCE}:/var/www/
ports:
- 8080:8080
The files are now accessible from http://dev.local:8080 !
But i would prefer run it manually only when i need it...
So i remove the command from the docker-compose.yml and run it from inside the container :
node:
image: node:10.15-stretch
tty: true
working_dir: /var/www/
volumes:
- ${PATH_SOURCE}:/var/www/
ports:
- 8080:8080
docker-compose run node bash
root#1535e3c963cc:/var/www/# ./node_modules/.bin/encore dev-server --host 0.0.0.0 --public http://dev.local:8080 --port 8080 --disable-host-check --hot
The process is running fine but the files are not accessible from http://dev.local:8080 ...
I am sure there is something i am missing from Docker but i can't find what...
Thanks for your help.
EDIT:
here the full config
version: '3'
services:
apache:
image: httpd
volumes:
- ${PATH_SOURCE}/.docker/conf/apache/httpd.conf:/usr/local/apache2/conf/httpd.conf
- ${PATH_SOURCE}/.docker/conf/apache/httpd-vhosts.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
- ${PATH_SOURCE}:/var/www/sadc/alarm
ports:
- 80:80
- 443:443
restart: always
depends_on:
- php
- postgres
php:
build: .docker
restart: always
ports:
- 9000:9000
volumes:
- ${PATH_SOURCE}/.docker/conf/php/php.ini:/etc/php/7.1/cli/php.ini
- ${PATH_SOURCE}/.docker/conf/php/php.ini:/etc/php/7.1/fpm/php.ini
- ${PATH_SOURCE}:/var/www/sadc/alarm
environment:
- PGDATESTYLE=ISO,DMY
working_dir: /var/www/sadc/alarm
postgres:
image: mdillon/postgis:10
restart: always
environment:
- POSTGRES_DB=${PG_DATABASE}
- POSTGRES_USER=${PG_USERNAME}
- POSTGRES_PASSWORD=${PG_PASSWORD}
- PGDATESTYLE=ISO,DMY
ports:
- 5432:5432
volumes:
- sadc-alarm-pgdata:/var/lib/postgresql/data
- ${PATH_SOURCE}:/var/www/sadc/alarm
- ${PATH_SOURCE}/.docker/conf/postgres/initdb.sql:/docker-entrypoint-initdb.d/initdb.sql
node:
image: node:10.15-stretch
tty: true
working_dir: /var/www/sadc/alarm
volumes:
- ${PATH_SOURCE}:/var/www/sadc/alarm
ports:
- 8080:8080
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6a394453de4 node:10.15-stretch "node" 2 hours ago Up 50 seconds 0.0.0.0:8080->8080/tcp alarm_node_1
5dcc8b936b58 httpd "httpd-foreground" 21 hours ago Up 49 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp alarm_apache_1
bb616453d0cc alarm_php "/bin/sh -c '/usr/sb…" 21 hours ago Up 49 seconds 0.0.0.0:9000->9000/tcp alarm_php_1
3af75f3a3716 mdillon/postgis:10 "docker-entrypoint.s…" 28 hours ago Up 49 seconds 0.0.0.0:5432->5432/tcp
EDIT 2
Problem is with "docker-compose run" method...
When i run the following using "docker exec" it works
docker exec -it node_alarm_1 bash
FINAL EDIT
OK.
So i missused "docker-compose run" method. It is "docker-compose exec" method that should be use because it's reuse the running container that is correctly mapped. "docker-compose run" instead run a non-mapped container...

docker-compose run it doesn't seem to respect port publishing described in docker-compose.yml file.
To fix your issue do the following
docker-compose run -p 8080:8080 node bash
or
docker-compose run --service-ports node bash

Related

Container db1479aa5900ccb7acb83db7e3f4f1a5351f4ab1609edca36e41cb01a39d9e0e is not running

I have my docker-compose.yml with this code i have added 2 services. This successfully build with docker-compose up -d --build
version: "2"
services:
expo:
build:
context: ./dev
dockerfile: Dockerfile
container_name: sis-expo
working_dir: /home/node/app
volumes:
- .:/home/node/app
environment:
- NODE_ENV=development
#- EXPO_DEVTOOLS_LISTEN_ADDRESS=0.0.0.0
- ADB_IP=192.168.1.1
- REACT_NATIVE_PACKAGER_HOSTNAME=192.168.1.33
expose:
- "19000"
- "19001"
- "19002"
ports:
- 19000:19000
- 19001:19001
- 19002:19002
tty: true
firebase:
build:
context: ./dev
dockerfile: firebase.dockerfile
container_name: sis-expo-firebase
ports:
- 4000:4000 # Emulator Suite UI
- 5000:5000 # Firebase Hosting
- 5001:5001 # Clound Functions
- 9000:9000 # Realtime Database
- 8080:8080 # Cloud Firestore
- 8085:8085 # Cloud Pub/Sub
- 9005:9005
command: "echo this is a test"
then run docker-compose up -d to start container then i check with docker ps but the container isn't there then i tried to do docker ps -a it show container exited.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
511c3cf9163c sis-mobile_firebase "docker-entrypoint.s…" About a minute ago Exited (0) 11 seconds ago sis-expo-firebase
And its logs can be viewed with docker logs <id>.
this is a test
Please let me know if you need further information. Not sure if this is enough to truly solve my problem. thanks a lot.

How to add image name to docker-compose up

I have a docker-compose file that I use the image block in the service to name. For example
version: '3'
services:
redis:
image: redis
restart: unless-stopped
ports:
- "6379"
worker:
image: worker:production
build: .
user: root
command: celery -A ExactEstate worker --loglevel=info
env_file: ./.env.prod
restart: unless-stopped
links:
- redis
depends_on:
- redis
beats:
image: beats:production
build: .
user: root
command: celery --pidfile= -A ExactEstate beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
env_file: ./.env.prod
restart: unless-stopped
links:
- redis
depends_on:
- redis
web:
image: web:production
build: .
user: root
command: daphne -b 0.0.0.0 -p 8000 ExactEstate.asgi:application
ports:
- "8000:8000"
env_file: ./.env.prod
restart: unless-stopped
links:
- redis
- worker
- beats
depends_on:
- redis
- worker
- beats
This gives a docker ps of:
0ad78269a9ce beats:production "celery --pidfile= -…" 7 minutes ago Up 7 minutes exactestate_beats_1
1a44f7c98b50 worker:production "celery -A ExactEsta…" 7 minutes ago Up 7 minutes exactestate_worker_1
f3a09723ba66 redis "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:32769->6379/tcp exactestate_redis_1
Let's suppose I also have built containers from a different compose file (i.e. staging) How can I use docker-compose to on pull up the exact service/image I want?
For example: docker-compose up web:production or docker-compose up web:staging
You can achieve this by using environment variables. Variables for docker-compose up could be passed as .env file or set by export (or set on Windows) command (docker documentation).
web:
image: web:production
Should be changed to
web:
image: web:${ENV}
And then you can run your application by running
$ export ENV=production && docker-compose up
Or you can create .env file containing line ENV=production. Then you can simply run application with docker-compose up.

Why can't I connect to my local docker-compose container on Windows 10?

I'm trying to dockerize a Python application, for which I've been following this tutorial. The tutorial is from April 2015 and still uses Docker Machine, which, judging from this answer, is no longer necessary to run Docker containers locally on Windows.
I got it working with Docker Machine before, and was able to see the web app and interact with it. But now I'm trying to get this working without Docker Machine, with Docker version 17.06.0-ce, build 02c1d87, on Windows 10.
Here's the docker-compose.yml:
web:
restart: always
build: ./web
expose:
- "8000"
links:
- postgres:postgres
volumes:
- /usr/src/app/static
env_file: .env
command: /usr/local/bin/gunicorn -w 2 -b :8000 app:app
nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- web
links:
- web:web
data:
image: postgres:latest
volumes:
- /var/lib/postgresql
command: "true"
postgres:
restart: always
image: postgres:latest
volumes_from:
- data
ports:
- "5432:5432"
I started the containers:
$ docker-compose up -d
Creating polly_data_1 ...
Creating polly_data_1 ... done
Creating polly_postgres_1 ...
Creating polly_postgres_1 ... done
Creating polly_web_1 ...
Creating polly_web_1 ... done
Creating polly_nginx_1 ...
Creating polly_nginx_1 ... done
Then, when I run docker ps, it shows the following three containers running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b2c1048f3a5 polly_nginx "/usr/sbin/nginx" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp polly_nginx_1
d561ac5b901a polly_web "/usr/local/bin/gu..." 5 seconds ago Up 4 seconds 8000/tcp polly_web_1
ecb029d6ec3a postgres:latest "docker-entrypoint..." 7 seconds ago Up 5 seconds 0.0.0.0:5432->5432/tcp polly_postgres_1
(At this point, navigating to http://localhost:8000/ in Chrome already yields ERR_CONNECTION_REFUSED.)
I then ran the script to set up the database, as per the tutorial (extra //s because I'm using Git Bash on Windows 10):
$ docker-compose run web ///usr/local/bin/python create_db.py
Starting polly_data_1 ...
Starting polly_data_1 ... done
Starting polly_postgres_1 ... done
Now when I run docker ps, it shows the following four containers running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a129c12f5982 polly_web "//usr/local/bin/p..." 5 seconds ago Up Less than a second 8000/tcp polly_web_run_1
9b2c1048f3a5 polly_nginx "/usr/sbin/nginx" 16 seconds ago Up 15 seconds 0.0.0.0:80->80/tcp polly_nginx_1
d561ac5b901a polly_web "/usr/local/bin/gu..." 17 seconds ago Up 16 seconds 8000/tcp polly_web_1
ecb029d6ec3a postgres:latest "docker-entrypoint..." 19 seconds ago Up 17 seconds 0.0.0.0:5432->5432/tcp polly_postgres_1
And localhost:8000 is still refusing to connect. The web container exposes port 8000, so I don't get why I can't connect to it.
How can I get this working so I can access the web app in the web container locally?
Just change:
expose:
- "8000"
By
ports:
- "8000:8000"
Btw http://localhost:80 is not working?
Regards
Turns out, as suggested by Carlos and 200_OK as part of their answers and comments, it was working as intended - it was running at port 80, not 8000.
Web exposes port 8000 internally inside the container. But that port is not mapped to your host machine port.
I think the problem is in your command. The option is -p, not -b.
web:
restart: always
build: ./web
expose:
- "8000"
links:
- postgres:postgres
volumes:
- /usr/src/app/static
env_file: .env
command: /usr/local/bin/gunicorn -w 2 -p :8000 app:app

Links in docker-compose are not resolved

docker-compose.yml
version: '2'
services:
redis:
image: redis
ports:
- "6379"
myapp:
build: ./myapp
ports:
- "80:80"
links:
- redis
depends_on:
- redis
Dockerfile of myapp
FROM <BASE_IMAGE>
EXPOSE 80
CMD ["curl", "http://redis"]
When I run docker-compose up -d --build and then docker run prefix_myapp_1, I'm getting the 'Host is not resolved' error after 4-5 seconds.
If I change the Dockerfile, say, as follows:
FROM <BASE_IMAGE>
EXPOSE 80
CMD sleep 1000000000
then do docker exec -it prefix_myapp_1 bash, and try running curl http://redis, the host is resolved successfully.
Where the problem lies?

Docker Compose CLI Flags

I am not sure how to run the docker-compose equivalent of the following...
docker run -d -p 8080:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer --logo "https://www.docker.com/sites/all/themes/docker/assets/images/brand-full.svg"`
So far, I have the following, which I know works...
ui:
image: portainer/portainer
container_name: ui
restart: always
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
expose:
- 9000
ports:
- 8080:9000
Specifically, I can't figure out how the --logo flag translates to compose.
docker run does not mention any --logo parameter in its reference man page.
That means it could maybe represent a parameter pass to the default CMD of the container portainer/portainer being run.
That seems to be the case in issue 399:
You can use the CLI flags in the command field of your docker-compose file:
ui:
image: portainer/portainer
command: portainer --logo http://mylogo.com -l owner=acme --templates http://mytemplates.com
container_name: ui
restart: always
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
Tony points out in the comments to a docker-compose example

Resources