How can i execute commands inside containers during host provision - linux

I am using vagrant to build docker host and then i have shell script which basically install all required packages for host and That script also build and run containers
vagrant file
config.vm.provision :shell, :inline => "sudo /vagrant/bootstrap.sh"
Inside that i run containers like
docker run -d . .bla bla .. .
This works fine but i have to ssh into container and run make deploy to install all the stuff.
Is there any way i can run that make deploy from within my bootsrap.sh.
The one way is make that as entry point but then that will do with every run,
I just want that when i provision host then that command should run inside some container and show me output like vagarnt shows for host

use docker exec
see the doc
http://docs.docker.com/reference/commandline/exec/
for example
docker exec -it container_id make deploy
or
docker exec -it container_id bash
and then
make deploy
inside your container

Related

Docker container don't start

Hi I've got a problem with docker. I'm using it on s390x Debian, everything was working fine but now i can't start my containers. Old containers are working but when i create new container using for example: docker run ubuntu then i'm trying docker start [CONTAINER] my container don't start. When i use docker ps -a I've got all of my containers, but after when I use docker ps i can't see my new container. As you can see on scr. I created container with name practical_spence and ID 3e8562694e9f but when i use docker start, it's not starting. Please help.
As you do not specify a CMD or entrypoint to run, the default is used which is set to "bash". But you are not running the container in interactive terminal mode, so the bash just exits. Run:
docker run -it ubuntu:latest
to attach the running container to you terminal. Or specify the command you want to run in the container.
You container did start but exit instantly as it has nothing to do. You can start like this docker run -d ubuntu sleep infinity. Then use docker ps to see the running container. You can of course exec into it to do something docker exec -it <container> bash. You can stop it docker stop <container>. Re-start it docker start <container>. Finally delete (stopped) it as you don't need it anymore docker container rm <container>.

Unable To Run Docker In Interactive Mode in Azure (Windows) VM

Trying to learn container (Docker) by running an interactive mode on Azure (windows) VM. The problem is no matter what I've tried I'm stuck with this:
Service 'w3svc' has been stopped
Service 'w3svc' started
Following are info I think are relevant (please correct me if I'm wrong) and things I've tried:
According to Docker Hub here are the requirement to install Docker Desktop on Windows.
I think it meets the requirements:
The Azure (windows) VM is a D4s_v3; the important thing here is that it's a v3 ( that means it supports Hyper-V)
Was able to install Docker Desktop successfully and it's a Windows container:
Was successfully in pulling two Microsoft base images:
Have tried running these commands as Admin either in PowerShell or Command line:
docker run -it 1202696d4a85 cmd
docker run -it microsoft/iis cmd
docker run -it mcr.microsoft.com/windows/servercore/iis cmd
docker run -it 1202696d4a85 --entrypoint cmd
docker run -it microsoft/iis --entrypoint cmd
docker run -it mcr.microsoft.com/windows/servercore/iis --entrypoint cmd
and we're stuck with this:
However, these two commands:
docker run -it 755f6d13fa75 cmd
docker run -it 755f6d13fa75 --entrypoint cmd
we get this error that tells us that the host and container OS are not compatible:
Is this a misleading error? Thought "Windows Sever 2019 DataCenter" is Server Core? And if it's the Desktop Experience it would have specified?
Also, when creating the VM on Azure there's no 'Server Core' option:
So, what do we need to do so that we can run in interactive mode?
TIA!
[Update]
Based on the suggestions below created a new Azure Windows 10 Pro VM.
Ran these two commands:
docker run -it 1202696d4a85 cmd
docker run -it 1202696d4a85 --entrypoint cmd
Same issue.

Can I run docker diff from a container on the same host as the container I want to run the diff on?

I have two containers running on a host. When I'm in container A I want to run a diff on container B compared to it's image to see what has changed in the filesystem. I know this can be ran easily from the host itself, but I'm wondering is there any way of doing this from inside container A, to see the difference on container B?
You can run any docker commands from within container which will communicate with host docker daemon if:
You have access to docker socket inside container
You have docker client inside container
You can achieve first condition by mounting docker socket to container - add following to your docker run call:
-v /var/run/docker.sock:/var/run/docker.sock.
The second condition depends on your docker image.
If you are running bare Ubuntu image you can have shell inside container which will be able to do what you want with following command:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:latest sh -c "apt-get update ; apt-get install docker.io -y ; bash"

Why app in docker container doesn't restart?

I've deployed some docker containers with golang apps. One of them I need to start by this command:
docker run --restart unless-stopped -it myapp /bin/bash
The next step I enter the container and edit some config files, then I run
go build main.go
and ./main
After that I press ctrl+q and leave it out.
Everything works perfectly and all my containers restart perfectly after restarting server. But there is one issue, when myapp container restarts, the golang application doesn't run while container still works. I have to enter this again and run ./main. How can I fixed it?
Dockerfile
FROM golang:1.8
WORKDIR /go/src/app
COPY . .
RUN go-wrapper download # "go get -d -v ./..."
RUN go-wrapper install # "go install -v ./..." RUN ["apt-get","update"]
RUN ["apt-get","install","-y","vim"]
EXPOSE 3000
CMD ["app"]
When you create a container and pass in /bin/bash as the command, that's as far as Docker cares. When the container restarts, it will start up another instance of /bin/bash.
Docker doesn't watch your shell session and see what things you do after it starts the command. If you want to actually run ./main as the command of the container, then you'll need to pass in /go/src/app/main as the command instead of /bin/bash.
Additionally, compiling code is something better done during the image build phase instead of at container runtime.

Docker: Alias command name to run another sibling docker container

I know how to -v the docker socket into a container to make the host's docker daemon available inside a container. Fine.
I have a dockerized application A that can operate on files on the host.
I have another dockerized application B that wants to use that application A to operate on files on the host, but is hardcoded to call /usr/bin/A filename.
How do I alias /usr/bin/A within container B, so that it will call out to the other container, like
docker run -ti --rm A filename
You could just replace /usr/bin/A in container B with a shell script:
#!/bin/sh
docker run -ti --rm A "$#"
You could do this in the image itself (via your Dockerfile), or you could bin-mount the script when you start container B (docker run -v /path/to/my/script:/usr/bin/A ...).
Now you run /usr/bin/A some_filename and it should do the right thing, assuming that you can successfully run docker inside the container.

Resources