Packer: create docker image with ubuntu and nginx - packer

I have the following Dockerfile:
FROM ubuntu:bionic
RUN apt-get update -y && apt-get install -y nginx
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
Building the container and running it through the following commands works:
$ docker build -t web:1.0 .
$ docker run -itd -p 8200:80 --name webserver1 web:1.0
I made an equivalent Packer file as follows:
packer {
required_plugins {
docker = {
version = ">= 1.0.1"
source = "github.com/hashicorp/docker"
}
}
}
source "docker" "ubuntu" {
image = "ubuntu:bionic"
commit = true
changes = [
"EXPOSE 80",
"CMD [\"/usr/sbin/nginx\", \"-g\", \"daemon off;\"]"
]
}
build {
name = "learn-packer"
sources = [
"source.docker.ubuntu"
]
provisioner "shell" {
inline = [
"apt-get -y update",
"apt-get install -y nginx",
]
}
post-processors {
post-processor "docker-tag" {
repository = var.docker_repo
tags = ["1.1"]
}
}
}
I'm creating the image with packer build . When I run the image through Docker
docker run -itd -p 8201:80 --name webserver2 xxx/web:1.1
I get the following error:
usr/sbin/nginx: 1: usr/sbin/nginx: Syntax error: ")" unexpected

Related

audiowaveform error (No such file or directory #include "gmock/gmock.h") while installing on aws elastic beanstalk

I have been installing and using audiowaveform on AWS Elastic Beanstalk via an ebextensions but for some reason I now get an error during the build process:
...
[ 72%] Building CXX object CMakeFiles/audiowaveform_tests.dir/src/WavFileWriter.cpp.o
[ 73%] Building C object CMakeFiles/audiowaveform_tests.dir/src/madlld-1.1p1/bstdfile.c.o
[ 75%] Building CXX object CMakeFiles/audiowaveform_tests.dir/test/FileFormatTest.cpp.o
/opt/src/audiowaveform/audiowaveform-master/test/FileFormatTest.cpp:26:25:
fatal error: gmock/gmock.h: No such file or directory #include "gmock/gmock.h" ^ compilation terminated.
make[2]: *** [CMakeFiles/audiowaveform_tests.dir/test/FileFormatTest.cpp.o] Error 1
make[1]: *** [CMakeFiles/audiowaveform_tests.dir/all] Error 2 make: *** [all] Error 2.
Below is the ebextension that I have been using:
packages:
yum:
make: []
cmake: []
gcc-c++: []
gd-devel: []
boost-devel: []
sources:
/opt/src/audiowaveform: https://github.com/bbc/audiowaveform/archive/master.zip
/opt/src/googletest: https://github.com/google/googletest/archive/release-1.10.0.tar.gz
/opt/src/libmad: https://sourceforge.net/projects/mad/files/libmad/0.15.1b/libmad-0.15.1b.tar.gz/download
/opt/src/libsndfile: http://www.mega-nerd.com/libsndfile/files/libsndfile-1.0.28.tar.gz
/opt/src/libid3tag: https://sourceforge.net/projects/mad/files/libid3tag/0.15.1b/libid3tag-0.15.1b.tar.gz/download
files:
/usr/lib/pkgconfig/mad.pc:
mode: "000755"
owner: root
group: root
content: |
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: mad
Description: MPEG audio decoder
Requires:
Version: 0.15.1b
Libs: -L${libdir} -lmad
Cflags: -I${includedir}
/etc/ld.so.conf.d/libsndfile:
mode: "000644"
owner: root
group: root
content: |
/usr/lib/
commands:
00-install-libmad-make:
command: "sed -i '/-fforce-mem/d' configure"
cwd: /opt/src/libmad/libmad-0.15.1b/
01-install-libmad-make:
command: "./configure --prefix=/usr && make && make install"
cwd: /opt/src/libmad/libmad-0.15.1b
test: "[ ! -f /usr/local/bin/audiowaveform ]"
02-install-libsndfile-make:
command: "./configure --prefix=/usr --disable-static --docdir=/usr/share/doc/libsndfile-1.0.28 && make && make install"
cwd: /opt/src/libsndfile/libsndfile-1.0.28
test: "[ ! -f /usr/local/bin/audiowaveform ]"
03-install-libid3tag-make:
command: "./configure --prefix=/usr && make && make install"
cwd: /opt/src/libid3tag/libid3tag-0.15.1b
test: "[ ! -f /usr/local/bin/audiowaveform ]"
04-change-mod-audiowaveform:
command: "chmod -R 755 audiowaveform-master"
cwd: /opt/src/audiowaveform
05-audiowaveform-ln-test:
command: "ln -s ../../googletest/googletest-release-1.10.0/googletest ./googletest"
cwd: /opt/src/audiowaveform/audiowaveform-master/
test: "[ ! -L ./googletest ]"
06-audiowaveform-ln-mock:
command: "ln -s ../../googletest/googletest-release-1.10.0/googlemock ./googlemock"
cwd: /opt/src/audiowaveform/audiowaveform-master/
test: "[ ! -L ./googlemock ]"
07-audiowaveform-mkdir-build:
command: "sudo mkdir build"
cwd: /opt/src/audiowaveform/audiowaveform-master/
test: "[ ! -d ./build ]"
08-audiowaveform-cmake:
command: "cmake .."
cwd: /opt/src/audiowaveform/audiowaveform-master/build/
test: "[ ! -f /usr/local/bin/audiowaveform ]"
09-audiowaveform-make:
command: "make"
cwd: /opt/src/audiowaveform/audiowaveform-master/build/
test: "[ ! -f /usr/local/bin/audiowaveform ]"
10-audiowaveform-make-install:
command: "make install"
cwd: /opt/src/audiowaveform/audiowaveform-master/build/
test: "[ ! -f /usr/local/bin/audiowaveform ]"
11-ldconfig:
command: "ldconfig"
Anyone know of something that has changed? So far I haven't found anything.
Do I need to use a different process? I am really not sure what to do next. I have created an new application and environment and tried deploying to it to make sure I haven't messed up the current one but I get the same error.
Well it looks like it is a version issue. This was on our dev environment and found out it was trying to install version 1.5.1 and our production version is on 1.4.2. So I changed the
/opt/src/audiowaveform: https://github.com/bbc/audiowaveform/archive/master.zip
to
/opt/src/audiowaveform: https://github.com/bbc/audiowaveform/archive/refs/tags/1.4.2.zip
and changed all the references to audiowaveform-master to audiowaveform-1.4.2 in the commands below and it seems to work.
Currently we are on a deprecated platform so am transitioning to a supported platform at the same time so I haven't made sure that it works completely but at least it builds it.

How to access external database from Node container?

I have a nextjs app which is supposed to connect to an external MySQL database (not one from the same docker network). When running the app locally, it works correctly when connecting to DB, but when running it in a Docker container it keeps on trying to connect to 127.0.0.1, even though the environment variables are configured correctly in the container
Error: Error: connect ECONNREFUSED 127.0.0.1:3306
nextjs_1 | at connect (/opt/app/node_modules/serverless-mysql/index.js:80:15)
Dockerfile config:
FROM node:alpine
RUN mkdir -p /opt/app
RUN apk add --no-cache libc6-compat
ENV NODE_ENV production
ENV PORT 3000
EXPOSE 3000
WORKDIR /opt/app
COPY package.json /opt/app
COPY package-lock.json /opt/app
RUN npm install --no-optional
COPY . /opt/app
RUN npm run build
RUN npx next telemetry disable
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
USER nextjs
CMD [ "npm", "start" ]
Connection code:
const mysql = require('serverless-mysql')
const db = mysql({
config: {
host: process.env.MYSQL_HOST,
database: process.env.MYSQL_DATABASE,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
},
})
exports.query = async (query) => {
try {
const results = await db.query(query)
await db.end()
return results
} catch (error) {
return { error }
}
}
Any ideas?

Connection refused when running October artisan command with docker exec in Jenkins

I'm trying to execute an OctoberCMS migration through Jenkins with a docker command, but I have the next error in Jenkins console:
+ docker exec -u jenkins dockerfilemaster_app_1 php artisan october:up
Migrating application and plugins...
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
information_s
chema.tables where table_schema = databasename and table_name =
migrations)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] Connection refused
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
This is the declarative Jenkinsfile I'm using (the "ping" command is to ensure the database is reachable):
pipeline {
agent any
stages {
stage('Composer') {
steps {
script {
sh '''
chmod -R 777 storage
composer install
docker-compose up --build -d
'''
}
}
}
stage('Docker') {
steps {
script {
sh '''
docker exec dockerfilemaster_app_1 useradd jenkins
docker exec -u jenkins dockerfilemaster_app_1 ping -c 2 database
docker exec -u jenkins dockerfilemaster_app_1 php artisan october:up
docker exec -u jenkins dockerfilemaster_app_1 php artisan october:down --force
'''
}
}
}
}
post {
always {
sh '''
docker-compose down
'''
}
}
}
But if I connect to the Jenkins server with SSH, and I execute the same script, It works:
docker-compose up --build -d
docker exec dockerfilemaster_app_1 useradd jenkins
docker exec -u jenkins dockerfilemaster_app_1 php artisan october:up
The database config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'database'),
'port' => env('DB_PORT', 3306),
'database' => env('DB_DATABASE', 'databasename'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
The docker-compose.yml file:
version: '2'
services:
web:
build:
context: ./
dockerfile: web.docker
volumes:
- ./:/var/www/public
ports:
- "8081:80"
links:
- app
app:
build:
context: ./
dockerfile: app.docker
volumes:
- ./:/var/www
links:
- database
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
database:
image: mysql:5.6
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=databasename"
ports:
- "3306:3306"
I can't see why If I execute the script in the server connected with SSH works, but It does not works If Jenkins execute the same script.
This is the whole Jenkins console output:
Branch indexing
> git rev-parse --is-inside-work-tree # timeout=10
Setting origin to https://gitlab.com/[...]
> git config remote.origin.url https://gitlab.com/[...] # timeout=10
Fetching origin...
Fetching upstream changes from origin
> git --version # timeout=10
> git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --tags --progress origin +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/develop
Seen branch in repository origin/master
Seen 2 remote branches
Obtained Jenkinsfile from e40e597c1a6bb87221f6c0844174534b5a5a4e15
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/docker-file_master
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://gitlab.com/[...] # timeout=10
Fetching without tags
Fetching upstream changes from https://gitlab.com/[...]
> git --version # timeout=10
using GIT_ASKPASS to set credentials
> git fetch --no-tags --progress https://gitlab.com/[...] +refs/heads/*:refs/remotes/origin/*
Checking out Revision e40e597c1a6bb87221f6c0844174534b5a5a4e15 (master)
> git config core.sparsecheckout # timeout=10
> git checkout -f e40e597c1a6bb87221f6c0844174534b5a5a4e15
Commit message: "Testing with env"
> git rev-list --no-walk 1b46891b6e4078d426f8a33dc5fc1f6b2a743f56 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Composer)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ whoami
jenkins
+ chmod -R 777 storage
+ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
+ docker-compose up --build -d
Creating network "dockerfilemaster_default" with the default driver
Building app
Step 1/4 : FROM php:7.1-fpm
---> 894f8d826f6a
Step 2/4 : RUN apt-get update && apt-get install -y apt-utils mcrypt libmcrypt-dev mysql-client git zip unzip zlib1g-dev iputils-ping && docker-php-ext-install pdo_mysql mysqli zip
---> Using cache
---> 0881c5505ea5
Step 3/4 : COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
---> Using cache
---> d1f1d74f8b0f
Step 4/4 : WORKDIR /var/www
---> Using cache
---> 60aefdca48ba
Successfully built 60aefdca48ba
Successfully tagged dockerfilemaster_app:latest
Building web
Step 1/3 : FROM nginx:1.10
---> 0346349a1a64
Step 2/3 : ADD ./vhost.conf /etc/nginx/conf.d/default.conf
---> Using cache
---> 74cadd23348a
Step 3/3 : WORKDIR /var/www
---> Using cache
---> 4142e3e3d33a
Successfully built 4142e3e3d33a
Successfully tagged dockerfilemaster_web:latest
Creating dockerfilemaster_database_1
Creating dockerfilemaster_app_1
Creating dockerfilemaster_web_1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Docker)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ docker exec dockerfilemaster_app_1 useradd jenkins
+ docker exec -u jenkins dockerfilemaster_app_1 whoami
jenkins
+ docker exec -u jenkins dockerfilemaster_app_1 ping -c 2 database
PING database (172.26.0.2) 56(84) bytes of data.
64 bytes from dockerfilemaster_database_1.dockerfilemaster_default (172.26.0.2): icmp_seq=1 ttl=64 time=0.158 ms
64 bytes from dockerfilemaster_database_1.dockerfilemaster_default (172.26.0.2): icmp_seq=2 ttl=64 time=0.198 ms
--- database ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.158/0.178/0.198/0.020 ms
+ docker exec -u jenkins dockerfilemaster_app_1 php artisan october:up
Migrating application and plugins...
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s
chema.tables where table_schema = databasename and table_name = migratio
ns)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] Connection refused
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Required)
Stage "Required" skipped due to earlier failure(s)
[Pipeline] parallel
[Pipeline] { (Branch: editorConfig)
[Pipeline] stage
[Pipeline] { (editorConfig)
Stage "editorConfig" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
Failed in branch editorConfig
[Pipeline] // parallel
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] sh
+ docker-compose down
Stopping dockerfilemaster_web_1 ...
Stopping dockerfilemaster_app_1 ...
Stopping dockerfilemaster_database_1 ...
[3A[2K
Stopping dockerfilemaster_web_1 ... done
[3B[2A[2K
Stopping dockerfilemaster_app_1 ... done
[2B[1A[2K
Stopping dockerfilemaster_database_1 ... done
[1BRemoving dockerfilemaster_web_1 ...
Removing dockerfilemaster_app_1 ...
Removing dockerfilemaster_database_1 ...
[2A[2K
Removing dockerfilemaster_app_1 ... done
[2B[3A[2K
Removing dockerfilemaster_web_1 ... done
[3B[1A[2K
Removing dockerfilemaster_database_1 ... done
[1BRemoving network dockerfilemaster_default
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Hmm seems when application is installing meanwhile DB instance is not able to start so may be we can add depends_on. database is always create problem in this environments.
version: '2'
services:
web:
build:
context: ./
dockerfile: web.docker
volumes:
- ./:/var/www/public
ports:
- "8081:80"
links:
- app
depends_on: <= THIS
- "app" <= N THIS
app:
build:
context: ./
dockerfile: app.docker
volumes:
- ./:/var/www
links:
- database
depends_on: <= THIS
- "database" <= N THIS
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
database:
image: mysql:5.6
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=databasename"
ports:
- "3306:3306"
may be it can solve your issue. if NOT then you need to check tools suggested in reference below will help you. as they also mention DB creates problem in docker environment.
You can check reference from here : https://docs.docker.com/compose/startup-order/
if any doubt please comment.
Adding the "sleep" command worked for me. Seems that Docker is still not build when the second stage (the stage named "Docker) starts. I thought the stage starts when the previous stage It's fully finished.
This is the final Jenkins file:
pipeline {
agent any
stages {
stage('Docker') {
steps {
script {
sh '''
chmod -R 777 storage
docker-compose up --build -d
sleep 30
docker exec dockerfilemaster_app_1 useradd jenkins
docker exec dockerfilemaster_app_1 chmod -R 777 /var/www
docker exec -u jenkins dockerfilemaster_app_1 composer install
docker exec -u jenkins dockerfilemaster_app_1 php artisan october:up
docker exec -u jenkins dockerfilemaster_app_1 php artisan october:down --force
'''
}
}
}
}
post {
always {
sh '''
docker-compose down
'''
}
}
}
I put together the "docker-compose up" command with the rest of docker commands, due to putting that command in another stage did not help.

I am trying to update the output of sudo knife node edit fqdn -c /etc/chef/client.rb using bash script

Here is the command that I run:
sudo knife node edit fqdn -c /etc/chef/client.rb . --> hit enter button then shows below output :
{
"name": "test",
"chef_environment": "standard_chef_environment",
"normal": {
"httpd": {
"fips_mode_enable": "false"
},
"enable_fips_mode": false,
"props": {
So i wanted to add few line under props using following command but its getting failed :
sudo knife node edit fqdn -c /etc/chef/client.rb |jq ‘.props |= . + { "ParameterKey": "Foo4", "ParameterValue": "Bar4" }'
The props key is nested under normal so you would need .normal.props or similar.

In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

in node's package.json I would like to reuse a command that I already have in a 'script'.
Here is the practical example
instead of (note the extra -w on the watch script):
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list -w",
}
I would like to have something like
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run script test" + "-w",
}
which doesn't work (can't do string concats in json), but you should get what I would like
I know that npm scripts support:
- & (parallel execution)
- && (sequencial execution)
so maybe there is another option?
This can be done in npm#2.1.17. You don't specify your OS and the version of npm that you are using, but unless you have done something to update it, you are probably running npm#1.4.28 which does not support the syntax below.
On Linux or OSX you can update npm with sudo npm install -g npm#latest. See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm for a guide to updating npm on all platforms.
You should be able to do this by passing an additional argument to your script:
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run test -- -w"
}
I verified this using the following, simplified package.json:
{
"scripts": { "a": "ls", "b": "npm run a -- -l" }
}
Output:
$ npm run a
> # a /Users/smikes/src/github/foo
> ls
package.json
$ npm run b
> # b /Users/smikes/src/github/foo
> npm run a -- -l
> # a /Users/smikes/src/github/foo
> ls -l
total 8
-rw-r--r-- 1 smikes staff 55 4 Jan 05:34 package.json
$