GoLang postgres testcontainers init script doesn't work - docker

I want to start postgres container with init script.
request := testcontainers.ContainerRequest{
Image: "postgres:14.1-alpine",
Entrypoint: nil,
Env: map[string]string{
"POSTGRES_DB": "postgres",
"PGUSER": "postgres",
"POSTGRES_PASSWORD": "postgres",
//"PGDATA": "postgres",
},
ExposedPorts: []string{"5432"},
BindMounts: map[string]string{
"/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
},
Name: "postgres",
User: "postgres",
WaitingFor: wait.ForLog("database system is ready to accept connections"),
AutoRemove: true,
}
container, err = testcontainers.GenericContainer(
test.CTX,
testcontainers.GenericContainerRequest{
ContainerRequest: request,
Started: true,
},
)
if err != nil {
log.Panicln(err)
}
I got the following panic messages in log.Panicln(err):
failed to create container
Error response from daemon
invalid mount config for type "bind": bind source path does not exist: /docker-entrypoint-initdb.d
The point is that it perfectly works from docker-compose.yml.
How to fix this?

Looking at the source, it appears that TestContainers wants container_path: host_path in BindMounts. What happens if you try:
BindMounts: map[string]string{
"/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
},
It looks like more recent versions of TestContainers have removed BindMounts completely and replaced it with a more generic Mounts field.

Related

Selenium Selenoid File Server not running in Browser Container

selenium is unable to download any files from the browsers due to a 502 error on my coworkers machine, none of my other coworkers are seeing the issue, just this one dude. We are using Firefox.
After looking at the Selenoid code a bit I learned that the containers the Browser runs in uses a File Server on port 8080 to allow downloading files from the container, but I discovered that this File Server is not running within these containers.
I verified this through this command:
docker exec -it <browser_container> curl 127.0.0.1:8080
On my machine I get a 200 response:
<pre>
test.xlsx
</pre>
But when I run this command on his machine I get this error:
Failed to connect to 127.0.0.1 port 8080 after 8 ms: Connection refused
This is indicative that the File Server is not running within his Browser Containers. I've been trying many different firefox arguments and I've restart selenoid and the docker containers and still can't figure out what's going on, I'm completely lost right now. If anyone knows what might be going on I would be appreciative, or even if anyone has any idea how to gain more information into what's going on.
Here is the Firefox options we are using
options = webdriver.FirefoxOptions()
options.add_argument('--width=1600')
options.add_argument('--height=900')
options.set_preference('browser.download.dir', '/home/selenium/Downloads')
And our browsers.json file
{
"chrome": {
"default": "105.0",
"versions": {
"105.0": {
"image": "selenoid/vnc_chrome:105.0",
"port": "4444",
"path": "/",
"env": ["TZ=America/Denver"]
}
},
"caps": {
"loggingPrefs": {"browser": "ALL"},
"enableVNC": true,
"browserName": "chrome",
"timeZone": "America/Denver",
"sessionTimeout": "1m30s"
}
},
"firefox": {
"default": "latest",
"versions": {
"latest": {
"image": "selenoid/firefox",
"port": "4444",
"path": "/wd/hub",
"env": ["TZ=America/Denver"]
}
},
"caps": {
"loggingPrefs": {"browser": "ALL"},
"enableVNC": true,
"browserName": "firefox",
"timeZone": "America/Denver",
"sessionTimeout": "1m30s"
}
}
}
We do have a custom docker-compose.yml file for starting the selenoid and selenoid_ui containers, here is the file just in case that setup matters, I doubt the issue lies here.
version: "3.9"
networks:
selenoid_net:
name: selenoid_net
attachable: true
ipam:
config:
- subnet: 172.198.1.0/24
services:
selenoid:
image: aerokube/selenoid
restart: always
networks:
selenoid_net:
ports:
- "4444:4444"
environment:
- OVERRIDE_VIDEO_OUTPUT_DIR=${VIDEO_OUTPUT}/video
- TZ=America/Denver
volumes:
- "/etc/selenoid:/etc/selenoid"
- "/var/run/docker.sock:/var/run/docker.sock"
- "${VIDEO_OUTPUT}/video:${VIDEO_OUTPUT}/video"
- "${VIDEO_OUTPUT}/logs:${VIDEO_OUTPUT}/logs"
- "${PWD}:/etc/browsers"
command: ["-conf", "/etc/browsers/browsers.json",
"-video-output-dir", "${VIDEO_OUTPUT}/video",
"-log-output-dir", "${VIDEO_OUTPUT}/logs",
"-limit", "6",
"-timeout", "1m30s","-container-network", 'selenoid_net']
selenoid-ui:
image: "aerokube/selenoid-ui:latest"
restart: always
networks:
selenoid_net:
links:
- "selenoid"
ports:
- "8080:8080"
command: ["--selenoid-uri", "http://selenoid:4444"]

Dockerize Vue.js: hot-reload is not working for vue/cli#4.5 or later versions

I greatly appreciate your effort and the time to solve unresponsive hot-reload function when trying to run Vue.js app on Docker container using Docker engine on Windows 10 while WSL2 active, please take a look at below configurations:
Vue.Setup.Dockerfile
FROM node:17-alpine
EXPOSE 8080
WORKDIR /app/frontend
RUN npm --force install -g #vue/cli#4.5.15
COPY /frontend /app/frontend
ENV PATH /app/frontend/node_modules/.bin:$PATH
CMD [ "npm", "run", "serve" ]
docker-compose.yml
version: "3.8"
services:
vue:
build:
context: .
dockerfile: dockerfiles/Vue.Setup.Dockerfile
restart: always
ports:
- "127.0.0.1:8080:8080"
container_name: vue_ui
volumes:
- ./frontend/:/app/frontend/
- /app/frontend/node_modules
environment:
- CHOKIDAR_USEPOLLING=true
vue.config.js
module.exports = {
publicPath:
process.env.NODE_ENV === "production"
? "/static/dist/"
: "http://127.0.0.1:8080",
pages: {
index: {
entry: 'src/main.js',
template: 'public/index.html',
filename: 'index.html',
title: 'QuestionTime',
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
},
// Webpack configuration
devServer: {
host: "0.0.0.0",
port: "8080",
hot: true,
headers: {"Access-Control-Allow-Origin": "*"},
devMiddleware: {
publicPath: "http://127.0.0.1:8080",
writeToDisk: (filePath) => filePath.endsWith("index.html"),
},
static: {
watch: {
ignored: "/node_modules/",
usePolling: true,
},
},
client: {
webSocketURL: {
/* You need to config this option, otherwise the below error will occur
in your browser console when trying to connect to development server
from another Docker container:
WebSocket connection to 'ws://127.0.0.1:<port-number>/ws' failed
*/
hostname: "0.0.0.0",
pathname: "/ws",
port: 8080,
},
},
},
};
Note: When run the command:
docker-compose up
The below message will show:
It seems you are running Vue CLI inside a container.
Since you are using a non-root publicPath, the hot-reload socket
will not be able to infer the correct URL to connect. You should
explicitly specify the URL via devServer.public.
Access the dev server via http://localhost:<your container's
external mapped port>
FYI: the option:
devServer.public
is no longer available in Vue/cli#4 or later versions.
WORKAROUND
solution
Thanks,

Why does my NestJS app try to connect to localhost when it runs inside docker container?

I have a NestJS project (monorepo) and now I'm trying to run all the apps as separate docker containers.
My docker-compose.yml seems like below:
version: '3.8'
services:
app-one:
build:
context: .
dockerfile: ./apps/app-one/Dockerfile
container_name: mono_app-one
ports:
- '3001:3001'
app-two:
build:
context: .
dockerfile: ./apps/app-two/Dockerfile
container_name: mono_app-two
ports:
- '3002:3002'
The Dockerfiles seem like:
FROM node:12.19.0-alpine3.9 AS development
WORKDIR /usr/src
COPY package.json .
RUN npm install --only=development
COPY . .
RUN npm run build --app-one
CMD ["sh", "-c", "npm start --app-one"]
The app.module.ts files seem like:
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: 'development.env',
isGlobal: true,
}),
TypeOrmModule.forRoot({
type: 'mssql',
host: process.env.HOST || 'localhost',
port: parseInt(process.env.PORT, 10) || 1433,
username: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DB_NAME,
entities: [SomeEntity],
synchronize: false,
options: { encrypt: false },
}),
],
...
})
export class AppModule {}
And finaly development.env is:
PORT=1433
HOST=12.34.56.78
USER=admin
PASSWORD=password
DB_NAME=MyDb
When I run docker-compose up --build images are created correctly, containers as well. But when the containers run, I see errors:
Even if I pass port, host, user, pass etc. directly (hardcoded) I see same errors. The question is: Why? In this case I have to look at attempts to connect with 12.34.56.78.
Use ConfigService instead of process.env when initializing TypeOrmModule.
see following doc:
https://docs.nestjs.com/techniques/database#async-configuration
https://docs.nestjs.com/techniques/configuration#using-the-configservice
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('HOST'),
port: +configService.get<number>('PORT'),
username: configService.get('USERNAME'),
password: configService.get('PASSWORD'),
database: configService.get('DATABASE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
inject: [ConfigService],
});

Setting requirepass with args while deploying Redis in k8s works with $() but not with ${}

I am trying to deploy Redis (by creating a Helm chart) as a StatefulSet in Kubernetes cluster. I am not creating another Redis image on top of Official Redis Docker image, rather I am just trying to use the defaults available in Official Redis Docker image and just provide my redis.conf and requirepass at runtime.
To provide redis.conf, I am using a ConfigMap and mounting it in /config/redis.conf in the container.
Now, I want to pass --requirepass option as args in Kubernetes as below:
...
containers: [
{
name: redis,
image: {
repository: redis,
tag: 5.0
},
imagePullPolicy: Always,
workingDir: /data/,
args: [ "/config/redis.conf", "--requirepass", "<password>" ], # line of concern
ports: [
containerPort: 6379
],
env: [
{
name: REDIS_AUTH,
valueFrom: {
secretKeyRef: {
name: redis,
key: password
}
}
}
],
...
The following line fails:
args: [ "/config/redis.conf", "--requirepass", "${REDIS_AUTH}" ]
and on the contrary, this works:
args: [ "/config/redis.conf", "--requirepass", "$(REDIS_AUTH)" ]
Even though, $() syntax is for command substitution and REDIS_AUTH is an environment variable rather than an executable, how does it work and ${REDIS_AUTH} does not?
This is a Kubernetes specific feature that if you want to expand an environment variable in command or args field then you've to use the $() syntax instead of ${} syntax.
Check this link: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#use-environment-variables-to-define-arguments

web-component-tester cannot bind to port

I have a docker setup with following containers:
selenium-hub
selenium-firefox
selenium-chrome
spring boot app
node/java service for wct tests
All these containers are defined via docker-compose.
The node/java service is created as follows (extract from docker-compose):
wct:
build:
context: ./app/src/main/webapp
args:
ARTIFACTORY: ${DOCKER_REGISTRY}
image: wct
container_name: wct
depends_on:
- selenium-hub
- selenium-chrome
- selenium-firefox
- webapp
The wct tests are run using:
docker-compose run -d --name wct-run wct npm run test
And the wct.conf.js looks like following:
const seleniumGridAddress = process.env.bamboo_selenium_grid_address || 'http://selenium-hub:4444/wd/hub';
const hostname = process.env.FQDN || 'wct';
module.exports = {
activeBrowsers: [{
browserName: "chrome",
url: seleniumGridAddress
}, {
browserName: "firefox",
url: seleniumGridAddress
}],
webserver: {
hostname: hostname
},
plugins: {
local: false,
sauce: false,
}
}
The testrun fails with this stacktrace:
ERROR: Server failed to start: Error: No available ports. Ports tried: [8081,8000,8001,8003,8031,2000,2001,2020,2109,2222,2310,3000,3001,3030,3210,3333,4000,4001,4040,4321,4502,4503,4567,5000,5001,5050,5432,6000,6001,6060,6666,6543,7000,7070,7774,7777,8765,8777,8888,9000,9001,9080,9090,9876,9877,9999,49221,55001]
at /app/node_modules/polymer-cli/node_modules/polyserve/lib/start_server.js:384:15
at Generator.next (<anonymous>)
at fulfilled (/app/node_modules/polymer-cli/node_modules/polyserve/lib/start_server.js:17:58)
I tried to fix it as per: polyserve cannot serve the app but without success.
I also tried setting hostnameto wct as this is the known hostname for the container inside the docker network, but it shows the same error.
I really do not know what to do next.
Any help is appreciated.
The problem was that the hostname was incorrect, so WCT was unable to bind to an unknown hostname.

Resources