I have two different projects with the same docker configuration (docker-compose.yml), but different files.
├── a
│ ├── docker-compose.yml
│ └── Dockerfile
└── b
├── docker-compose.yml
└── Dockerfile
How is it possible to build containers with the same name but for different projects? I don't want to think of new names for each project if I work only on one project at the same time.
ERROR: for mysql Cannot create container for service mysql: Conflict. The container name "/mysql" is already in use by container "90a84268d483ec2bd5cf0feb7ab1972384941ba255a671c0cfd2b4017ce90682". You have to remove (or rename) that container to be able to reuse that name.
Docker-compose.yml
version: '3'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0.19
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306: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
Don't override container_name.
Related
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 have a Docker setup serving two WordPress blogs with a proxy, and using the following structure and docker-compose file:
.
├── nginx-proxy
│ └── docker-compose.yml
└── blogs
└── docker-compose.yml
docker-compose.yml
version: "3"
services:
db:
container_name: ${CONTAINER_DB_NAME}
image: mariadb:latest
restart: unless-stopped
volumes:
- ${DB_PATH}:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
wordpress1:
depends_on:
- db
container_name: ${CONTAINER_WP_NAME1}
image: wordpress:latest
restart: unless-stopped
volumes:
- ${WP_CORE}:/var/www/html
- ${WP_CONTENT}:/var/www/html/wp-content
- ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
WORDPRESS_DB_USER: ${MYSQL_USER1}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
VIRTUAL_HOST: ${DOMAINS1}
LETSENCRYPT_HOST: ${DOMAINS1}
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
logging:
options:
max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}
wordpress2:
depends_on:
- db
container_name: ${CONTAINER_WP_NAME2}
image: wordpress:latest
restart: unless-stopped
volumes:
- ${WP_CORE}:/var/www/html
- ${WP_CONTENT}:/var/www/html/wp-content
- ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME2}:3306
WORDPRESS_DB_NAME: ${MYSQL_DATABASE2}
WORDPRESS_DB_USER: ${MYSQL_USER2}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD2}
WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX2}
VIRTUAL_HOST: ${DOMAINS2}
LETSENCRYPT_HOST: ${DOMAINS2}
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL2}
logging:
options:
max-size: ${LOGGING_OPTIONS_MAX_SIZE2:-200k}
networks:
default:
external:
name: ${NETWORK}
But I'd like to make it more maintainable. I want to refactor this in order to make a separate container to the mysql db, refactor each blog config to it's own docker-compose file and reference the db on each blog configuration, like so:
.
├── db
│ └── docker-compose.yml
├── blog1
│ └── docker-compose.yml
├── blog2
│ └── docker-compose.yml
└── blog3
└── docker-compose.yml
db container
version: "3"
services:
db:
container_name: ${CONTAINER_DB_NAME}
image: mariadb:latest
restart: unless-stopped
volumes:
- ${DB_PATH}:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
default:
external:
name: ${NETWORK}
blog1 container
version: "3"
services:
wordpress1:
depends_on:
- db // Does Docker knows this is db service I created outside it?
container_name: ${CONTAINER_WP_NAME1}
image: wordpress:latest
restart: unless-stopped
volumes:
- ${WP_CORE}:/var/www/html
- ${WP_CONTENT}:/var/www/html/wp-content
- ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
WORDPRESS_DB_USER: ${MYSQL_USER1}
WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
VIRTUAL_HOST: ${DOMAINS1}
LETSENCRYPT_HOST: ${DOMAINS1}
LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
logging:
options:
max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}
networks:
default:
external:
name: ${NETWORK}
... and so on.
I have no idea on how to handle the db connection. Today, I put a reference to it at the "depends_on" section, as the db service configuration lies on the same file as the blog. But how do I invoke the db instance when the sites are declared into separate files? Does Docker knows that the "db" is the db service declared externally to that file?
Any ideas would be great!
You can specify multiple compose files when running a docker compose command, you could for instance set up a single blog with:
docker-compose -f db.yml -f blog1.yml up
or all of them:
docker-compose -f db.yml -f blog1.yml -f blogX.yml up
Read more on how to specify multiple compose files
Note that you could also use an environment variable to specify your files:
COMPOSE_FILE=db.yml:blog1.yml docker-compose up
More here
The easiest way to set this up would be with a separate database per application. So split this out into blog1/docker-compose.yml and so on as you describe, but have each of those Compose files be self-contained, with its own db: and wordpress: services. You can then launch as many copies of this as you'd like, provided that (a) you set different published ports: for each copy, and (b) that you launch each copy from a differently-named directory.
You should also let Compose set container names for you; this will reduce the possibility of collisions. Delete the container_name: fields. Containers can still reach each other using their services: name as host names; for example, set WORDPRESS_DB_HOST: db:3306. There are docker-compose ... equivalents to most of the standard plain docker commands, but if you do need to directly interact with a container they'll usually be named things like blog1_db_1.
I'm trying to connect to a mysql container from within another container on the same network specified by a compose file.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
volumes:
- ./data:/srv
mysql:
image: mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
I'm not really sure what the connection parameters would be if I'm trying to connect to the mysql container from the php container.
Specifically, what are the host and port for the mysql container from within another container of the same docker-compose network?
I've tried host: mysql port: 3306, but that doesn't seem to work.
You should link the containers. Add ports section to mysql container and links section to php container.
version: "2"
services:
web:
build:
context: .
dockerfile: nginx/Dockerfile
ports:
- "8080:80"
volumes:
- ./data:/srv
php:
build:
context: .
dockerfile: php-fpm/Dockerfile
links:
- mysql:mysql
volumes:
- ./data:/srv
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_USER=dummy_user
- MYSQL_PASSWORD=12345
With that configuration you'll be able to access mysql container from php with mysql:3306
A bit of documentation: https://docs.docker.com/compose/networking/#updating-containers
You don't share port for mysql
add to your docker-compose.yml to mysql service:
ports:
- "3306:3306"
UPD
try to set environments of your mysql container
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: dev
MYSQL_PASSWORD: 123456
MYSQL_DATABASE: myapp
and
$link = mysqli_connect("mysql", "dev", "123456", "myapp");
should works fine
I have the following docker-compose.yml:
version: '3'
services:
web:
build:
context: .
dockerfile: ./.docker/node.dockerfile
volumes:
- D:\Proj\Web:/app
ports:
- '3000:3000'
depends_on:
- 'db'
networks:
- holder-network
restart: on-failure
tty: true
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
networks:
- holder-network
restart: always
environment:
MYSQL_ROOT_PASSWORD: P#ssw0rd
MYSQL_DATABASE: holder_db
MYSQL_USER: holder_usr
MYSQL_PASSWORD: P#ssw0rd
networks:
holder-network:
driver: bridge
volumes:
db_data:
And the node.dockerfile:
FROM node:7.10
MAINTAINER Juliano Nunes
RUN mkdir /var/www
RUN npm install nodemon -g
WORKDIR /var/www
ADD . /var/www
RUN npm install
CMD nodemon
I'm changing the files under D:\Proj\Web from the host, however it doesn't update the files in the container. Why?
I've found the error. My docker-compose.yml was using /app as the path and node.dockerfile was using /var/www.
Here's the updated (working) version:
version: '3'
services:
web:
build:
context: .
dockerfile: ./.docker/node.dockerfile
volumes:
- D:\Proj\Web:/var/www
ports:
- '3000:3000'
depends_on:
- 'db'
networks:
- holder-network
restart: on-failure
tty: true
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
networks:
- holder-network
restart: always
environment:
MYSQL_ROOT_PASSWORD: P#ssw0rd
MYSQL_DATABASE: holder_db
MYSQL_USER: holder_usr
MYSQL_PASSWORD: P#ssw0rd
networks:
holder-network:
driver: bridge
volumes:
db_data:
To map a host volume in a container running on docker for windows, you need to ensure your drive is shared into the embedded VM. Otherwise, the directory will mount from the VM's filesystem which will be empty at that location. You need to go into the docker settings on Windows and add the D drive to the shared directory list. See docker's documentation on this here: https://docs.docker.com/docker-for-windows/#shared-drives
I have two project using docker, in first project is laravel, and second is wordpress. In laravel I want to connect both database (to convert laravel database to wordpress database).
but I don't know how to connect it:
here is two docker-compose.yml file:
in laravel:
version: '2'
services:
# The Application
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
# The Web Server
web:
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8081:80
# The Database
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=homestead"
- "MYSQL_USER=homestead"
- "MYSQL_PASSWORD=secret"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
volumes:
dbdata:
and my docker-compose.yml file in wordpress:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wpshop
MYSQL_USER: root
MYSQL_PASSWORD: 123456
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
volumes:
db_data:
I cd to each project and run docker-compose up -d
Please help me!
Create an external network and use that network as default network for all your containers. This way you'll be able to reach all container by its name.
Take a look to Docker container networking: https://docs.docker.com/engine/userguide/networking/