Vagrant docker-exec - docker

I am running Vagrant on Mac OS X. I have created following Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.insert_key = true
config.vm.provider "docker" do |doc|
doc.image = "httpd"
doc.ports = ["80:80"]
doc.name = 'apache'
doc.remains_running = true
doc.has_ssh = false
end end
It is starting, however I can't execute following command:
vagrant docker-exec -dt apache -- /bin/bash
I have also tried to change apache into container ID, but have failed too.
The container is running as I can check it in Virtualbox.
I can only see that I have vagrant docker-logs and vagrant docker-run, but the documentation of Vagrant says that there should be docker-exec.
Any ideas?

-i --interactive is required if you want a bash shell you can type in.
-d --detach will not work for typing either as the process will be in started in the background.
Use vagrant docker-exec -it apache -- /bin/bash

Yep, that is correct. I also know now that you need to do list-commands to see this docker-exec command.
thank you

Related

dockerd not running on nixos

I installed docker on nixos, using:
nix-env -i docker
after that, dockerd was not running, so I started the daemon manually with:
dockerd
and in the logs, I see:
WARN[2019-06-26T01:02:31.784701442Z] could not change group
/var/run/docker.sock to docker: group docker not found
should I care about this warning?
When installing docker on NixOS, it's best to enable it in the NixOS configuration. Doing so will install docker as a system service.
Snippet for /etc/nixos/configuration.nix:
virtualisation.docker.enable = true;
# ...
users.users.YOU = { # merge this with your unix user definition, "YOU" is for illustration
isNormalUser = true;
# ...
extraGroups = [
# ...
"docker"
];
};
created a group docker. Docker needs that user group to start as a service.

Vagrant and docker Protocol error mounting directory windows 7

I have a custom boot2docker with this configuration attached to the .iso
config.vm.provider "virtualbox" do |v|
v.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 0, '--device', 0, '--type', 'dvddrive', '--medium', File.expand_path("../boot2docker.iso", __FILE__)]
v.customize ['modifyvm', :id, '--nictype1', 'virtio']
end
config.vm.network "private_network", ip: "192.168.10.10", id: "default-network", nic_type: "virtio"
My files in the directory
mycompany/
dockerhost/
Vagrantfile
Vagrantfile
The vagrantfile of the docker-host that replaces the above.
## This is required with the plugin winnfsd
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder "../", "/vagrant", type: "nfs"
When I start up the dockerhost with vagrant up it works well and the NFS is setup correctly, but when I start a service container vagrant up myservice
shows the error
==> myservice: Docker host is required. One will be created if necessary...
myservice: Docker host VM is already ready.
==> myservice: Syncing folders to the host VM...
dockerhost: Mounting shared folders...
dockerhost: /var/lib/docker/docker_1472079332_51007 => C:/Users/myuser/Desktop/mycompany
Vagrant was unable to mount VirtualBox shared folders. This is usually because the filesystem "vboxsf" is not available. This filesystem is made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attemped was:
set -e
mount -t vboxsf -o uid=`id -u docker`,gid=`getent group docker | cut -d: -f3` b5973a5087 /var/lib/docker/docker_1472079332_51007 mount -t vboxsf -o uid=`id -u docker`,gid=`id -g docker` b5973a5087 /var/lib/docker/docker_1472079332_51007
The error output from the command was:
mount: mounting b5973a5087 on /var/lib/docker/docker_1472079332_51007 failed: Protocol error
It looks like Vagrant mount another volumen when starts a container.
Any idea about how to fix it?
or why vagrant mounts another for my?
Tks
Vagrant and VirtualBox version incompatibility problems
This fix my problem:
Vagrant 1.8.5 should be compatible with the latest VirtualBox 5.1.2 release, however VirtualBox Guest Additions version we currently have is 5.0.20 and the latest boot2docker release is at 5.0.24. Both will most likely have issues with VirtualBox 5.1.2.
So for now avoid VirtualBox 5.1.x and stick with:
Vagrant 1.7.4 - 1.8.4
VirtualBox 5.0.x
Reference:
https://github.com/blinkreaction/boot2docker-vagrant/issues/83

Project Atomic - Cockpit doesnt install through Vagrant

I use vagrant to install Atomic and i'd like to install Cockpit through my vagrantfile.
Manually I use sudo atomic install fedora/cockpitws and I get no problems.
When I try to insert this in my vagrantfile I get this error: cannot enable tty mode on non tty input
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
The only sentence I add in my vagrantfile is : config.vm.provision :shell, inline: "sudo atomic install fedora/cockpitws"
Am I doing something wrong? Thanks in advance
you can try running as
config.vm.provision :shell, privileged: false, inline: "sudo atomic install fedora/cockpitws"
so it will ssh with your vagrant user and run the command so tty should be ok. when you omit privileged: false it ssh with your root user

Vagrant docker provider: create and start vs run

I'm new to vagrant, using 1.7.4 with VirtualBox 5.0.10 on Windows 7 and trying to figure out how to get it to setup and run docker containers the way I'd like, which is like so:
Start my docker host, which is already provisioned with the latest docker tools and boots with the cadvisor container started - I get this box from the publicly available williamyeh/ubuntu-trusty64-docker
If (for example) the mongo container I'd like to use has not been created on the docker host, just create it (don't start it)
Else, if the container already exists, start it (don't try to create it)
With my current setup, using the docker provider, after the first use of vagrant up, using vagrant halt followed by vagrant up will produce this error:
Bringing machine 'default' up with 'docker' provider...
==> default: Docker host is required. One will be created if necessary...
default: Docker host VM is already ready.
==> default: Warning: When using a remote Docker host, forwarded ports will NOT be
==> default: immediately available on your machine. They will still be forwarded on
==> default: the remote machine, however, so if you have a way to access the remote
==> default: machine, then you should be able to access those ports there. This is
==> default: not an error, it is only an informational message.
==> default: Creating the container...
default: Name: mongo-container
default: Image: mongo
default: Port: 27017:27017
A Docker command executed by Vagrant didn't complete successfully!
The command run along with the output from the command is shown
below.
Command: "docker" "run" "--name" "mongo-container" "-d" "-p" "27017:27017" "-d" "mongo"
Stderr: Error response from daemon: Conflict. The name "mongo-container" is already in use by container 7a436a4a3422. You have to remove (or rename) that container to be able to reuse that name.
Here is the Vagrantfile I'm using for the docker host:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.hostname = "docker-host"
config.vm.box_check_update = false
config.ssh.insert_key = false
config.vm.box = "williamyeh/ubuntu-trusty64-docker"
config.vm.network "forwarded_port", guest: 27017, host: 27017
config.vm.synced_folder ".", "/vagrant", disabled: true
end
...and here is the docker provider Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
VAGRANTFILE_API_VERSION = "2"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "docker" do |docker|
docker.vagrant_vagrantfile = "../docker-host/Vagrantfile"
docker.image = "mongo"
docker.ports = ['27017:27017']
docker.name = 'mongo-container'
end
end
Well, I'm not sure what had gotten munged in my environment, but while reconfiguring my setup, I deleted and restored my base docker host image, and from that point on, vagrant up, followed by vagrant halt, followed by vagrant up on the docker provider worked exactly like I was expecting it to.
At any rate, I guess this question is already supported by vagrant.

Debugging rails app running docker with vagrant

I'm trying to figure out the best development workflow with vagrant and docker running a rails app. In my dockerfile I have this:
FROM quirky/rails:latest
RUN mkdir /opt/app
WORKDIR /opt/app
# Install gems
ADD ./Gemfile /opt/app/Gemfile
ADD ./Gemfile.lock /opt/app/Gemfile.lock
RUN bundle install
# Instal npm packages
ADD ./package.json /opt/app/package.json
RUN npm install
# Expose directories and ports
VOLUME /opt/app
EXPOSE 3000
# Run the web server
WORKDIR /opt/app
CMD rm -f /opt/app/tmp/pids/server.pid && bundle exec rails s
My Vagrantfile looks like this:
config.vm.define "app" do |app|
app.vm.provider "docker" do |d|
d.build_dir = "."
d.link "db:db"
d.link "redis:redis"
d.link "solr:solr"
d.volumes = ["/app:/opt/app"]
d.ports = ["3000:3000"]
d.vagrant_vagrantfile = "./docker/Vagrantfile"
d.remains_running = true
end
end
config.vm.define "db" do |db|
db.vm.provider "docker" do |d|
d.image = "paintedfox/postgresql"
d.name = "db"
d.env = {USER: "vagrant", PASS: "password"}
d.vagrant_vagrantfile = "./docker/Vagrantfile"
end
end
config.vm.define "redis" do |redis|
redis.vm.provider "docker" do |d|
d.image = "dockerfile/redis"
d.name = "redis"
d.ports = ["6379:6379"]
d.vagrant_vagrantfile = "./docker/Vagrantfile"
end
end
config.vm.define "solr" do |solr|
solr.vm.provider "docker" do |d|
d.image = "quirky/solr"
d.name = "solr"
d.ports = ["8080:8080"]
d.vagrant_vagrantfile = "./docker/Vagrantfile"
end
end
Typically if I want to debug something I just stick a debugger statement in the code and I'm running it as a local process and it just hits the breakpoint and brings up pry or whatever the debugger console is. How does this work inside of a container inside vagrant?
This is how I start my dev environment:
vagrant up app --provider=docker
It launches it in the background. There doesn't appear to be a way to launch it and attach to it. Am I missing a command or a flag I can pass in to vagrant?
You are looking for docker exec or nsenter [1]. With one of these tools you can log into the container without SSH and check your logs.
If you want to debug vagrant creating and running the docker-container you can append the --debug flag like so:
vagrant up app --provider=docker --debug
But this won't give you any debug-info from your Vagrantfile directly. If you still want to get debug-messages out of your vagrantfile I recommend you to read about vagrants UI class.
PS: Maybe you simply want puts statements like so: puts "I'm here!"?
PPS: If you want to stick with vagrant and SSH the has_ssh value and a SSH-Server in the container is the way to go.
nsenter/docker exec
Have you tried the has_ssh option for the Vagrand Docker provider? It states that:
If true, then Vagrant will support SSH with the container. This allows vagrant ssh to work, provisioners, etc. This defaults to false
As an aside, I haven't tried this myself. I'm using Docker with a CoreOS image and running docker containers manually (with provisioning).

Resources