Docker: Docker run does not report error but docker file has errors, how to view them? - node.js

Here is my docker file a very simple docker file
FROM node:19-alpine
COPY package.json /app/
COPY src /app/
WORKDIR /app
RUN npm install
RUN ls
CMD [ "node", "src/index.js" ] //it has error, it should be index.js
When I try to execute docker build command it shoes the container id however docker ps does not show any containers, which means container was not launched successfully and there is problem with docker file.
the error is reported if I try to run container from docker client GUI.
How to view the errors from command line if docker file has a problem and it wasn't reported by docker build command?

docker ps only shows running containers.
If you do docker ps -a you should see the container and that it has the 'exited' status.
You can then do docker logs <container name> to see any error messages.

You need first to be clear on these two points:
1. docker file is used to build an image.
Use this command to build an image
you can also refer here
docker build -t node:test --progress=plain .
if run successly, your will get the image ID
2. but you also need to start a container based by this image.
docker run -itd --name=node-test node:test
docker ps | grep node-test
Also, check this container if or not based by your image

Related

Docker buildx with node app on Apple M1 Silicon - standard_init_linux.go:211: exec user process caused "exec format error

Please help!
I am trying to deploy a docker image to a kuebernetes clusters. No problem until I switched to new Macbook Pro with M1.
Once I build the image on the m1 machine and deploy I get the following error from the kuebernetes pod:
standard_init_linux.go:211: exec user process caused "exec format error"
After doing some research, I followed this medium post on getting docker buildx added and set up.
Once I build a new image using the new buildx and run it locally using the docker desktop (the m1 compatible preview version), it runs without issue. However the kubernetes pod still shows the same error.
standard_init_linux.go:211: exec user process caused "exec format error"
My build command
docker buildx use m1_builder && docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6 -f Dockerfile -t ${myDockerRepo} --push . '
During the build I see each platform logging out that it is running the commands from my Dockerfile.
My push command
docker push ${myDockerRepo}
One odd thing to note is the sha256 digest in the docker push command response does not change.
Here is my docker file:
# Use an official Node runtime as a parent image
FROM node:10-alpine
# Copy the current directory contents into the container at /app
COPY dist /app
# Set the working directory to /app
WORKDIR /app
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Run npm run serve:dynamic when the container launches
CMD ["node", "server"]
I am no docker expert, clearly.
Started with a full head of hair. Down to 3 strands. Please save those 3 strands.
I appreciate all help and advice!
Update
I have pulled the image built by the M1 macbook down to my other macbook and could run the image locally via docker desktop. I am not sure what this means. Could it be just a kuebernetes setting?
Try adding --platform=linux/amd64 to your dockerfile:
FROM --platform=linux/amd64 node:10-alpine

docker run not working: no container created despite making container image

Summary
docker run doesn't seem to build a container (but it also doesn't throw an error) despite docker build successfully building the container image.
Input and Output
1. Successful docker image creation..
$ docker build -t minitwitter:latest .
...
Successfully built da191988e0db
Successfully tagged minitwitter:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minitwitter latest da191988e0db 6 seconds ago 173MB
python 3.7-alpine b11d2a09763f 9 days ago 98.8MB
2. ..and docker run completes without error..
$ docker run --name minitwitter -d -p 8000:5000 --rm minitwitter:latest
e8835f1b4c72c8e1a8736589c74d56ee2d12ec7bcfb4695531759fb1c2cf0e48
3. ..but docker container doesn't seem to exist.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
And navigating to the port where the app should be exposed, http://localhost:8000, returns the connection error ERR_CONNECTION_REFUSED.
Docker file, boot.sh
The Dockerfile and boot.sh files are pretty simple I think:
Dockerfile
FROM python:3.7-alpine
RUN adduser -D minitwitter
WORKDIR /home/minitwitter
COPY requirements.txt requirements.txt
RUN python -m venv env
RUN env/bin/pip install -r requirements.txt
RUN env/bin/pip install gunicorn
COPY app app
COPY migrations migrations
COPY minitwitter.py config.py boot.sh ./
RUN chmod a+x boot.sh
ENV FLASK_APP minitwitter.py
RUN chown -R minitwitter:minitwitter ./
USER minitwitter
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]
boot.sh
# BOOTS A DOCKER CONTAINER
#!/bin/sh
source env/bin/activate
flask db upgrade
exec gunicorn -b :5000 --access-logfile - --error-logfile - minitwitter:app
Place the 'shebang' -- #!/bin/sh -- on the first line of the boot.sh shell script.
How I found this answer: This blog post which refers to this Stackoverflow post.
The problem: the original script has a comment on the first line and the shebang on the second line.
Note: The title of the 'Question' is misleading: a docker container was built. The container, however, was short-lived and given I used the -rm option in the docker run command, the container was deleted after it terminated within 2 seconds; this is why it didn't appear in the docker images -a command.

How to launch a Docker container that i've got from another person?

I am a Docker-newbie and I've got a project from another developer including a Dockerfile. This shall give me the Virtual Machine to continue work with the (nodeJS-) project inside this project folder.
Docker is already installed on my machine.
How can I launch this container now?
I've read about a command
sudo docker run -name my_first_instance
but i can't find any container name in the Dockerfile.
The dockerfile will create an image for you that you can launch containers from. this being said , Follow this:
Create a folder.
Copy dockerfile in the folder
cd into the folder execute the following command:
docker build -t <your desired image name> .
This will create an image using directives in the dockerfile in the current folder.
Now launch a container from the image.
docker run -d --name <your container name> <imagename from previous step> <optional startup commands>
Useful docker commands:
You can expose ports in the previous command using -p switch.
You can list Images via docker images
You can list running containers via docker ps
you can list running + exited containers via docker ps -a
have a look at
https://hub.docker.com/_/node/
it is the official repository for NodeJS docker images
If you want some docker images based on NodeJS, you will need to pull them docker pull my_node_image
Then you can launch one with such a command
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/app -w /usr/src/app node:4 node your-daemon-or-script.js
The Dockerfile is just the recipe to build a docker image, for Nginx, Mysql, MongoDb, Redis, Wordpress, Spotify, atop, htop...
If docker images shows nothing, it means you have not yet pulled any docker image.

Start node app when running docker container from cli

I'm relatively new to Docker and have a node web server which I have added to a docker image. My image is built using packer, so I don't have a Dockerfile.
My question is when running the docker container on the command line with docker run -it -d <imageId> is there a way to pass in the command to run my web server that resides in the container?
So something like docker run -it -d <imageId> npm start
Got it working with
docker run -it -d -w /path/to/code/folder <imageName:version> node server.js 'daemon off;'

docker run container, how to rerun

I build container with:
docker build -f Dockerfile.xyz -t dave/xyz .
after that I run docker with:
docker run -it \
--env='LDAP_USER=uid=bot_for_git,ou=bots,dc=company,dc=org' \
--env='LDAP_PASS=' --volume=/srv/docker/xyz/data1:/data \
-p 8010:8010 -p 9989:9989 dave/xyz
and verified that's all ok.
what is next?
My guess, that I should run docker ps, take container id from there, and to run container with the same preferences (environment, port mapping, volumes mapping) I should run:
docker start -a container_id
am I right?
And what about rebuilding image, if change Dockerfile.xyz and rebuild dave/xyz, does container with container_id get
update automatically, or I should repeat docker run -it step?
docker build [...] creates an image. You can see your images with docker images. You may give that image a specific name with the --tag=[...] option:
docker build --tag="superuser/bestimage:latest" .
docker run [...] <imageId> takes that image and starts a container. You can see active containers with docker ps (all with docker ps -a). If you used the tag above, docker run -it superuser/bestimage:latest may be used.
When you rebuild an image, a new image with a new id is created. You may see that through docker images.
does container with container_id get update automatically
No. In order to update your container, you must first remove the container with docker kill <id> and then start a new one with docker run -it <newID>.
Your initial guess
docker start -a container_id
is close but to be able to interact with the container's terminal, include the -i option, as follows:
docker start -ai container_id

Resources