Debug dockerized nodejs application on startup - node.js

I have a setup of containers running ( docker-compose ) and one with a nodejs application running inside of it. Currently i debug the application by connecting via VS Code to the debug port (9229) of the application. The problem with this approach is that i can't connect to the application on startup. If the error is on some event like an http connection that is no problem, but if i want to check the initialisation process the process is already running for some time until i can connect so the process ran past my debug points.
Is there a solution to this?

Run the following commands to find the running container and navigate into the container...
List all Docker images: docker image ls
View contents of a running Docker container: docker exec -it <container-id> bash
once inside the container, then you can stop the node process inside the container and start by node app.js where you will be able to see the logs from initialisation Or if you have a logs file then there aswell you can check.
The basic idea here is to navigate inside the docker container and then its like running node server like how you would run normally from ay linux terminal.

Related

Docker is not pointing to existing containers

I'm on Ubuntu and I am using docker for my Laravel application but after the restart, I can't execute commands like docker-compose up -d, docker-compose restart, docker-compose exec backend PHP artisan migrate anymore. I checked other solutions like killing containers running on that specific port but the problem keeps coming back. What seems to be the problem?
whenever I try docker-compose up -d, this is the result:
Error Logs
My application is completely running, but somehow I can't execute Laravel artisan commands so I tried restarting it, but it's pointing to these containers instead of the ones that are currently running:
Containers

uWSGI server does not run with & argument

I build a docker image which runs flask on an uWSGI server. I have a uwsgi.ini file with configuration. When I use
CMD ["uwsgi", "--ini", "uwsgi.ini"]
in the Dockerfile, everything works fine and the server runs, I can access my flask app via the browser. The only problem is that I cannot use the container's bash while it's running, as it is blocked by the uWSGI process. I found that appending an & should make the uWSGI run in the background. However, when I use
CMD ["uwsgi", "--ini", "uwsgi.ini", "&"]
I get an error saying
unable to load configuration from &
I get that when I try this, the uWSGI thinks I'm passing another argument that it should process. However, I cannot find any way to tell it that it is not the case. Using docker run with the -d argument also only detaches the container from the current terminal on the host, but when I use docker attach, I get a bash that I can't do anything with.
Is there a way to tell uWSGI explicitly that I want it to run in the background? Am I missing something?
You can execute a command on your container by using the exec command.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9fb488c77d55 nginx "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 80/tcp distracted_brown
so here I have an nginx container image running in a container called distracted_brown. So I can ask the container to run a command using exec. In this case the command i want to run is the shell sh. I also pass the -it flag so i can run interactivly with STDIN and STDOUT
docker container exec -it distracted_brown sh
this will allow me shell access to the container where nginx is running as PID 1. As a side note you dont normally want to run your CMD process in the background as when PID 1 closes the container will close.

Can I use restarting docker instead restarting app in container?

I run app in Docker container. I didn't bundle the app's code into image, but using -v to map the code into container in order to upgrade the code more conveniently.
I used to use pm2 to manage the process, and when I upgrade the code, I use docker exec -it app bash to go into the container and run pm2 restart.
But now I didn't use pm2 any more, just run node app.js. When I upgraded the code and need to restart the app, I run docker restart to restart the container directly.
If there is any side effect of docker restart? Or is there a better way to restart a node app?
Doing a docker restart will just restart the node process in your container, not much. So there's no side effect.

How to run Node.js and MongoDB interactive shell simultaneously within a Docker container

I have a Docker image configured with node.js (with express) and mongoDB.
I run the mongod service in the background: mongod --fork --logpath /var/lib/mongodb.log. I start my node.js app: npm start which results in an interactive shell(shows the requests to server).
But if I want to monitor the DB changes being made by my node.js application, each time I am forced to stop the node server (ctrl + c) and launches the mongoDB interactive shell using: mongo.
So the next time if I want to run my node.js app, I had to stop the mongoDB interactive shell (ctrl + c) and run the server all over again.
Is there any way to run both node.js interactive shell and mongoDB interactive shell simultaneously, may be in two different terminal window in Docker ?
The image below shows the snapshot of my terminal.
I am using Ubuntu 15.04 and Docker version 1.5.0, build a8a31ef
I would suggest not running these services in the same container. Run each one in a separate container and use docker-compose to manage building and running the containers.
docker-compose logs will show you the output of each service.
Managing the services in separate containers will let you modify each independently, and gives you an environment that is closer to a production setup.
I would recommend you try installing tmux. You can add the following to your Dockerfile to make tmux available in the container:
RUN apt-get update && apt-get install -y tmux
tmux will provide you with a screen that can represent multiple windows with multiple panes, handling the I/O for you.
Alternatively, you can use CTRL+Z, fg, bg, to change the process your viewing in the foreground. A final solution might be to run docker exec in two separate terminals.
Lastly, not exactly related to your question, you could expose the port to mongod to your host and connect to it via your local mongo CLI client or a GUI client such as Robomongo.

TCP, UDP and HTTP servers inside docker stop immediately after starting the container?

I am to host TCP, UDP and HTTP servers, developed by Java, inside a java docker container.
All what I have in the dockerfile is a copy command for the server application and the bash files, which used to run the server/s.
However, the servers are stopped some seconds after running the container?
I use the following command for running the container:
docker run -d -p 8000:8000 -p 2701:2701 --name app_test_con_1 app_test_img_1
And this is the bootstrap command in the dockerfile:
CMD ["/workspace/one_click.sh"]
Is there any way to keep server running for ever? Since it does that if I run it with the same .sh files in the host OS; Of course as long as the terminal is not closed.
Docker containers will exit as soon as the process in the foreground completes. That means if your one_click.sh script does something like service apache2 start and then exits, the container will exit right away. Most of our Java images have some variant of the following to prevent this:
CMD service apache2 start && /bin/bash
The preferred way to use Docker is 1 process per container, if you're using it to run full VMs with services then you need to work around it.

Resources