i try to migrate from v.2 docker-compose file to v3.3
early i can write somthing like that
nginx:
image: nginx
ports:
- "80:80"
volumes_from:
- php
php:
build:
./docker/php
volumes:
- ./:code
now volume_from dont exist and i must do somthng like that
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./:/code
php:
build:
./docker/php
volumes:
- ./:/code
i dont like this way, i wish have one volumes for two container, i'm RTFM the docker docs and try do next
version: "3.3"
services:
nginx:
image: nginx
ports:
- "8014:80"
volumes:
- type: volume
source: data
target: /code
php:
build:
./docker/php
volumes:
- type: volume
source: data
target: /code
volumes:
data:
But, of couse i dont have any files in data how i can put's all my files from ./ to data ?
Related
I am trying to change my docker-compose to use volumes but my /application dir in containers is empty.
When i have config without volumes everything works fine:
volumes:
- .:/application
But when i use
volumes:
- code:/application
volumes:
code:
i get empty /application in containers.
Full docker-compose file:
version: "3.9"
services:
mariadb:
image: mariadb:10.5
container_name: youtube-playlist-mariadb
working_dir: /application
networks:
- backend
volumes:
- /var/lib/mysql/data:/var/lib/mysql
- /var/lib/mysql/logs:/var/log/mysqld.log
- /var/docker/mariadb/conf:/etc/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=database
- MYSQL_USER=root
- MYSQL_PASSWORD=password
ports:
- "3003:3306"
web:
image: nginx:alpine
container_name: youtube-playlist-web
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
php:
build: docker/php-fpm
container_name: youtube-playlist-php
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
node:
image: node:12.22.1
container_name: youtube-playlist-node
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
networks:
frontend:
backend:
volumes:
code:
x-mutagen:
sync:
defaults:
ignore:
vcs: true
code:
alpha: "."
beta: "volume://code"
mode: "two-way-resolved"
Edit: added mutagen config
Edit:
SOLUTION
I added this config to docker-compose
volumes:
code:
driver: local
driver_opts:
type: none
device: $PWD
o: bind
when you do what you are doing, you are just creating a volume where the content of the /application is going to be stored.
This is different to a bind-mount as you are doing in the first example (the one you say it's working). In this case, you are binding a directory (.) to /application.
I have a docker-compose which looks like:
version: '3.2'
services:
jobsaf-server:
build:
context: ./application
dockerfile: Dockerfile.production
container_name: jobsaf-server
env_file:
- ./application/.env
tty: true
depends_on:
- "redis"
- "mongo"
links:
- mongo
- redis
volumes:
- ./application/server:/var/www/app/jobsaf-website/server
- ./application/public/assets:/var/www/app/jobsaf-website/public/assets
- ./application/uploads:/var/www/app/jobsaf-website/uploads
- ./application/sitemaps:/var/www/app/jobsaf-website/sitemaps
- ./application/rss:/var/www/app/jobsaf-website/rss
- "/etc/timezone:/etc/timezone:ro"
- "/etc/localtime:/etc/localtime:ro"
nginx:
image: nginx:stable
tty: true
env_file:
- ./.env
environment:
- NGINX_HOST=${APP_HOST}
- NGINX_PORT=${APP_PORT}
- PUID=1001
- PGID=1001
- TZ=Asia/Kabul
links:
- jobsaf-server
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./nginx/ssl/star_jobs_af.pem:/etc/ssl/star_jobs_af.pem
- ./nginx/ssl/jobs.af.key:/etc/ssl/jobs.af.key
- ./nginx/ssl/star_jobs_af.crt:/etc/ssl/star_jobs_af.crt
ports:
- "80:80"
- "443:443"
mongo:
image: mongo:latest
container_name: mongo
tty: true
env_file:
- ./.env
volumes:
- "db-data:/data/db"
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USER}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
- MONGO_INITDB_DATABASE=admin
ports:
- "0.0.0.0:27017:27017"
redis:
image: redis
container_name: redis
tty: true
volumes:
db-data:
# - /data/db
networks:
front-tier:
back-tier:
It build jobsaf-server:latest by default.
what I want is to keep the old tag and build the new one.
let say, while building the images I should pass something similar to this
docker-compose -f docker-compose.production --tag=1.0.1
the above command should build for me and image with tag jobsaf-server:1.0.1
Is it really possible to have such result?
Or is there any alternative solution for it
Thanks in advance.
Note: I want to keep the old image, in case if my new image has issue, then I can use the old image.
version: '3.2'
services:
jobsaf-server:
image: jobsaf-server:${TAG}
build:
context: ./application
dockerfile: Dockerfile.production
...
The best way to supply the tag is with a .env file like this:
TAG=1.0.1
Docker-compose will pick this up automatically.
From #Mihai suggestion following steps worked for me, incase if someone else needed.
version: '3.2'
services:
jobsaf-server:
image: jobsaf-server:${TAG}
build:
context: ./application
dockerfile: Dockerfile.production
To build:
Run TAG=1.0 docker-compose build it will create jobsaf-server:1.0
To Up:
Run TAG=1.0 docker-compose up -d
To down:
Run TAG=1.0 docker-compose down
Note: we can add TAG to .env file also by default.
I have a value in a Dockerfile called ${APP_NAME}. What is it? If this were bash scripting, I would assume it to be some sort of variable but it hasn't been assigned a value anywhere. Is it a command line argument? If so, how would I pass it in when I wanted to call docker-compose with it?
For reference, the Docker file looks like this:
version: '2'
services:
nginx:
container_name: ${APP_NAME}_nginx
hostname: nginx
build:
context: ./containers/nginx
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- .:/app
links:
- phpfpm
networks:
- backend
phpfpm:
container_name: ${APP_NAME}_phpfpm
hostname: phpfpm
expose:
- "9000"
build:
context: ./containers/php-fpm
dockerfile: Dockerfile
working_dir: /app
volumes:
- .:/app
links:
- mysql
networks:
- backend
mysql:
container_name: ${APP_NAME}_mysql
hostname: mysql
build:
context: ./containers/mysql
dockerfile: Dockerfile
volumes:
- ./storage/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
ports:
- "33061:3306"
expose:
- "3306"
networks:
- backend
networks:
backend:
driver: "bridge"
And actually, I'm probably going to have a lot of questions about docker because I've never really used it before so a reference to Dockerfile syntax would be helpful.
This means that there is probably somewhere in your project .env file which contains variables necessary for docker compose. You can find more about it at the official docker compose docs. It says that you can set default values for environment variables using a .env file, which Compose automatically looks for. Values set in the shell environment override those set in the .env file. Try to find more here: https://docs.docker.com/compose/compose-file/#variable-substitution
I have a yml file with a configuration to run two containers. Here's the file:
web:
build: ./web
ports:
- "8000:8000"
restart: always
volumes:
- website:/www/
nginx:
build: ./nginx
ports:
- "80:80"
restart: always
links:
- web
volumes:
- website:/www/
volumes:
website:
When I run this I always get the following error:
The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for volumes: 'website'
I have googled this and I think this is good as it is now. What is wrong with it?
i think you should add version and services in docker-compose file.
version: '3'
services:
web:
build: ./web
ports:
- "8000:8000"
restart: always
volumes:
- website:/www/
nginx:
build: ./nginx
ports:
- "80:80"
restart: always
links:
- web
volumes:
- website:/www/
volumes:
website:
reference :
docker compose file
getting start with docker-compose
I'm using this docker-compose.yml.
And I wanna make simpler and inherrit configuration, if its possible.
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
web_one:
container_name: "web_one"
build:
context: ./
dockerfile: web.docker
volumes:
- ./../one:/var/www
environment:
- VIRTUAL_HOST=whoami_one.local
links:
- app_one
app_one:
container_name: "app_one"
build:
context: ./
dockerfile: app.docker
volumes:
- ./../one:/var/www
links:
- db
web_two:
container_name: "web_two"
build:
context: ./
dockerfile: web.docker
volumes:
- ./../two:/var/www
environment:
- VIRTUAL_HOST=whoami_two.local
links:
- app_two
app_two:
container_name: "app_two"
build:
context: ./
dockerfile: app.docker
volumes:
- ./../two:/var/www
links:
- db
I have 15 sites with same configuration.
Can I make config simpler? Like this:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
one:
extends:
file: common-services.yml
volumes:
- ./../one:/var/www
environment:
- VIRTUAL_HOST=whoami_one.local
two:
extends:
file: common-services.yml
volumes:
- ./../two:/var/www
environment:
- VIRTUAL_HOST=whoami_two.local
Or better?
Thank you!
UPDATE 31 Aug 2021 In the latest docker compose there's support for profiles https://docs.docker.com/compose/profiles/ so this problem would be perfectly solved by that new feature.
Another way is to create no-op services that depend on other services.
For example, in the following docker-compose.yml I have two namespaces, dev for services needed when developing the app, and metrics for services related to visualizing app metrics (since I'm not interested instarting those when developing).
version: "3"
services:
dev:
image: tianon/true
depends_on: ["postgres", "keycloak"]
metrics:
image: monroe/noop
depends_on: ["grafana"]
postgres: ...
keycloak: ...
grafana: ...