Docker Exec -it bash Terminal Output Improperly Formatted [duplicate] - docker

This question already has answers here:
Missing Carriage Return in Docker for Mac Containers
(3 answers)
Closed 5 years ago.
The command promp after running docker exec -it ... bash is not formatted nicely. The problem happens on the OSX terminal or iTerm2. Any setting for the iTerm2 I need to change or is that related to the bash command? Thanks.
Picture:

I think I can tell from the picture that you're not actually talking about docker logs (which is a docker real docker command that you didn't run). It looks like you want to know why your console output is not printing with nice formatting after you get to a command prompt inside a container with docker exec -it ... bash.
I suspect your PS1 length is being calculated incorrectly either inside the container or on your host machine. Read about it here. The prompt inside the container looks like the standard prompt and the prompt on your host machine looks fancy, so I bet you have a problem with the PS1 on your OS X host.
To see if your host machine is causing the problem, drop into a subshell with a minimal PS1, then run Docker and see if the problem persists:
$ bash --rcfile <(echo "PS1='$ '") -i
$ docker exec -it ... bash
# ls
If the problem goes away, it is a problem with the PS1 on your host machine. Fix it permanently by following the directions in the link above so the length is calculated correctly.

Related

How do I run a command line tool in a Rails app on a docker container?

so I have a working Rails project which is running in a docker container
I have a need to use this tool : https://github.com/google/oauth2l
I need to use this tool within my rails application, take it's output and use it in a post request I'm making via HTTParty.
I'm able to do this already but that's assuming I have oauth2l installed on my system. But since
I'm running my app in production in a container, I'm not sure what the best way to go about this is.
In the docs, there does seem to be a way to 'inject' it into my container but adding those lines to the Dockerfile is leading to syntax errors.
Any ideas on what I could do here?
As a general rule, I think you shouldn't run commands inside the docker container unless you really need it, the best approach is to install what you need while you are building your docker image (with only docker or docker-compose), but in case you really need to enter inside your container here is how I usually do it:
sudo -s
docker container ls (list all containers)
docker container -ti <id of the container> sh
or
docker exec -ti <id of the container> /bin/bash
The sudo -s sometimes is needed depending on how you configure the instance where your container is running, which means that all the commands you are running from now on are like admin. And you may need to execute sh or bash, again depending on your instance.
Hope that helps! 👍
Is better to type all the commands needed in the Dockerfile.
BUt, if you want to run a command within the container, try this:
docker exec -it my-app-web-1 rails db:create db:migrate
docker exec -it my-app-web-1 rails devise:install
These are some examples that you can do.

Docker Bind Mounts in WSL do not show files

Im trying to use the docker client from inside WSL, connecting to the docker engine on Windows. Ive exposed the docker engine on Windows on port 2375, and after setting the DOCKER_HOST environment variable in WSL, I can verify this works by running docker ps.
The problem comes when i attempt to mount directories into docker containers from WSL. For example:
I create a directory and file inside my home folder on WSL (mkdir ~/dockertest && touch ~/dockertest/example.txt)
ls ~/dockertest shows my file has been created
I now start a docker container, mounting my docker test folder (docker run -it --rm -v ~/dockertest:/data alpine ls /data)
I would expect to see 'example.txt' in the docker container, but this does not seem to be happening.
Any ideas what I might be missing?
There are great instructions for Docker setup in WSL at https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly#ensure-volume-mounts-work - solved most of my problems. The biggest trick for me was with bind-mounted directories; you have to use a path in WSL that the Docker daemon will be able to translate, for example /c/Users/rfay/myproject.
I don't think you need to change the mound point as the link suggests. Rather if you use pwd and sed in combination you should get the effect you need.
docker run -it -v $(pwd | sed 's/^\/mnt//'):/var/folder -w "/var/folder" alpine
pwd returns the working folder in the format '/mnt/c/code/folder'. Pipe this to sed and replace '/mnt' with empty string will leave you with a path such as '/c/code/folder' which is correct for docker for windows.
Anyone stumbling here over this issue follow this: Docker Desktop WSL2 Backend and make sure you are running the version 2 of the WSL in PowerShell:
> wsl -l -v
NAME STATE VERSION
* docker-desktop Running 2
Ubuntu Running 2
docker-desktop-data Running 2
If your Ubuntu VERSION doesn't say 2, you need to update it according to the guide above.
After that, you will be able to mount your Linux directory to the Docker Container directly like:
docker run -v ~/my-project:/sources <my-image>
Specific to WSL 1
Ran into the same issue. One thing to remember is that docker run command does not execute a container then and there on a command shell. It sends the run arguments to a docker daemon that does not interpret WSL path correctly. Hence, you need to pass Windows formatted path in quotes and with back slashes escaped
Your Windows path is
\\wsl$\Ubuntu\home\username\dockertest
Docker command after escaping will probably be like
docker run -it --rm -v "\\\\wsl\$\\Ubuntu\\home\\username\\dockertest":/data alpine ls /data
try
docker -v /:{path} exe
Hope to help you.

Docker, Kitematic, running terminal commands

I am using Docker with Kitematic and I have installed an image of Linux with Magento 2 on it.
It all works well, but Magento 2 requires that I run terminal commands for several things. How do I do this?
I know where the terminal is and there's Docker CLU, but when I go to the actual Magento directory, there's only the app but no bin/magento? So how does this work, or, in other words, where do I run the root commands from?
This has already been asked and answered. Just run
docker exec -it <container_id_or_name> /bin/bash
Check the link below.
How do I run a command on an already existing Docker container?

Using Docker's CMD to run a Shell Script + Get Output

I've been using a docker file for about 6 months now without issue but after a few changes in compute engine, I'm hitting a weird issue where something in my start up script is behaving the way it should / it used to.
I have a shell script that does a couple tweaks to the environment before starting a web server which is started like so:
ADD src/docker/startup.sh /home/gauntface/docker/startup.sh
CMD /home/gauntface/docker/startup.sh
startup.sh echo's logs but I can't find a way to view these logs, does anyone have any advice?
docker logs shows nothing for my container
Additional Notes
I'm running the docker command with daemon mode. Without Daemon mode, docker throws this error:
the input device is not a TTY
The Docker file and start up script are here:
https://github.com/gauntface/gf-site/blob/staging/src/docker/Dockerfile-base
https://github.com/gauntface/gf-site/blob/staging/src/docker/startup.sh
docker logs by default will show the output from stdout/stderr:
$ docker run -it --name test-it busybox echo hello world
hello world
$ docker logs test-it
hello world
We'd need to know more about your shell script, what output it generates, and what debugging you've done to give a more detailed answer about why it's not working.

Why is "docker start" outputting the name of the container?

I have a small minimal test container made using the ruby image. The ruby script is simple, and outputs the single string "Twitter".
When I first run the image and create the container, I get this output:
$ docker run -it --name my-running-script my-ruby-app
Twitter
Great so far - the script completes and the container exits.
But when I try to start it again, it first outputs the name of the container:
$ docker start -a my-running-script
my-running-script
Twitter
What is causing this output, and how can I get it to stop? (It's printed on stdout, and redirecting stderr doesn't help.)
I don't know if it's relevant, but this is running on OS X using boot2docker.
This is the expected behavior for docker start. Then you can do things like assign the containerID to a variable, etc.

Resources