Running a linux command in Dockerfile throwing error - linux

This is my Dockerfile content and it keeps throwing me error in running this statement. Any idea what am I doing wrong. I just want to dump ENV VARS into a file for React build on the container.
FROM node:12
WORKDIR /usr/src/app
COPY . .
RUN printenv | grep REACT_APP_ > client/.env
# RUN ["printenv", "|", "grep", "REACT_APP_", ">", "client/.env"]
RUN npm run setup
RUN npm run build:all
EXPOSE 3005
CMD [ "npm", "run", "start:prod" ]
Error: ERROR: Service '<name>' failed to build: The command 'printenv | grep REACT_APP_ > client/.env' returned a non-zero code: 1
I have spent many hours with no luck. Help appreciated.

So there's a couple things to consider:
Make sure the client dir exists. It doesn't come in the node:12 image by itself
Make sure the env has a variable that will allow the grep REACT_APP_ to find something. If it does not, grep will return 1 and the docker image build will halt. 1 is normal return code when grep finds nothing, so it's not an error (and thus nothing more is printed). But the docker build treats it as an error since it's nonzero, and thus the build stops.
Also to read on EXIT CODES on man grep page,
EXIT STATUS Normally the exit status is 0 if a line is selected, 1 if
no lines were selected, and 2 if an error occurred.
Here's a test I did that passes (slightly modified, just based on the considerations above):
FROM node:12
WORKDIR /usr/src/app
COPY . .
RUN mkdir client
ENV REACT_APP_1 1
RUN printenv | grep REACT_APP_ > client/.env
RUN cat client/.env
The output I get is this:
$ docker build -t test .
Sending build context to Docker daemon 2.048kB
Step 1/7 : FROM node:12
---> 28faf336034d
Step 2/7 : WORKDIR /usr/src/app
---> Running in 753293fa6257
Removing intermediate container 753293fa6257
---> 3a04798b1b9f
Step 3/7 : COPY . .
---> 3dd0d465a6e2
Step 4/7 : RUN mkdir client
---> Running in d513df2a0a34
Removing intermediate container d513df2a0a34
---> d46aa5200ae7
Step 5/7 : ENV REACT_APP_1 1
---> Running in af81940a90fb
Removing intermediate container af81940a90fb
---> 6169ad694a4d
Step 6/7 : RUN printenv | grep REACT_APP_ > client/.env
---> Running in 365020eeb2e5
Removing intermediate container 365020eeb2e5
---> b6ef574c48c8
Step 7/7 : RUN cat client/.env
---> Running in a6a69d6ba6c2
REACT_APP_1=1
Removing intermediate container a6a69d6ba6c2
---> 0814306133f0
Successfully built 0814306133f0
Successfully tagged test:latest

Related

Docker build showing invalid containerPort

I need help in debugging this image build:
My dockerfile is :
FROM node:latest
WORKDIR /app
COPY . .
ENV PORT = 3000
RUN npm install
EXPOSE $PORT
ENTRYPOINT [ "node", "app.js" ]
and when I run sudo docker build -t uddeshya/node1 . to build the image, the log shows the following:
Sending build context to Docker daemon 2.009MB
Step 1/7 : FROM node:latest
---> dcda6cd5e439
Step 2/7 : WORKDIR /app
---> Using cache
---> 9450405b180f
Step 3/7 : COPY . .
---> Using cache
---> 91689830af35
Step 4/7 : ENV PORT = 3000
---> Using cache
---> d99d55d0ae81
Step 5/7 : RUN npm install
---> Using cache
---> dac4854ec168
Step 6/7 : EXPOSE $PORT
Invalid containerPort: =
The error point is at "EXPOSE $PORT", how do I fix this?
The syntax of ENV should be
ENV PORT=3000
That is the build throw error as it receives = because of spaces.
Step 3/6 : ENV PORT = 3000
---> Using cache
---> 8edc1281a96c
Step 4/6 : RUN echo ${PORT}
---> Running in 090a69369847
= 3000
without spaces it should be
Step 3/6 : ENV PORT=3000
---> Using cache
---> dc29398a0ca6
Step 4/6 : RUN echo ${PORT}
---> Running in 9fc5d9b07342
3000
then yon can verify
docker inspect your_image
you can see
"ExposedPorts": {
"3000/tcp": {}
}

Unable to run NodeJS microservices through Docker containers

I was getting started with Docker, created couple of tiny Express(NodeJS) services.
Plan is to run the microservices inside Docker containers and then establish a inter-communication between them using Docker Compose service names.
Here is the Github repo of this simple project. Am able to build images with below commands :
cd books
docker build -t node-micro/books .
cd auth
docker build -t node-micro/auth .
Commands to start containers :
docker run -d -p 6677:6677 node-micro/auth
docker run -d -p 7766:7766 node-micro/books
But when i hit below URL's there is no response, which was working fine couple of day's before :
http://localhost:6677/
http://localhost:7766/
And have no clue what's happening with docker compose. No luck on accessing same URL's as mentioned above after stoping all containers, delete all images & ran this command :
docker-compose up -d
Need some help on bringing up the containers individually and also through docker-compose.
I can see in each of your micro-service, your application is running on ports 3000 in the container but you are exposing 7766 and 6677 in your docker-compose.yml
Please check the below docker-compose.yml
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3005"
and then run the below command
docker-compose up --build
--build will build the images as well.
Then, you should be able to access the service
http://localhost:6677/
http://localhost:7766/
Output
docker-compose up --build
Creating network "node_microservices_default" with the default driver
Building auth
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Running in a1dc67b70538
Removing intermediate container a1dc67b70538
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 454f1b7aba87
Step 4/7 : RUN npm install
---> Running in a24eea8b79d4
npm WARN auth#1.0.0 No description
npm WARN auth#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 8.58s
found 0 vulnerabilities
Removing intermediate container a24eea8b79d4
---> 31b31ff4516e
Step 5/7 : COPY . .
---> 1eeaa8e70300
Step 6/7 : EXPOSE 3000
---> Running in fc798167dbcd
Removing intermediate container fc798167dbcd
---> 4d964d25c099
Step 7/7 : CMD ["npm", "start"]
---> Running in 3c28d92f9ef6
Removing intermediate container 3c28d92f9ef6
---> 514f68d11d7c
Successfully built 514f68d11d7c
Successfully tagged node_microservices_auth:latest
Building books
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Using cache
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 56addb6c75a5
Step 4/7 : RUN npm install
---> Running in 4864fb7a171c
npm WARN books#1.0.0 No description
npm WARN books#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 5.111s
found 0 vulnerabilities
Removing intermediate container 4864fb7a171c
---> 82bb2cd54357
Step 5/7 : COPY . .
---> 12893a93e82e
Step 6/7 : EXPOSE 3000
---> Running in 1301e29dbd52
Removing intermediate container 1301e29dbd52
---> c26948ebcb3b
Step 7/7 : CMD ["npm", "start"]
---> Running in db948866a121
Removing intermediate container db948866a121
---> 703b901d7bc4
Successfully built 703b901d7bc4
Successfully tagged node_microservices_books:latest
Creating node_microservices_auth_1 ... done
Creating node_microservices_books_1 ... done
Attaching to node_microservices_auth_1, node_microservices_books_1
auth_1 |
auth_1 | > auth#1.0.0 start /usr
auth_1 | > node index.js
auth_1 |
auth_1 | Running on port 3005
auth_1 | --------------------------
books_1 |
books_1 | > books#1.0.0 start /usr
books_1 | > node index.js
books_1 |
books_1 | Running on port 3000
books_1 | --------------------------
You are exposing from both Dockerfiles the port 3000. Replace the port for each microservice in docker-compose.yml file.
- "7766:3000"
- "6677:3000"
The mapping for ports is wrong. In both Dockerfile you are exposing the port 3000.
So you must to map the ports 6677 and 7766 to the exposed port on the Dockerfile.
To fix this, on your docker-compose.yml you must to config ports like this:
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3000"

Docker Container exits upon running with "sh -c"

I am trying to run a webserver (right now still locally) out of a docker container. I am currently going step by step to understand the different parts.
Dockerfile:
FROM node:12.2.0-alpine as build
ENV environment development
WORKDIR /app
COPY . /app
RUN cd /app/client && yarn && yarn build
RUN cd /app/server && yarn
EXPOSE 5000
CMD ["sh", "-c","NODE_ENV=${environment}", "node", "server/server.js"]
Explanation:
I have the "sh", "-c" part in the CMD command due to the fact that without it I was getting this error:
docker: Error response from daemon: OCI runtime create failed:
container_linux.go:346: starting container process caused "exec:
\"NODE_ENV=${environment}\": executable file not found in $PATH":
unknown.
Building the container:
Building the container works just fine with:
docker build -t auth_example .
It takes a little while since the build context is (even after excluding all the node_modules) roughly 37MB, but that's okay.
Running the container:
Running the container and the app inside works like a charm if I do:
MyZSH: docker run -it -p 5000:5000 auth_example /bin/sh
/app # NODE_ENV=development node server/server.js
However, when running the container via the CMD command like this:
MyZSH: docker run -p 5000:5000 auth_example
Nothing happens, no errors, no nothing. The logs are empty and a docker ps -a reveals that the container was exited right upon start. I did some googling and tried different combinations of -t -i -d but that didn't solve it either.
Can anybody shed some light on this or point me into the right direction?
The problem is you're passing three arguments to sh -c whereas you'd usually pass one (sh -c "... ... ...").
It's likely you don't need the sh -c invocation at all; use /usr/bin/env to alias that environment variable instead (or just directly pass in NODE_ENV instead of environment):
FROM node:12.2.0-alpine as build
ENV environment development
WORKDIR /app
COPY . /app
RUN cd /app/client && yarn && yarn build
RUN cd /app/server && yarn
EXPOSE 5000
CMD /usr/bin/env NODE_ENV=${environment} node server/server.js

What happens if I write RUN node index.js instead of CMD node index.js in a dockerfile?

I made a simple hello world node.js application with the dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD node index.js
If I change the CMD to RUN in my dockerfile it still works.
It is documented in dockerfile to use CMD as it will then start the node server upon the running of the container.
I would like to know what will happen underhood if I use RUN cmd instead of CMD.
Basically what happens if I make a docker image which itself is in running state.
RUN will execute a command during the build process. CMD is used as the default command when executing a container, as opposed to building. If you run node index.js in a RUN instruction, your build will never finish and you don't have a container to share with others.
Refer to the dockerfile documentation for more detail: RUN and CMD.
Relevant bits from that documentation:
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
The main purpose of a CMD is to provide defaults for an executing container.
EDIT: using OP's index.json, package.json, and Dockerfile files, the docker image build does not complete when using RUN node index.js and does complete (as expected) when using CMD node index.js.
Contents of index.js:
//Load express module with `require` directive
var express = require('express')
var app = express()
//Define request response in root URL (/)
app.get('/', function (req, res) {
res.send('Hello World!')
})
//Launch listening server on port 8081
app.listen(8080, function () {
console.log('app listening on port 8080!')
})
Contents of package.json:
{
"name": "dummy_nodejs_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "Debojit",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
When using the Dockerfile as follows:
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
RUN node index.js
then the build hangs. Here is the output:
jakub#dash:/tmp/test-node$ docker build -t test .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM node:10
---> d5680e53a228
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> a4b4547833e5
Step 3/7 : COPY package*.json ./
---> Using cache
---> 2b19cc3e48a3
Step 4/7 : RUN npm install
---> Using cache
---> fe1f1e72d17d
Step 5/7 : COPY . .
---> eb6fe0e3d1a7
Step 6/7 : EXPOSE 8080
---> Running in e573b923fcb2
Removing intermediate container e573b923fcb2
---> b3590153eed7
Step 7/7 : RUN node index.js
---> Running in 08b408e6e6f3
app listening on port 8080!
This hangs indefinitely.
When using the Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD node index.js
the build output is:
jakub#dash:/tmp/test-node$ docker build -t test .
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM node:10
---> d5680e53a228
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> a4b4547833e5
Step 3/7 : COPY package*.json ./
---> Using cache
---> 2b19cc3e48a3
Step 4/7 : RUN npm install
---> Using cache
---> fe1f1e72d17d
Step 5/7 : COPY . .
---> Using cache
---> fc036f428e34
Step 6/7 : EXPOSE 8080
---> Using cache
---> d1ede7276d34
Step 7/7 : CMD node index.js
---> Using cache
---> cf051929395b
Successfully built cf051929395b
Successfully tagged test:latest
The RUN step executes a temporary container with the provided command, waits for that command to exit, and then captures to changes to the container filesystem as another layer of the resulting image. It does not store the running processes, changes to environment variables, or any changes to the state of the shell since those are not written to the filesystem. It also does not capture changes to volumes since temporary containers are started with the volumes defined in the image, and changes to a volume are not applied the container filesystem. This is a build time step.
The CMD step replaces the existing default command that docker runs when the image is run as a container. Containers exist for as long as this command is running, and you can only have a single value for the command. If you define CMD a second time, the previous value is replaced. And if you start a container with an overridden command, the image value for CMD is ignored.
Therefore you want to separate the steps to modify the filesystem at image build time from the steps to perform when the container is run into RUN and CMD respectively.
First of all, if you start any long-running process in build stage with RUN command your build process will be stuck.
The RUN command execute at build time and this is designed for build-time configuration and installation packages and tools, with RUN command your prepare your Docker image, for example installing npm modules and some other application dependency that will be available when the process is up in the container.
The CMD execute when you start the container, it does not execute at build time, CMD should be a long-running process to keep your container.

Docker multi-stage build: COPY --from=builder failed: no such file or directory

The following Dockerfile
FROM nvidia/cuda:10.2-devel-ubuntu18.04 AS builder
...
RUN ls $HOME/bin
FROM nvidia/cuda:10.2-runtime-ubuntu18.04
COPY --from=builder $HOME/bin/ffmpeg /usr/local/bin/ffmpeg
returns
Step 21/23 : RUN ls $HOME/bin
---> Running in a8dee059d9a6
ffmpeg
Removing intermediate container a8dee059d9a6
---> 1cb71814a43b
Step 22/23 : FROM nvidia/cuda:10.2-runtime-ubuntu18.04
---> e442a6c5cd9a
Step 23/23 : COPY --from=builder $HOME/bin/ffmpeg /usr/local/bin/ffmpeg
COPY failed: stat /var/lib/docker/overlay2/b9283a57527d878d76ea8f8a1e87eb1a6466e849ee1cb4bc69601e184f6dea1f/merged/bin/ffmpeg: no such file or directory
Am I missing something? Is it a bug in docker?
You cannot use $HOME (or any environment variable) in the COPY operation. You have details in this issue https://github.com/moby/moby/issues/34482
Simple test:
This works:
FROM ubuntu AS builder
RUN echo "fooo" > ${HOME}/test.txt
FROM ubuntu
COPY --from=builder /root/test.txt /tmp/test.txt
RUN cat /tmp/test.txt
This doesn't work, same error as yours:
FROM ubuntu AS builder
RUN echo "fooo" > ${HOME}/test.txt
FROM ubuntu
COPY --from=builder ${HOME}/test.txt /tmp/test.txt
RUN cat /tmp/test.txt

Resources