How to add image name to docker-compose up - docker

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.

Related

Not all docker containers are starting on server startup/reboot

We have Ubuntu servers that automatically run Docker on startup and in the docker compose the containers have restart: always. One containers depends on the other 2 and this is the one that doesn't boot up correctly. If I run docker-compose restart it does show as running.
When I run docker-compose logs control-php> they are empty.
docker-compose ps on startup results in:
control-php docker-php-entrypoint /hom ... Exit 127
db docker-entrypoint.sh mysqld Up 3306/tcp
redis docker-entrypoint.sh redis ... Up 6379/tcp
control-php depends on db and redis (which are both running). I did try introducing health_checks on db and redis and adding them to the depends on for control-php but this didn't seem to change anything. In fact, when I ran docker-compose ps I could see the db and control-php condition was running. After sometime they became healthy but web and control-php still showed as 'Exit 127'.
Here's my docker-compose config:
version: '3'
services:
db:
container_name: db
image: mariadb:10.3.4
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_DATABASE: control
MYSQL_ROOT_PASSWORD: password
redis:
container_name: redis
image: redis:3.2.11
restart: always
volumes:
- redis_data:/data
control-php:
container_name: control-php
image: livebuzzevents/php:e4d22a4
environment:
- APP_ENV
- NODE
- REDIS_CLIENT="predis"
- REDIS_HOST="redis"
restart: always
volumes:
- /home/livebuzz/code/control:/var/www/control
- /home/livebuzz/onsite-setup/control/crontab:/home/crontab
- /home/livebuzz/onsite-setup/control/nuke-locks.php:/home/nuke-locks.php
depends_on:
- db
- redis
volumes:
db_data:
redis_data:
Any help would be appreciated.
Thanks,
Jack

Nginx do not start with docker-compose run command

I am dockerizing my existing application. But there's a strange issue. When i start my application with
docker-compose up
each service in the docker-compose runs successfully with no issues. But there are some services which i don't want to run sometimes (celery, celerybeat etc). For that i run
docker-compose run nginx
the above command should run nginx, web, db services as configured with docker-compose.yml but it only runs web and db not nginx.
Here's my yml file
docker-compose.yml
version: '3'
services:
db:
image: postgres:12
env_file: .env
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5431:5432"
volumes:
- dbdata:/var/lib/postgresql/data
nginx:
image: nginx:1.14
ports:
- "443:443"
- "80:80"
volumes:
- ./config/nginx/:/etc/nginx/conf.d
- ./MyAPP/static:/var/www/MyAPP.me/static/
depends_on:
- web
web:
restart: always
build: ./MyAPP
command: bash -c "
python manage.py collectstatic --noinput
&& python manage.py makemigrations
&& python manage.py migrate
&& gunicorn --certfile=/etc/certs/localhost.crt --keyfile=/etc/certs /localhost.key MyAPP.wsgi:application --bind 0.0.0.0:443 --reload"
expose:
- "443"
depends_on:
- db
env_file:
- .env
volumes:
- ./MyAPP:/opt/MyAPP
- ./config/nginx/certs/:/etc/certs
- ./MyAPP/static:/var/www/MyAPP.me/static/
broker:
image: redis:alpine
expose:
- "6379"
celery:
build: ./MyAPP
command: celery -A MyAPP worker -l info
env_file:
- .env
volumes:
- ./MyAPP:/opt/MyAPP
depends_on:
- broker
- db
celery-beat:
build: ./MyAPP
command: celery -A MyAPP beat -l info
env_file:
- .env
volumes:
- ./MyAPP:/opt/MyAPP
depends_on:
- broker
- db
comment-classifier:
image: codait/max-toxic-comment-classifier
volumes:
dbdata:
TL;dr: docker-compose up nginx
There's a distinct difference between docker-compose up and docker-compose run. The first builds, (re)creates, starts, and attaches to containers for a service. The second runs a one-time command against a service. When you do docker-compose run, it starts db and web because nginx depends on them, then it runs a single command on nginx and exits. So you have to use docker-compose up nginx in order to get what you want.

docker-compose: run a command on a pgsql container

I am trying to run the following docker-compose file:
version: "3"
services:
db:
image: postgres
container_name: pgsql
environment:
- foo=foo
- bar=bar
volumes:
- ./sql/:/opt/sql
command: bash /opt/sql/create-db.sql
# command: ps -aux
web:
image: benit/debian-web
container_name: web
depends_on:
- db
ports:
- 80:80
volumes:
- ./html:/var/www/html
I am encountering an error with the line:
command: bash /opt/sql/create-db.sql
It is because pgsql service is not started. It can be monitored with command: ps -aux
How can I run my script once pgsql service is started ?
You can use a volume to provide an initialization sql script:
version: "3"
services:
db:
image: postgres
container_name: pgsql
environment:
- foo=foo
- bar=bar
volumes:
- ./sql/:/opt/sql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
web:
image: benit/debian-web
container_name: web
depends_on:
- db
ports:
- 80:80
volumes:
- ./html:/var/www/html
This will work because original Posgresql dockerfile contains a script (that runs after Posrgres has been started) which will execute any *.sql files from /docker-entrypoint-initdb.d/ folder.
By mounting your local volume in that place, your sql files will be run at the right time.
It's actually mentioned in documentation for that image: https://hub.docker.com/_/postgres under the How to extend this image section.

Cannot launch django with celery in Docker Compose v3

Here is my docker-compose.yml:
version: '3.4'
services:
nginx:
restart: always
image: nginx:latest
ports:
- 80:80
volumes:
- ./misc/nginx.conf:/etc/nginx/conf.d/default.conf
- /static:/static
depends_on:
- web
web:
restart: always
image: celery-with-docker-compose:latest
build: .
command: bash -c "python /code/manage.py collectstatic --noinput && python /code/manage.py migrate && /code/run_gunicorn.sh"
volumes:
- /static:/data/web/static
- /media:/data/web/media
- .:/code
env_file:
- ./.env
depends_on:
- db
volumes:
- ./app:/deploy/app
worker:
image: celery-with-docker-compose:latest
restart: always
build:
context: .
command: bash -c "pip install -r /code/requirements.txt && /code/run_celery.sh"
volumes:
- .:/code
env_file:
- ./.env
depends_on:
- redis
- web
db:
restart: always
image: postgres
env_file:
- ./.env
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
redis:
restart: always
image: redis:latest
privileged: true
command: bash -c "sysctl vm.overcommit_memory=1 && redis-server"
ports:
- "6379:6379"
volumes:
pgdata:
When I run docker stack deploy -c docker-compose.yml cryptex I got
Non-string key at top level: true
And docker-compose -f docker-compose.yml config gives me
ERROR: In file './docker-compose.yml', the service name True must be a quoted string, i.e. 'True'.
I'm using latest versions of docker and compose. Also I'm new to compose v3 and started to use it for getting availability of docker stack command. If you see any mistakes or redudants in config file please, let me know. Thanks
you have to validate you docker compose file, is probably that the have low value inside
Validating your file now is as simple as docker-compose -f
docker-compose.yml config. As always, you can omit the -f
docker-compose.yml part when running this in the same folder as the
file itself or having the

How to name a volume using a docker-compose.yml file?

I'm new to Docker and I'm trying to find out how to set the name of the created data volume. Currently the directory is automatically named as a long hash under /var/libs/docker which is far from user friendly.
I'm attempting to set up a development environment for MODX as shown here:
https://github.com/modxcms/docker-modx
Currently my docker-compose.yml file is as follows:
web:
image: modx
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
This works perfectly but I'm unsure as to how to name the data volume that I would edit directly with my IDE.
(As a side question, does it have to be created under /var/libs/docker ? Or is there a way of setting it to a directory in my home folder?)
Update:
Thanks to the help from #juliano I've updated my docker-compose.yml file to:
version: '2'
services:
web:
image: modx
volumes:
- html:/home/muzzstick/dev/modxdev
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
volumes:
html:
external: false
Unfortunately this seems to stop the web container from running.
db and myadmin containers show they're running ok.
There weren't any errors... if I type docker start docker_web_1 it appears to start but docker ps -a shows it exited as soon as it started.
Update 2
Running docker-compose up -d appears to run without issue. But then as you can see below, the web container exits as soon as it's created.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1dd6d8ac94e modx "/entrypoint.sh apach" 10 seconds ago Exited (1) 5 seconds ago docker_web_1
ee812ae858dc phpmyadmin/phpmyadmin "/run.sh phpmyadmin" 10 seconds ago Up 5 seconds 80/tcp, 0.0.0.0:8080->8080/tcp docker_myadmin_1
db496134e0cf mysql "docker-entrypoint.sh" 11 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp docker_db_1
Update 3
OK the error logs for this container shows:
error: missing MODX_DB_HOST and MYSQL_PORT_3306_TCP environment variables
Did you forget to --link some_mysql_container:mysql or set an external db
with -e MODX_DB_HOST=hostname:port?
This error appears to be originating from https://github.com/modxcms/docker-modx/blob/master/apache/docker-entrypoint.sh#L15-L20
Could it be something like linking is handled differently in docker-compose version 2?
To create a named data volume using the version 2 of compose files you will have a separated area:
version: '2'
services:
db:
image: postgres
volumes:
- amazingvolume:/var/lib/postgresql/data
volumes:
amazingvolume:
external: true
So you can define the volume name (amazingvolume), if it's external or not and under your service (db in this example) you can define which directory you gonna mount.
Just search in the docker documentation for hosted mounted volumes:
version: '2'
services:
web:
image: modx
environment:
- MYSQL_PORT_3306_TCP=3306
- MODX_DB_HOST=mysql:3306
volumes:
- /home/muzzstick/dev/modxdev/html:/var/www/html
links:
- db:mysql
ports:
- 80:80
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
ports:
- 3306:3306
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION
myadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8080:8080
Change /var/www/html to the directory where the html files will be inside the container. And also create the directory at the left in your host and give read permission to all users.
Regards

Resources