How to add and enable swagger-ui config in docker? - docker-compose

I am trying to get configure the a docker-compose deployed swagger-ui with config options so as to enable the requestSnippets option.
My docker-compose service:
---
version: '3.8'
services:
swagger:
image: swaggerapi/swagger-ui:latest
container_name: swagger
environment:
SWAGGER_JSON_URL: "http://localhost:8083/services/rest/swagger.json"
ports:
- "9091:8080"
volumes:
- ./swagger-config.yaml:/usr/share/nginx/html/swagger-config.yaml
The config I'm trying to load: (Taken from the github doc page)
---
requestSnippetsEnabled: true
requestSnippets: >
{
generators: {
curl_bash: {
title: "cURL (bash)",
syntax: "bash"
},
curl_powershell: {
title: "cURL (PowerShell)",
syntax: "powershell"
},
curl_cmd: {
title: "cURL (CMD)",
syntax: "bash"
},
},
defaultExpanded: true,
languages: [ "curl_bash", "curl_powershell", "curl_cmd" ]
}
Has anyone attempted swagger config in this way who can enlighten me as to what I am missing?
Thanks.

Related

MongoDB Docker container not initializing authentication when deployed to Azure Container Instances using Bicep

I'm working on deploying a multi-container group to Azure Container Instances using Azure Bicep. The application is made up of 2 containers:
A .NET API (custom developed)
MongoDB (using the official 'mongo' imagine on Docker Hub)
The ACI container group is tied to a pre-existing subnet, disallowing public access. Persistent storage is achieved through Azure Storage File Shares mounted to the MongoDB Docker image.
All of this configuration works as intended once deployed through Bicep. The issue I can't seem to resolve is how to initialize the database to enable authentication.
According to the official MongoDB Docker Hub documentation by setting two environment variables - MONGO_INITDB_ROOT_USERNAME & MONGO_INITDB_ROOT_PASSWORD - a root user will be created using these details in the admin database. With both values set, mongod will also start with authentication enabled, as though the --auth flag was passed.
Additionally, it states that any .sh or .js scripts mounted to the docker-entrypoint-initdb.d directory will be executed on container start, allowing for additional databases, users and the like to be created.
I've set up all of this in my main.bicep file but upon successful deployment MongoDB is still running with authentication disabled. I can access the instance on its private IP without issue and even shell into it with mongosh. Querying the instance for users confirms that neither the admin user, or the application database\collection\user (as specified in my init script) were created.
Below is the section of my main.bicep file that deploys the container group. I've left off the other resource creations as I can confirm they are provisioned successfully. I've also confirmed that the initialization script is successfully mounted in docker-entrypoint-initdb.d
resource containerGroup 'Microsoft.ContainerInstance/containerGroups#2021-07-01' = {
name: containerGroupName
location: location
dependsOn: [
storageSubnet
]
properties: {
containers: [
{
name: apiContainerName
properties: {
image: apiContainerImage
ports: [
{
protocol: 'TCP'
port: 80
}
]
resources: {
requests: {
cpu: 1
memoryInGB: 1
}
}
}
}
{
name: dbContainerName
properties: {
image: dbContainerImage
ports: [
{
protocol: 'TCP'
port: 27017
}
]
resources: {
requests: {
cpu: 1
memoryInGB: 1
}
}
volumeMounts: [
{
mountPath: '/docker-entrypoint-initdb.d'
name: dbInitVolumeName
}
{
mountPath: '/data/mongoaz'
name: dbDataVolumeName
}
{
mountPath: '/var/log/mongoaz'
name: dbLogsVolumeName
}
]
environmentVariables: [
{
name: 'MONGO_INITDB_ROOT_USERNAME'
value: dbAdminUsername
}
{
name: 'MONGO_INITDB_ROOT_PASSWORD'
secureValue: dbAdminPassword
}
{
name: 'MONGO_INITDB_DATABASE'
value: 'admin'
}
{
name: 'MONGO_APP_DATABASE'
value: dbName
}
{
name: 'MONGO_APP_COLLECTION'
value: dbAppCollection
}
{
name: 'MONGO_APP_USERNAME'
value: dbAppUsername
}
{
name: 'MONGO_APP_PASSWORD'
secureValue: dbAppPassword
}
]
command: [
'mongod'
'--dbpath'
'/data/mongoaz'
'--logpath'
'/var/log/mongoaz/mongodb.log'
'--bind_ip'
'0.0.0.0'
]
}
}
]
imageRegistryCredentials: [
{
server: 'registry.hub.docker.com'
username: dockerUsername
password: dockerPassword
}
]
restartPolicy: 'OnFailure'
osType: 'Linux'
ipAddress: {
type: 'Private'
ports: [
{
protocol: 'TCP'
port: 80
}
{
protocol: 'TCP'
port: 27017
}
]
}
volumes: [
{
name: dbInitVolumeName
azureFile: {
shareName: fileShareDbInit.name
storageAccountName: storageaccount.name
storageAccountKey: storageaccount.listKeys('2021-02-01').keys[0].value
}
}
{
name: dbDataVolumeName
azureFile: {
shareName: fileShareDbData.name
storageAccountName: storageaccount.name
storageAccountKey: storageaccount.listKeys('2021-02-01').keys[0].value
}
}
{
name: dbLogsVolumeName
azureFile: {
shareName: fileShareDbLogs.name
storageAccountName: storageaccount.name
storageAccountKey: storageaccount.listKeys('2021-02-01').keys[0].value
}
}
]
subnetIds: [
{
id: containerGrpSubnetId
name: containerGrpSubnetName
}
]
}
}
What am I missing that would cause the initialization to be ignored? I don't see any errors or warning in the deployment logs so I don't think it's failing.
Any help would be greatly appreciated!
EDIT
I've also confirmed that it's not due to the data volume mount having a pre-initialized database by deleting the mount entirely & rerunning the Bicep template. So no preexisting users or databases should cause the user creation to be skipped.

Asp.net Core : Docker-Compose.yml configuration vs Docker Section configuration in the LaunchSettings.json

I have an Asp.net Core application with Docker-Compose.yml configuration and Docker Section configuration in the LaunchSettings.json
Connection string is configured like mentioned below in the Docker-Compose.yml
PersistenceAccess__ConnectionString= Server=db;Port=5432;Database=DemoDatabase;User Id=postgres;Password=postgres;
version: '3.4'
services:
Demo.api:
image: ${DOCKER_REGISTRY-}demoapi
build:
context: .
dockerfile: Sources/Code/Demo.Api/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- PersistenceAccess__ConnectionString= Server=db;Port=5432;Database=DemoDatabase;User Id=postgres;Password=postgres;
ports:
- '8081:80'
depends_on:
- db
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5438:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
# copy the sql script to create tables
- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
# copy the sql script to fill tables
- ./sql/fill_tables.sql:/docker-entrypoint-initdb.d/fill_tables.sql
Same Connection string is configured in the LaunchSettings.json like mentioned below
"PersistenceAccess__ConnectionString": "Server=host.docker.internal;Port=5432;Database=DemoDatabaseNew;User Id=postgres;Password=postgres;"
{
"iisSettings": {
..
},
"profiles": {
"IIS Express": {
...
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"PersistenceAccess__ConnectionString": "Server=host.docker.internal;Port=5432;Database=DemoDatabaseNew;User Id=postgres;Password=postgres;"
},
"DockerfileRunArguments": "--add-host host.docker.internal:host-gateway",
"publishAllPorts": true,
"useSSL": false
}
}
}
Which configuration will be used while I run the application using
docker-compose -f docker-compose.yml up
Does the above command create a Database? If so, when it will create the database and what would be the name of the database? Also, when it would create the tables and seed the data?
Please suggest.

How to fix "Error: getaddrinfo ENOTFOUND redis" on docker?

I am using Redis using NestJS and I see following error. I am going through different articles like here and looks like I am following the same but still getting this error.
Steps:
I used docker compose up command
Made sure that host in redis.module.ts is same as service name in docker-compose.yml which is redis.
What am I missing here?
Error:
Error: getaddrinfo ENOTFOUND redis
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
Code:
redis.module.ts
import { CacheModule, Module } from '#nestjs/common';
import { ConfigModule, ConfigService } from '#nestjs/config';
import { RedisService } from './redis.service';
import * as redisStore from 'cache-manager-redis-store';
import { envVariables } from '../env.variables';
#Module({
imports: [
CacheModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
store: redisStore,
host: process.env.REDIS_HOST,
port: configService.get('REDIS_PORT'),
ttl: configService.get('CACHE_TTL'),
max: configService.get('MAX_ITEM_IN_CACHE'),
}),
}),
],
providers: [RedisService],
exports: [RedisService],
})
export class RedisModule {}
.env
#REDIS
REDIS_HOST=redis
docker-compose.yml
version: "3.8"
services:
partnersusers:
image: partnersusers
build:
context: .
dockerfile: ./Dockerfile
environment:
- RUN_ENV=dev
- NODE_ENV=development
ports:
- "4000:4000"
networks:
- default
redis:
image: 'redis:alpine'
ports:
- "6379:4000"
networks:
default:
driver: bridge
Error in Docker:
I'm not an expert, but I notice a couple of things on your docker-compose.yml file.
First your redis service is missing the network assignation:
networks:
- default
Without this, redis-commander won't be able to find it as it's not on the same network.
Second redis by default runs on port: 6379 if you want it to run on port 4000 I believe you will need to specify an env var for it.
Or here maybe you just confused the order of the port matching which should've been: 4000:6379 (host_port:container_port).
this is my working docker-compose.yml for reference:
---
version: '3.8'
services:
...
redis:
image: redis
container_name: redis
hostname: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- '6379:6379'
networks:
- my-net
redis-commander:
depends_on:
- redis
container_name: redis-commander
hostname: redis-commander
image: rediscommander/redis-commander:latest
restart: always
environment:
- REDIS_HOSTS=local:redis:6379 # note: this has to be the port the redis container exposes.
ports:
- "8081:8081"
networks:
- my-net
networks:
my-net:
Hope this helps :)

Strapi + Next.JS + docker-compose: Is static generation impossible during build?

When I try to build my Next.JS application, which has a getStaticProps() function to get data from Strapi, the fetch fails. It fails because it is unable to resolve the alias for the Strapi container while building. Is there a way around this? Here's some info. Let me know if more info would be helpful:
Docker version 20.10.14
docker-compose version 1.29.2
Next.JS version 12.1.0
Node.JS version v17.6.0
In my pages/index.tsx file, I have the following:
export async function getStaticProps() {
let data: PageData = null;
try {
const response = await axios.get(
`${process.env.STRAPI_BACKEND_URL}/api/pages?filters[slug]=strapi-sample-homepage-content`,
{
headers: {
Accept: "application/json",
},
timeout: 500,
}
);
data = response.data.data[0].attributes || null;
} catch (error) {
console.log(error);
}
return {
props: {
data,
},
revalidate: 60,
};
}
This is what Next.JS wants to generate statically while building, and it's unable to because it cannot resolve the location of the Strapi container during build.
docker-compose.yml:
version: '3.8'
services:
db-strapi:
image: 'location/of/image'
container_name: db-strapi
restart: always
volumes:
- db-strapi:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${STRAPI_DB_NAME}
- POSTGRES_PASSWORD=${STRAPI_DB_PW}
- POSTGRES_SCHEMA=${STRAPI_DB_SCHEMA}
- POSTGRES_USER=${STRAPI_DB_USER}
strapi-backend:
image: 'location/of/image'
container_name: strapi-backend
environment:
- ADMIN_JWT_SECRET=${STRAPI_ADMIN_JWT_SECRET}
- API_TOKEN_SALT=${STRAPI_API_TOKEN_SALT}
- APP_KEYS=${STRAPI_APP_KEYS}
- DATABASE_CLIENT=${STRAPI_DB_CLIENT}
- DATABASE_HOST=${STRAPI_DB_HOST}
- DATABASE_NAME=${STRAPI_DB_NAME}
- DATABASE_PASSWORD=${STRAPI_DB_PW}
- DATABASE_PORT=${STRAPI_DB_PORT}
- DATABASE_SCHEMA=${STRAPI_DB_SCHEMA}
- DATABASE_USERNAME=${STRAPI_DB_USER}
- JWT_SECRET=${STRAPI_JWT_SECRET}
- NODE_ENV=${STRAPI_NODE_ENV}
ports:
- 1337:1337
depends_on:
- "db-strapi"
db-nextjs:
image: 'location/of/image'
container_name: db-nextjs
restart: always
volumes:
- db-nextjs:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${NEXTJS_DB_NAME}
- POSTGRES_PASSWORD=${NEXTJS_DB_PW}
- POSTGRES_SCHEMA=${NEXTJS_DB_SCHEMA}
- POSTGRES_USER=${NEXTJS_DB_USER}
nextjs-app:
image: 'location/of/image'
container_name: nextjs-app
environment:
- STRAPI_BACKEND_URL
- NEXTJS_DB_URL
- SECRET_COOKIE_PASSWORD
- NEXT_TELEMETRY_DISABLED=1
restart: always
depends_on:
- "db-nextjs"
nginx:
image: 'location/of/image'
container_name: nginx
environment:
- NGINX_SERVER_NAME
- NGINX_STRAPI_SERVER_NAME=${STRAPI_BACKEND_HOST}
- RESOLVER
- ENVIRONMENT
- NGINX_CERTIFICATE_NAME
- NEXTJS_HOST
- STRAPI_HOST
ports:
- 80:80
- 443:443
restart: always
depends_on:
- "nextjs-app"
- "strapi-backend"
volumes:
- certbot_certificates:/etc/letsencrypt
- certbot_challenges:/usr/share/nginx/html/.well-known/acme-challenge
volumes:
db-strapi:
db-nextjs:
certbot_certificates:
certbot_challenges:
In my .env.local file:
STRAPI_BACKEND_HOST="strapi-backend"
STRAPI_BACKEND_PORT="1337"
STRAPI_BACKEND_URL="http://${STRAPI_BACKEND_HOST}:${STRAPI_BACKEND_PORT}"
When I build, I have the following output:
info - Generating static pages (0/7)
info - Generating static pages (1/7)
info - Generating static pages (3/7)
info - Generating static pages (5/7)
Error: getaddrinfo ENOTFOUND strapi-backend
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:72:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'strapi-backend',
config: {
transitional: {
silentJSONParsing: true,
forcedJSONParsing: true,
clarifyTimeoutError: false
},
adapter: [Function: httpAdapter],
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 500,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: { Accept: 'application/json', 'User-Agent': 'axios/0.24.0' },
method: 'get',
url: 'http://strapi-backend:1337/api/pages?filters[slug]=strapi-sample-homepage-content',
data: undefined
},
To me, it seems there's no way to resolve this, because Next.js doesn't seem to be on the docker network yet during build. Are there any solutions?
It seems that Docker's hostname resolution does not work during build time. On Mac, you can try STRAPI_BACKEND_HOST = "host.docker.internal". On Linux, using host networking during build worked for me:
nextjs-app:
build:
network: "host"
# ...
network_mode: "host"
This way, you can use STRAPI_BACKEND_HOST = localhost.

what is the skywalking agent image address about 7.0.0

I am now using skywalking as my apm, and now I am configuring the address of my skywalking agent like this:
"initContainers": [
{
"name": "init-agent",
"image": "apache/skywalking-agent:7.0.0",
"command": [
"sh",
"-c",
"set -ex;mkdir -p /skywalking/agent;cp -r /opt/skywalking/agent/* /skywalking/agent;"
],
"resources": {},
"volumeMounts": [
{
"name": "agent",
"mountPath": "/skywalking/agent"
}
],
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "IfNotPresent"
}
],
but it tells me this address is not correct. Is skywalking agent having docker image? What is the docker image address to use in kubernetes v1.16.0 cluster? I am searching from internet and only find a skywalking base image.
We (Apache SkyWalking Team) have officially provided the Java agent Docker image here https://github.com/apache/skywalking-docker/tree/master/java-agent#use-this-image-as-sidecar-of-kubernetes-service, its usage can be also found there.
As for this specific question, you can always pass environment variable to override the default configuration, for the OAP backend address, its env var is SW_AGENT_COLLECTOR_BACKEND_SERVICES so you can simply pass SW_AGENT_COLLECTOR_BACKEND_SERVICES=your-oap-address:11800 to point the agent to your real OAP address.
apiVersion: v1
kind: Pod
metadata:
name: agent-as-sidecar
spec:
restartPolicy: Never
volumes:
- name: skywalking-agent
emptyDir: { }
containers:
- name: agent-container
image: apache/skywalking-java-agent:8.4.0-alpine
volumeMounts:
- name: skywalking-agent
mountPath: /agent
command: [ "/bin/sh" ]
args: [ "-c", "cp -R /skywalking/agent /agent/" ]
- name: app-container
image: springio/gs-spring-boot-docker
volumeMounts:
- name: skywalking-agent
mountPath: /skywalking
env:
- name: JAVA_TOOL_OPTIONS
value: "-javaagent:/skywalking/agent/skywalking-agent.jar"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: "your-oap-address-accessible-inside-docker" # <<=== THIS
Finally I build the side car image by myself:
wget https://www.apache.org/dyn/closer.cgi/skywalking/7.0.0/apache-skywalking-apm-7.0.0.tar.gz && tar -zxvf apache-skywalking-apm-7.0.0.tar.gz
this is the docker file:
FROM busybox:latest
ENV LANG=C.UTF-8
RUN set -eux && mkdir -p /usr/skywalking/agent/
ADD apache-skywalking-apm-bin/agent/ /usr/skywalking/agent/
WORKDIR /