Skip to content

Instantly share code, notes, and snippets.

@gwpl
Created September 11, 2023 12:56
Show Gist options
  • Save gwpl/a81c07da3f958f96258348dfa006e62c to your computer and use it in GitHub Desktop.
Save gwpl/a81c07da3f958f96258348dfa006e62c to your computer and use it in GitHub Desktop.
# https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me
FROM ubuntu
ENV TZ=America/Mexico_City
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update -y
RUN apt-get install -y --no-install-recommends ssh sudo
RUN useradd --create-home -s /bin/bash vagrant
RUN echo -n 'vagrant:vagrant' | chpasswd
RUN echo 'vagrant ALL = NOPASSWD: ALL' > /etc/sudoers.d/vagrant
RUN chmod 440 /etc/sudoers.d/vagrant
RUN mkdir -p /home/vagrant/.ssh
RUN chmod 700 /home/vagrant/.ssh
RUN echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==" > /home/vagrant/.ssh/authorized_keys
RUN chmod 600 /home/vagrant/.ssh/authorized_keys
RUN chown -R vagrant:vagrant /home/vagrant/.ssh
RUN sed -i -e 's/Defaults.*requiretty/#&/' /etc/sudoers
RUN sed -i -e 's/\(UsePAM \)yes/\1 no/' /etc/ssh/sshd_config
RUN mkdir /var/run/sshd
RUN apt-get -y install openssh-client
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Copy From: https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me
$ vagrant up
$ vagrant ssh
Mario Garcia
Posted on 11 Aug 2020 • Updated on 12 Mar 2021
![](https://dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg) 7 ![](https://dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg) 3 ![](https://dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg) ![](https://dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg) ![](https://dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg)
# Using Docker as provider for Vagrant
[#linux](https://dev.to/t/linux) [#docker](https://dev.to/t/docker) [#tutorial](https://dev.to/t/tutorial)
According to the [documentation](https://www.vagrantup.com/docs/providers), Vagrant has support for [VirtualBox](https://www.virtualbox.org/), [VMWare](https://www.vmware.com/), [Hyper-V](https://www.microsoft.com/hyper-v) and [Docker](https://www.docker.io/) as providers.
Through this blog post you will learn how to configure Docker and Vagrant.
## [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#docker)Docker
For installing Docker on Linux follow the instructions in the [documentation](https://docs.docker.com/engine/install/). Go directly to the instructions of your distribution from the following list:
- [CentOS](https://docs.docker.com/engine/install/centos)
- [Debian](https://docs.docker.com/engine/install/debian)
- [Fedora](https://docs.docker.com/engine/install/fedora)
- [Raspbian](https://docs.docker.com/engine/install/debian)
- [Ubuntu](https://docs.docker.com/engine/install/ubuntu)
If you use Arch Linux or any Arch-based distribution, install it using `pacman` and initialize the daemon:
```
$ sudo pacman -S docker
$ sudo systemctl start docker
```
Enter fullscreen mode Exit fullscreen mode
For running Docker commands you will need `root` permissions. As Vagrant will run it on your behalf, you have to configure it for running without `sudo`. You can follow the instructions in the [Post-installation steps for Linux](https://docs.docker.com/engine/install/linux-postinstall/) section as described below.
First create the `docker` group:
```
$ sudo groupadd docker
```
Enter fullscreen mode Exit fullscreen mode
Add your user to the `docker` group:
```
$ sudo usermod -aG docker $USER
```
Enter fullscreen mode Exit fullscreen mode
You will have to log out and log back in for the changes to take effect.
If you want to activate the changes to groups in your current session, run:
```
$ newgrp docker
```
Enter fullscreen mode Exit fullscreen mode
To check if you can run docker commands without sudo:
```
$ docker run hello-world
```
Enter fullscreen mode Exit fullscreen mode
This command will download the test container `hello-world` and run it.
## [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#vagrant)Vagrant
For installing Vagrant go to the [download page](https://www.vagrantup.com/downloads) and get the right package for your distribution. You can also install it from the repositories of some Linux distributions.
#### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#debianbased)Debian-based:
```
$ sudo apt install vagrant
```
Enter fullscreen mode Exit fullscreen mode
#### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#fedora)Fedora:
```
$ sudo dnf install vagrant
```
Enter fullscreen mode Exit fullscreen mode
#### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#centos)CentOS
```
$ sudo dnf install -y https://releases.hashicorp.com/vagrant/2.2.9/vagrant_2.2.9_x86_64.rpm
```
Enter fullscreen mode Exit fullscreen mode
#### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#arch-linux)Arch Linux:
```
$ sudo pacman -S vagrant
```
Enter fullscreen mode Exit fullscreen mode
## [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#vagrant-docker)Vagrant + Docker
There are two ways you can use Docker as provider. Using an image from the Docker registry:
```
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.image = "foo/bar"
end
end
```
Enter fullscreen mode Exit fullscreen mode
Or a `Dockerfile`:
```
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.build_dir = "."
end
end
```
Enter fullscreen mode Exit fullscreen mode
### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#using-a-dockerfile)Using a Dockerfile
First you have to create a directory to store the configuration files for your environment and change to this directory.
```
$ mkdir docker-test
$ cd docker-test
```
Enter fullscreen mode Exit fullscreen mode
Create a Dockerfile:
```
$ touch Dockerfile
```
Enter fullscreen mode Exit fullscreen mode
And add the following content:
```
FROM ubuntu
ENV TZ=America/Mexico_City
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update -y
RUN apt-get install -y --no-install-recommends ssh sudo
RUN useradd --create-home -s /bin/bash vagrant
RUN echo -n 'vagrant:vagrant' | chpasswd
RUN echo 'vagrant ALL = NOPASSWD: ALL' > /etc/sudoers.d/vagrant
RUN chmod 440 /etc/sudoers.d/vagrant
RUN mkdir -p /home/vagrant/.ssh
RUN chmod 700 /home/vagrant/.ssh
RUN echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==" > /home/vagrant/.ssh/authorized_keys
RUN chmod 600 /home/vagrant/.ssh/authorized_keys
RUN chown -R vagrant:vagrant /home/vagrant/.ssh
RUN sed -i -e 's/Defaults.*requiretty/#&/' /etc/sudoers
RUN sed -i -e 's/\(UsePAM \)yes/\1 no/' /etc/ssh/sshd_config
RUN mkdir /var/run/sshd
RUN apt-get -y install openssh-client
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
```
Enter fullscreen mode Exit fullscreen mode
The official Docker image of Ubuntu will be used as specified in `FROM ubuntu`.
When running `apt-get update -y` or `apt update -y`, it will ask you to configure the timezone, the prompt will wait for you to enter the selected option.
To avoid this, you have to add the configuration options in the `Dockerfile`, as described [here](https://dev.to/setevoy/docker-configure-tzdata-and-timezone-during-build-20bk), by adding the following lines:
```
ENV TZ=America/Mexico_City
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
```
Enter fullscreen mode Exit fullscreen mode
Replacing the value of `TZ` according to your timezone.
Vagrant requires an SSH connection to access the container and Docker images come only with the `root` user. You have to configure another user with `root` permissions. That's why the `ssh` and `sudo` packages are required.
In the following lines the `vagrant` user is created and a password assigned. The user wouldn't be required to use a password when running any command that requires `root` permissions. The user is also added to the `sudo` group.
```
RUN useradd --create-home -s /bin/bash vagrant
RUN echo -n 'vagrant:vagrant' | chpasswd
RUN echo 'vagrant ALL = NOPASSWD: ALL' > /etc/sudoers.d/vagrant
```
Enter fullscreen mode Exit fullscreen mode
`.ssh` directory must be created. This is the directory when configuration files related with SSH connection are stored.
```
RUN mkdir -p /home/vagrant/.ssh
RUN chmod 700 /home/vagrant/.ssh
```
Enter fullscreen mode Exit fullscreen mode
An insecure key is added for the initial configuration. This key will be replaced later when you initialize your virtual environment the first time. Also, the ownership of the `.ssh` directory is changed to `vagrant` user.
```
RUN echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==" > /home/vagrant/.ssh/authorized_keys
RUN chmod 600 /home/vagrant/.ssh/authorized_keys
RUN chown -R vagrant:vagrant /home/vagrant/.ssh
```
Enter fullscreen mode Exit fullscreen mode
You can log in with the `root` user but the password wasn't assigned. You can change the password adding a similar line but changing `vagrant:vagrant` to `root:THEPASSWORDYOUCHOOSE` or after log in.
### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#vagrantfile)Vagrantfile
Now create a `Vagrantfile`:
```
$ touch Vagrantfile
```
Enter fullscreen mode Exit fullscreen mode
And add the following content:
```
Vagrant.configure("2") do |config|
config.vm.provider :docker do |d|
d.build_dir = "."
d.remains_running = true
d.has_ssh = true
end
end
```
Enter fullscreen mode Exit fullscreen mode
Here you tell Vagrant to build the Docker image from the `Dockerfile` and the container can be accessed through SSH and must be always running.
```
d.build_dir = "."
d.remains_running = true
d.has_ssh = true
```
Enter fullscreen mode Exit fullscreen mode
For installing software you can use a shell script or any provisioning tool supported by Vagrant. You can also add the instructions for installing and configuring the tools you required in the Dockerfile.
If you want to use a bash script, just add the following line after `config.vm.provider`.
```
config.vm.provision :shell, path: "script.sh", privileged: false
```
Enter fullscreen mode Exit fullscreen mode
The `privileged` option is set to `false` as you will not require to run these commands with `root` permissions.
### [](https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me#up-and-running)Up and running
When running `vagrant up`, Vagrant will build the Docker image based on the `Dockerfile` and run the container.
You can log in to the virtual environment running `vagrant ssh`.
If you want to stop the environment, run `vagrant halt`. For destroying the virtual environment run `vagrant destroy`.
# https://dev.to/mattdark/using-docker-as-provider-for-vagrant-10me
Vagrant.configure("2") do |config|
config.vm.provider :docker do |d|
d.build_dir = "."
d.remains_running = true
d.has_ssh = true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment