Connect to Docker Compose SQL Server from Visual Studio's SQL Server Object Explorer and save database to local PC - sql-server

Two questions:
Is it possible to connect to Docker Compose SQL Server from Visual Studio's SQL Sever Object Explorer? If so, how?
Visual Studio 2019 usually saves the local databases into C:\Users\Username\ProjectName.mdf. Can I make docker-compose save it on my local PC instead of the Linux docker? For example in C:\SkybotDb.
docker-compose.yml
version: '3.4'
services:
db:
container_name: skybotdb
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: "SkybotPassword123456"
ACCEPT_EULA: "Y"
ports:
- 1433:1433
restart: unless-stopped
networks:
- webnet
skybot.web:
image: ${DOCKER_REGISTRY-}skybotweb
build:
context: .
dockerfile: src/Skybot.Web/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://0.0.0.0:5001
- "UseInMemoryDatabase=true"
- "ConnectionStrings__DefaultConnection=Server=db;Database=SkybotDb;User=sa;Password=SkybotPassword123456;MultipleActiveResultSets=true"
- ElasticConfiguration__Uri=http://es01:9200
ports:
- 5000:5000
- 5001:5001
restart: on-failure
networks:
- webnet
depends_on:
- db
- es01
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.12.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- cluster.initial_master_nodes=es01
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
restart: unless-stopped
networks:
- webnet
kib01:
image: docker.elastic.co/kibana/kibana:7.10.1
container_name: kib01
ports:
- 5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
restart: unless-stopped
networks:
- webnet
volumes:
data01:
driver: local
networks:
webnet:
driver: bridge
Skybot.Web.Dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5000
EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["src/Skybot.Web/Skybot.Web.csproj", "src/Skybot.Web/"]
COPY ["src/Skybot.Application/Skybot.Application.csproj", "src/Skybot.Application/"]
COPY ["src/Skybot.Domain/Skybot.Domain.csproj", "src/Skybot.Domain/"]
COPY ["src/Skybot.Infrastructure/Skybot.Infrastructure.csproj", "src/Skybot.Infrastructure/"]
RUN dotnet restore "src/Skybot.Web/Skybot.Web.csproj"
COPY . .
WORKDIR "/src/src/Skybot.Web"
RUN dotnet build "Skybot.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Skybot.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Skybot.Web.dll"]

Related

Traefik Django & React setup

Recently I came across server configuration using GitLab CI/CD and docker-compose, I have two separated repositories one for Django and the other for React JS on Gitlab.
The Django Repo contains the following production.yml file:
version: '3'
volumes:
production_postgres_data: {}
production_postgres_data_backups: {}
production_traefik: {}
services:
django: &django
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: one_sell_production_django
platform: linux/x86_64
expose: # new
- 5000
depends_on:
- postgres
- redis
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
command: /start
labels: # new
- "traefik.enable=true"
- "traefik.http.routers.django.rule=Host(`core.lwe.local`)"
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: one_sell_production_postgres
expose:
- 5432
volumes:
- production_postgres_data:/var/lib/postgresql/data:Z
- production_postgres_data_backups:/backups:z
env_file:
- ./.envs/.production/.postgres
traefik: # new
image: traefik:v2.2
ports:
- 80:80
- 8081:8080
volumes:
- "./compose/production/traefik/traefik.dev.toml:/etc/traefik/traefik.toml"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
redis:
image: redis:6
This is work perfectly using the Traefik, I have also the following code for React JS repo:
version: '3.8'
services:
frontend:
build:
context: ./
dockerfile: Dockerfile
expose:
- 3000
labels: # new
- "traefik.enable=true"
- "traefik.http.routers.django.rule=Host(`lwe.local`)"
restart: 'always'
env_file:
- .env
Now I don't know how to connect both Django and React Js Repo using the Traefik and also how the CI/CD configuration should be, the following is the CI/CD configuration for Django Repo (I omitted unnecessary info and just include the deploy stage):
deploy:
stage: deploy
tags:
- docker
when: always
before_script:
- mkdir -p .envs/.production/
- touch .envs/.production/.django
- touch .envs/.production/.postgres
- touch .env
- chmod +x ./setup_env.sh
- sh setup_env.sh
- less .envs/.production/.django
- less .envs/.production/.postgres
- docker-compose -f production.yml build
- docker-compose -f production.yml run --rm django python manage.py migrate
script:
- docker-compose -f local.yml up -d

cannot dockerize react app: unable to connect to database

I am trying to dockerize a react app with postgres database.
I am new to docker, so I followed tutorials online to come up with Dockerfile and docker-compose as shown below.
Dockerfile
# pull the official base image
FROM node:13.12.0-alpine
# set working direction
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
EXPOSE 1338
ENV PATH /app/node_modules/.bin:$PATH
# install application dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm i
# add app
COPY . ./
# start app
CMD ["npm", "start"]
docker-compose.yml
version: '3.7'
services:
sample:
container_name: sample
build:
context: .
dockerfile: ./Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- 1338:1338
environment:
- CHOKIDAR_USEPOLLING=true
- ASPNETCORE_URLS=https://+:1338
- ASPNETCORE_HTTPS_PORT=1338
depends_on:
- db
db:
container_name: db
image: postgres:14-alpine
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: ###
POSTGRES_USER: ###
POSTGRES_PASSWORD: ###
# I hide these information for privacy purpose, but I am 100% sure I input these information correctly.
volumes:
- ./db-data/:/var/lib/postgresql/data/
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
pgdata1:
so what happened is when I tried to run docker-compose up , I suppose the db part had no issue, since it wrote database system is ready to accept connections. However, the "sample" part ended up with an error:
Server wasn't able to start properly.
error Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete]
which does not make much sense to me since the database is already up so there should not be any issue with connection at all.
Feel free to share your view, any idea would be appreciated. Thank you.

Docker is not saving django media files into project 'media' directory on production

App Description
I have an app with django-gunicorn for back-end and reactjs-nginx with front-end all containerized as well as hosted on aws ec2 instance.
Problem
On development environment, media files are being saved in the 'media' directory permanently. Tho, those files are only saved on the current running docker container on production time. As a result, the files will be removed when I rebuild/stopped the container for a new code push.
Expectation
I wanted to store the file on the 'media' folder for permanent use.
Important code
settings.py
ENV_PATH = Path(__file__).resolve().parent.parent
STATIC_ROOT = BASE_DIR / 'django_static'
STATIC_URL = '/django_static/'
MEDIA_ROOT = BASE_DIR / 'media/'
MEDIA_URL = '/media/'
docker-compose-production.yml
version: "3.3"
services:
db:
image: postgres
restart: always #Prevent postgres from stopping the container
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- 5432:5432
nginx:
restart: unless-stopped
build:
context: .
dockerfile: ./docker/nginx/Dockerfile
ports:
- 80:80
- 443:443
volumes:
- static_volume:/code/backend/server/django_static
- ./docker/nginx/production:/etc/nginx/conf.d
- ./docker/nginx/certbot/conf:/etc/letsencrypt
- ./docker/nginx/certbot/www:/var/www/certbot
depends_on:
- backend
# Volume for certificate renewal
certbot:
image: certbot/certbot
restart: unless-stopped
volumes:
- ./docker/nginx/certbot/conf:/etc/letsencrypt
- ./docker/nginx/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
backend:
restart: unless-stopped
build:
context: .
dockerfile: ./docker/backend/Dockerfile
entrypoint: /code/docker/backend/wsgi-entrypoint.sh
volumes:
- .:/code
- static_volume:/code/backend/server/django_static
expose:
- 8000
depends_on:
- db
volumes:
static_volume: { }
pgdata: { }
I finally figured out the issue. I forgot to add .:/code to my nginx volumes config in my docker-compose file. Thank to this answer
Updated nginx volumes confi
volumes:
- .:/code
- static_volume:/code/backend/server/django_static
- ./docker/nginx/production:/etc/nginx/conf.d
- ./docker/nginx/certbot/conf:/etc/letsencrypt
- ./docker/nginx/certbot/www:/var/www/certbot

MongoNetwork ECONNREFUSED when renaming service and database

I have a problem starting mongodb with Docker. I have some code which i want to reuse for different purpose. After i made a copy of that code everything worked just fine but after renaming the service, database and building everything again with
docker-compose -f docker-compose.dev.yml build
and running with
docker-compose -f docker-compose.dev.yml up
mongodb won't start and i get the ECONNREFUSED error. I tried to remove all the services and containers with
docker-compose -f docker-compose.dev.yml rm
docker rm $(docker ps -a -q)
but nothing seems to help. I also tried to discard all the changes i made (to the point where it worked) but it still doesn't work. I am quite new to programming itself and have no idea what is happening. What am i missing?
Also including my config.js, .env and docker-compose.dev.yml files.
Config.js
const config = {
http: {
port: parseInt(process.env.PORT) || 9000,
},
mongo: {
host: process.env.MONGO_HOST || 'mongodb://localhost:27017',
dbName: process.env.MONGO_DB_NAME || 'myresume',
},
};
module.exports = config;
.env
NODE_ENV=development
MONGO_HOST=mongodb://db:27017
MONGO_DB_NAME=myresume
PORT=9001
docker-compose.dev.yml
version: "3"
services:
myresume-service:
build: .
container_name: myresume-service
command: npm run dev
ports:
- 9001:9001
links:
- mongo-db
depends_on:
- mongo-db
env_file:
- .env
volumes:
- ./src:/usr/myresume-service/src
mongo-db:
container_name: mongo-db
image: mongo
ports:
- 27017:27017
volumes:
- myresume-service-mongodata:/data/db
environment:
MONGO_INITDB_DATABASE: "myresume"
volumes:
myresume-service-mongodata:
I am not completely sure but I think that your service needs the env var
MONGO_HOST=mongodb://mongo-db:27017 instead of the one that you have. The two services are only visible to each other that way. I believe you also need a network to connect the two of them.
something like this:
version: "3"
networks:
my-network:
external: true
services:
myresume-service:
build: .
container_name: myresume-service
command: npm run dev
ports:
- 9001:9001
links:
- mongo-db
depends_on:
- mongo-db
env_file:
- .env
volumes:
- ./src:/usr/myresume-service/src
networks:
- my-network
mongo-db:
container_name: mongo-db
image: mongo
ports:
- 27017:27017
volumes:
- myresume-service-mongodata:/data/db
environment:
MONGO_INITDB_DATABASE: "myresume"
networks:
- my-network
volumes:
myresume-service-mongodata:
you probably need to create the network using the command:
docker network create my-network

Transform docker-compose environment variable's value to service ip address

I want to convert the environtment variable's value (it is a service in the same docker-compose.yml file) to the ip of that respective container and i can't achieve this.
docker-compose.yml
version: '3'
services:
server:
restart: always
build:
dockerfile: Dockerfile
context: ./server
ports:
- '3001:3001'
volumes:
- /app/node_modules
- ./server:/app
client:
restart: always
build:
dockerfile: Dockerfile
context: ./client
ports:
- '3000:3000'
volumes:
- /app/node_modules
- ./client:/app
environment:
- REACT_APP_API_HOST=server #I WANT TO TRANSFORM THIS TO THE SERVICE IP
- REACT_APP_API_PORT=3001
When i console.log(process.env.REACT_APP_API_HOST) i get "server" in the console and i want this to be the ip address of the server (it's an api)
Thanks.
Try to rely on the service or container names instead. They will be resolved by the docker daemon.
version: '3'
services:
server:
container_name: myserver
...
client:
container_name: myclient
...
environment:
- REACT_APP_API_HOST=myserver
- REACT_APP_API_PORT=3001

Resources