VScode remote containers - how to view the dockerised service console output? - docker

This is a follow on from this question (none of the current answers seem hit the nail on the head).
VScode's default behaviour for starting a remote vscode session (using VScode Remote-Containers) seems to be:
Run the project's docker-compose file
If the user selects show log (a UI popup) during build, open a VScode terminal session called Dev Containers reflecting Docker's build logging, supplemented with VSCode Remote-Containers logging. This output ends after build complete.
If the user didn't select show log, and later opens a VScode terminal after build complete, just start a new bash session within the container. No other VScode terminal sessions exist.
Launch a VScode session from inside the now-running container
From the user's perspective, the container is running, but the output that is happening inside the container seems inaccessible (even if the docker-compose command did not use daemon mode).
So, how can the user now view the console output that is happening inside the container?
If I am reading correctly, VScode Remote-Containers documentation seems to suggest overriding the default behaviour, ie:
supress your docker-compose command that would otherwise have started the service, and instead apply some dummy command to persist the container upon creation, then
manually start the service (using debug mode, or via VScode terminal) from inside the remote session. This reveals the output, but within an accessible VSCode terminal session.
Is there no way to:
A) Start the services via system terminal (e.g. docker-compose up), and then start a VSCode remote session in this already running container*, or
B) Accessing the service's output without having to override as above (the override seems hacky)
*This would be ideal. The Remote-Containers "Attach to Running Container..." command sounds close to this. But it seems to instantiate itself in a directory I don't recognise, and doesn't seem to be the container.

Option A seems to be achievable by
Starting the service in terminal (docker-compose up)
In vscode, using the remote-containers "remote explorer" UI (not the cmd+P "Attach to container" commands) to select the running container's working directory. Right click > "Open in container". This doesn't actually open a new container, it "Opens the directory, from within the container".
OR ( thanks #cybercoder )
Letting vscode start the services
In a separate terminal: docker logs -f container_name OR docker-compose logs -f

Related

How to make VSCode forget about an old docker container?

I ran docker container prune, but after attaching it to my new container, I still see the old container in the explorer pane in VSCode. And I do not see the new container in the explorer pane.
And if I try to open a new terminal (Terminal > New Terminal), I see:
The terminal process failed to launch: Starting directory (cwd)
"/path/inside/my/old/container" does not exist.
Whenever I create a new container based on the same image (drupal:latest), VSCode tries to open the old container based on this image, even if I give the new container a different name.
VSCode also sometimes shows the error "Workspace does not exist" when I attach it to the new container.
After attaching to the new container, I needed to do...
File > Open Folder
And then, in the Open Folder UI, I needed to edit the old path there, which still referred to the old container, and then click OK.
For example, in the new container, the path /opt exists, so entered that path and clicked OK. This causes the explorer to refresh and the old container disappears and now I can access the new container. I can also open the terminal now.
0
I succeeded in connecting to a remote server configured with Docker through vscode. By the way, the list of containers from the past was fetched from the remote explorer of vscode. If you look at this list of containers, they are obviously containers made with images I downloaded a few days ago. I don't know why this is happening.

VS Code log into container as specific user

I built a Debian 10 sandbox container where I could play with my code bases. I added a user to maintain a permission wall between sudo commands typed too quickly with
USER ELDUDE
in the Dockerfile. However, when I build the container with that user as default login option VS Code fails to connect to the container with
Error: stream ended with:124 but wanted 1128865906
This is the initial error that brought me down this path.
If I do not specify the default user in the Dockerfile I can connect fine but VSCode server in the container runs as root. Now, that becomes a mess with file permissions etc if I start working on stuff.
Any idea what is going on? Can I set user names when I log into a container?
I also thought of enabling SSH on the container and connect through VS SSH but that failed thus far...

How to reconnect to docker-compose output log?

Please help, I'm not even sure if I am asking the right question here as there are many new components to my environment (the new components of my Environment are that I am new to developing in a Windows OS, New to Visual Studio Code IDE, and new to Docker within VS Code. This question could pertain to any of those factors.
Scenario:
I boot up my windows 10 machine, open VS Code, go to the command line from within VS Code (I am using a Git Bash Shell within VS Code). From this terminal I start my project with the following command: docker-compose up --build
as a result of running this command, I see the output in my terminal which indicates that all three of my containers have started up (Note this is a Flask application using Postgres with an Angular front end, each one has it's own container).
My application has a test API endpoint which when called responds with 'status ok'. Upon hitting that endpoint in postman I see a couple of lines of output in my terminal indicating that the application has processed the request for the specific URL. Everything is great.
Now I close all my applications and reboot the machine.
Upon rebooting I see a message from the system informing me that my docker containers are starting. This is good. But now I would like to get back to the state where I can see that same output that I saw when I ran the docker-compose up command, however this is no longer in the terminal on VS Code.
My question is, how can I get that output again without shutting down the docker containers and re-building them? Sure, I could do that, but this seems like an unnecessary step since the containers auto-restarted on system reboot.
Is there a log I can tail?
Additional info:
In the DockerFile for the API server. The server is started with the following command:
CMD ["./entrypoint.local.sh"]
In the entrypoint.local.sh file, the actual application is started with this command:
uwsgi --ini /etc/uwsgi.local.ini --chdir /var/www/my-application
Final note: This is not an application I created so I would like to avoid changing it since this will affect others on my team
In your terminal run: docker-compose logs --follow <name-of-your-service>
Or see every log stream for every service with docker-compose logs --follow
You can find the name of your docker-compose service by looking at each key under services: in your docker-compose.yml

How to make VSCode run custom script when attaching to a running remote container

I have a running Docker container and would like to use the VSCode remote container plugin to attach to it.
Is it possible to have VSCode run a script when it attaches? Some custom actions are required to setup the container. These actions cannot be baked into the Dockerfile/Image.
Is it possible to configure the Docker exec arguments when attaching to a running container. (This is possible for Docker Run using .devcontainer when creating new containers, but I haven't found anything about Docker exec regarding already running containers).
There is a "postAttachCommand" that lets you execute a custom command after the vscode attached to the running container.
However my preference would be to use a login shell, for that there is an undocumented property called
"userEnvProbe": "loginInteractiveShell"
Below github issue explains this parameter (This is where i learnt about the parameter as well) :
https://github.com/microsoft/vscode-remote-release/issues/3585
The userEnvProbe and postAttachCommand is per docker container, you have to add them to "Container Configuration File", hover your mouse on the tip of the red arrow and you will see a settings icon, when you press it you can access to the "Container Configuration File"
For further customization there is a great github page that explains what else you can do to further customize the way you execute docker commands as well
https://github.com/microsoft/vscode-docker/issues/1596

How to view docker logs from vscode remote container?

I'm currently using vscode's remote containers extension with a .devcontainer.json file that points to my docker-compose.yml file.
Everything works fine and my docker-compose start command gets run (which launches a web server), but I haven't found a way to quickly see the logs from the web server. Has anyone found a way to view the docker log output automatically once vscode connects to the remote container?
I know as an alternative I could remove my container's start command and, after vscode connects, manually open a terminal and start the web server, but I'm hoping there's an easier way.
Thanks in advance!
I'm not using remote containers, just local once, so not sure if this applies but for locally running containers, you can go to the "Docker" tab (you need to install the official Microsoft Docker VS Code Plugin) where you can see your running containers. Just right-click on the container you want to see the logs for and select "View Logs":
You'll see a new "Task" appear in the Terminal pane that will show all your docker logs:
This question is really old and I'm not sure it this option was available at this time but just open the Command Palette (F1) and select/find "Remote-Containers: Show Log".
You see now the log of your container in the terminal.
You can open the command palette and search for: Remote Explorer: Focus on containers view. You should see a sidebar of containers, if you right click your container you can view logs.
I use VS Code's builtin terminal to see the live logs of the docker container that is connected with VS Code.
When VS Code is connected to the docker container, you can open the builtin terminal using the View > Terminal menu option. You should see an existing terminal labeled Dev Containers.
Maybe this is too late? But for others, this is how I do it.
First, instead of logging stuff to the stdout, I redirect all of the outputs into one single file and then using the tail command to steam the output to the terminal instead.
For example, I am going Go here:
logFile, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
log.Fatal("Fail to open the log file")
}
logrus.SetOutput(logFile)
Once that's done, I open up my terminal and run my the following command:
$ tail -f {logFileName}
That's one way to do it I guess, but I sure hope VSCode can come up with a better solution.
In the Remote Explorer tab you can see all your docker containers. Under "Dev Containers" is the container for the service specified in devcontainer.json; the rest are in "Other Containers." Simply right click on the container you're interested in and click "Show Container Log." You'll see the full output of the command for that service, just like in an interactive terminal - not a docker build log!
Note I am using a local development container and did not test with remote containers but I'm guessing it's the same.

Resources