Can I restart a docker container from within the container terminal? - ruby

I am making a Sinatra App inside a container, But whenever I want to see the changes I have to detach and run:
docker restart <container_ID>
to see the changes.
Is there any way that I could restart the docker from within to see the changes?
I cloned https://github.com/tcnksm-sample/docker-sinatra.git
Build sudo docker build -t sinatra .
Run container sudo docker run -d -p 4567:4567 sinatra
Enter the container terminal sudo docker exec -it <container_ID> bash
Changed the app.rb file but nothing changed on http://localhost:4567,
So I detach from the container and ran docker restart <container_ID> to see the changes. Since I am going to change the app.rb alot It is so inconvenient for every time I change something I have to detach and run docker restart <container_ID>

You shouldn't have to restart the all docker engine itself.
If your Dockerfile pull the changes from a repo, and redo a bundle install, as in this Dockerfile, all you need to do would be, as in this example:
# on docker server or the same machine
$ sudo docker stop container-id
$ sudo docker pull luisbebop/docker-sinatra-hello-world
$ sudo docker run -d -p 5000:5000 luisbebop/docker-sinatra-hello-world

Related

Cannot mount a volume without using sudo

The following command works and mounts the local volume:
sudo docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
The following command does not work and does not mount the volume
docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
For some reason, docker is only able to mount volumes using the sudo command, rendering our local docker environment useless on a colleagues laptop. The same docker-compose file works on my laptop (also a mac, same OS).
Any idea as to what the issue might be with his laptop configuration? Or indeed the docker setup.
(The code extract is to make clear the problem with mounting volumes, the same issue presents itself using a compose.yml file.)
Non working code:
docker run -ti -v "$PWD/codebase/realsmart-saml-copy":/var/www/html realsmart-docker_smartlogin bash
No error messages are displayed, but the results are not as expected as the volume does not mount without using sudo.
Try to see if the user is part of the docker group.
It would make sense that sudo works, but not for the local user, if that local user is not part of the docker group.
The solution for anyone interested.
After upgrading to Docker Desktop Boot2Docker has been replaced.
Steps to fix the issue:
docker-machine rm machine-name
unset DOCKER_TLS_VERIFY
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_HOST
restart Docker Desktop
cd path/to/docker-project.
docker-compose build
docker-compose up (or docker run)
project now available on localhost
Further details: https://docs.docker.com/docker-for-mac/docker-toolbox/
Add your user to the docker group.
sudo usermod -aG docker $USER

Run docker with aliased port and access to bash

I'm trying to start a docker snapshot and connect to it via bash but also alias its port so I can access it from my local system at localhost:3333, this is what I have:
docker run -d -p 3333:3000 -t -i mysnapshot /bin/bash
However while it does start the container image it doesn't connect to it via bash
This is the output it generates:
3c86ca433d645c6c11315e89bbeaf89f072e2d1fa83213d4c4256c4a1af98322
and this is the dockerfile used to build the image:
FROM node:10
Setting working directory. All the path will be relative to WORKDIR WORKDIR /usr/src/app
Installing dependencies COPY package*.json ./ RUN npm install
Copying source files COPY . .
Building app
RUN npm run build
Running the app CMD [ "npm", "start" ]
You used -d option in docker run command, which will run the container in detached mode in the background.
Please check this out.
To get into the bash run
docker exec -it <conatiner-id> /bin/bash
where <container-id> can be retrieved from docker ps output.
Also as per your dockerfile you want npm start to be the first process in the container, so while running docker run command don't specify /bin/bash because it will override the CMD npm start mentioned in the dockerfile.
Hope this helps, let me know.
It seems you may need to overwrite your entrypoint because last line of your dockerfile mention your start command is npm start.
Also, -d detached mode is not needed.
Try this one:
docker run -it -p 3333:3000 --entrypoint=/bin/bash mysnapshot

docker sharing host directory

I used to share my host diretory with a docker container with the command (osx):
docker run --name tensorflower -it -p 8888:8888 -v /c/foo/labs:/home tensorflow/tensorflow
on windows it used to work with:
docker run --name tensorflower -it -p 8888:8888 -v //c/foo/labs:/home tensorflow/tensorflow
But now it does not work on windows. I tried both -v variants. As I do not get any error messages, I have no glue where to dig in to solve the problem.
How can I share the host directory c:\foo\labs with my docker container?
Or at least get some more informaton regarding the error?
Docker version 17.09.1-ce, build 19e2cf6 on windows 10

Error while running a container using docker

I have installed docker in my machine
I could successfully pull images from the repository and the pulled images are clearly listed when I see the list of images pulled.
The docker service was also started using
sudo service docker start
However, when I try to run the same using the command,
sudo docker run -it ubuntu:12.04
I am getting the following error
docker: Error response from daemon: Container command '/bin/bash' not
found or does not exist..
The issue remains the same for any image that I have tried with
What could be the reason for this issue?
It depends on your version of docker, but check if you have issue 23411, where adding a workdir is needed:
sudo docker run --workdir /var -it ubuntu:12.04
There seem to be some recent bug in start.go, and a PR in progress.

Phundament under Windows - "Interactive mode is not yet supported on Windows"

I have Docker Toolbox installed under Windows 7. The Docker daemon is running inside a VM (the default behavior of Docker Toolbox).
I am trying to get Phundament running using the default tutorial.
It all works fine until I reach this command:
docker-compose run php composer install
It results in:
I've successfully attached to the running container using docker exec -it <container ID> bash but when I do a ls /app command on any of the two containers I get no files in that directory. In effect, the attempt to run composer install there fails.
I tried attaching to both containers and the result is identical.
I also noticed that behavior just recently, it's sadly a limitation of docker-compose on Windows.
For the command you mentioned you can actually run
docker-compose run -d php composer install
As general workarounds...
use docker exec -it app_php_1 bash
see also https://getcarina.com/docs/troubleshooting/troubleshooting-cannot-enable-tty-mode-on-windows/
if you don't really need an interactive shell, you could just run a command or script, like docker-compose run -d php setup.sh
Note: I need to double-check the above suggestions on a real Windows testing system.
PS: I am the author if Phundament. I've also just created an issue for this.
Please try:
winpty docker-compose run php composer install
it works for example:
winpty docker run --rm -it debian bash

Resources