Docker-Compose - ASP.NET app not able to resolve DNS - windows-container

I am trying to run ASP.NET application with SQL server in windows container using docker compose.
I am able to ping SQL container IP from asp.net container however not able to connect through alias name. Please let me know if I am doing anything wrong.
My compose file:
version: '3'
#services details
services:
contosouniversity:
build:
context: .
ports:
- "5000:80"
expose:
- "5000"
env_file:
- ".env"
depends_on:
- university_db
#data base details
university_db:
image: microsoft/mssql-server-windows-developer
environment:
sa_password: "sa"
ACCEPT_EULA: "Y"
ports:
- "5672:1433"
#network configuration
networks:
default:
external:
name: nat
ASP.NET web.config file has connection string:
<add name="SchoolContext"
connectionString="Data Source=university_db;Initial Catalog=ContosoUniversity2;Integrated Security=false;User ID=sa;Password=sa"
providerName="System.Data.SqlClient" />

Related

How to automatically get an available port?

I'm working with docker containers for some projects and to save time i clone the docker composer file for my other projects.
The problem I have is that the ports for my mysql_database and apache_service are fixed values.
Example:
version: "3.2"
services:
apache_service:
build:
context: './docker/apache/'
links:
- mysql_service:mysql_service
depends_on:
- mysql_service
ports:
- "8080:80" # "random_port:80"
volumes:
- ./:/var/www/
mysql_service:
build:
context : ./
dockerfile : ./docker/mysql/Dockerfile
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--default-authentication-plugin=mysql_native_password',
]
restart:
always
volumes:
- ./docker/initdb:/docker-entrypoint-initdb.d
- ./docker/mysql/logs:/var/log/mysql
ports:
- "4306:3306" # "random_port:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
container_name:
mysql_service
When i copy the docker-composer file and write docker-composer up i alwayas have to change the ports previously...
How i could automatily get an avaliable port for this services?
Use 0 as the host port: this way you will get the first available (random) port from the operating system; to later get the actual used port you can use the docker port <container> command.

Login failed for user 'sa'

I am new in docker, and i am working on MAC.
i am using docker compose to containerize my dotnet api,
but when i try to do a migration after "docker-compose up" i have the error "Login failed for user 'sa'."
but when running the app locally using this sql server image i can be able to connect.
the two containers are running and this is my docker-compose file :
version: '3'
services:
web:
build: .
container_name: web
ports:
- "8000:80"
depends_on:
- db
networks:
- connection
db:
image: "mcr.microsoft.com/mssql/server:latest"
container_name: db
environment:
MSSQL_SA_PASSWORD: "P#$$w0rd!"
ACCEPT_EULA: "Y"
UserID: "sa"
ports:
- "1433:1433"
networks:
- connection
networks:
connection: driver: bridge
and this is my connection strings :
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Initial Catalog=database;User ID=sa;Password=P#$$w0rd!"},
In docker-compose.yml, the $$ is an escaped $ since $something expands the variable. Therefore:
P#$$w0rd!
becomes
P#$w0rd!
and the application will attempt to connect with the wrong password. You can escape both $'s with:
MSSQL_SA_PASSWORD: "P#$$$$w0rd!"

Expose DNS entries via docker-compose

I currently have the following setup:
# https://github.com/SeleniumHQ/docker-selenium
version: "3"
services:
selenium-hub:
image: ${DOCKER_REGISTRY}selenium/hub:2.53.1-americium
container_name: selenium-hub
ports:
- 4444:4444
environment:
- NODE_MAX_SESSION=5
- GRID_DEBUG=false
selenium-chrome:
image: ${DOCKER_REGISTRY}selenium/node-chrome-debug:2.53.1-americium
container_name: chrome
ports:
- 5900:5900
depends_on:
- selenium-hub
environment:
- HUB_PORT_4444_TCP_ADDR=selenium-hub
- HUB_PORT_4444_TCP_PORT=4444
- SHM-SIZE=2g
- SCREEN_WIDTH=2560
- SCREEN_HEIGHT=1440
- GRID_DEBUG=false
volumes:
- /tmp/
- /dev/shm/:/dev/shm/
tomcat:
build:
context: .
args:
ARTIFACTORY: ${DOCKER_REGISTRY}
container_name: tomcat
restart: on-failure
ports:
- 8080:8080
depends_on:
- db
volumes:
- ./src/test/resources/tomcat/context.xml:/opt/tomcat/conf/context.xml
- ./src/test/resources/tomcat/tomcat-users.xml:/opt/tomcat/conf/tomcat-users.xml
The above config sets up a selenium hub and deploys a webapp to a tomcat container. The resources that are served will have a href in the likes of http://tomcat:8080/...
If I want to access these resources via href from the outside, the tomcat DNS will not be resolved as the DNS is only exposed inside the virtual container network. One resolution would be to expose that internal DNS to the host machine, but I have no idea how.
Another would be to do a string replace of the href value and replace tomcat to localhost but that looks kind of dirty.
Anyone of you guys know how I can expose the internal DNS to the host machine?
Answer can be found at https://docs.docker.com/config/containers/container-networking/
Exposing /etc/hosts and /etc/resolv.conf

connect to mysql container from another container

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

Can't to connect to postgres container

I define postgres server in docker-compose.yml:
db:
image: postgres:9.5
expose:
- 5432
Then in another docker container I tried to connect to this postgres container. But it gives an error with warning:
Is the server running on host "db" (172.22.0.2) and accepting
data-service_1 | TCP/IP connections on port 5432?
Why container can't to connect to another by provided information (host="db" and port=5432)?
PS
Full docker-compose.yml:
version: "2"
services:
data-service:
build: .
depends_on:
- db
ports:
- "50051:50051"
db:
image: postgres:9.5
depends_on:
- data-volume
environment:
- POSTGRES_USER=cobrain
- POSTGRES_PASSWORD=a
- POSTGRES_DB=datasets
ports:
- "8000:5432"
expose:
- 5432
volumes_from:
- data-volume
# - container:postgres9.5-data
restart: always
data-volume:
image: busybox
command: echo "I'm data container"
volumes:
- /var/lib/postgresql/data
Solution #1. Same file.
To be able to access the db container, you have to define your other containers in context of docker-compose.yml. When containers are started, each container gets all other containers mapped in /etc/hosts.
Just do
version: '2'
services:
web:
image: your/image
db:
image: postgres:9.5
If you do not wish to put your other containers into the same docker-compose.yml, there are other solutions:
Solution #2. IP
Do docker inspect <name of your db container> and look for IPAddress directive in the result listing. Use that IPAddress as host to connect to.
Solution #3. Networks
Make your containers join same network. For that, under each service, define:
services:
db:
networks:
- myNetwork
Don't forget to change db for each container you are starting.
I usually go with the first solution during development. I use apache+php as one container and pgsql as another one, a separate DB for every project. I do not start more than one setting of docker-compose.yml, so in this case defining both containers in one .yml config is perfect.
the depends on is not correct. i would try to use other paramters like LINKS and environment:
version: "2"
services:
data-service:
build: .
links:
- db
ports:
- "50051:50051"
volumes_from: ["db"]
environment:
DATABASE_HOST: db
db:
image: postgres:9.5
environment:
- POSTGRES_USER=cobrain
- POSTGRES_PASSWORD=a
- POSTGRES_DB=datasets
ports:
- "8000:5432"
expose:
- 5432
#volumes_from:
#- data-volume
# - container:postgres9.5-data
restart: always
data-volume:
image: busybox
command: echo "I'm data container"
volumes:
- /var/lib/postgresql/data
this one works for me (not postgres but mysql)

Resources