---
title: Running Docker in a Vagrant VM
description: Want to use Docker without Docker Desktop? Follow these step-by-step-configurations.
image: https://blog.avenuecode.com/hubfs/ian-taylor-jOqJbvo1P9g-unsplash.jpg
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [Running Docker in a Vagrant VM](https://blog.avenuecode.com/running-docker-in-a-vagrant-vm)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Rafael Romão](https://blog.avenuecode.com/running-docker-in-a-vagrant-vm/author/rafael-rom%C3%A3o?hsLang=en-us)

*schedule* 12/29/21 2:00 PM

Learn how to install and configure Docker Engine in a Vagrant VM, plus connect it to your local Docker CLI and your preferred IDE.

Whether or not you are planning to deploy the software you're developing to containers, using [Docker](https://www.docker.com/) to run them, along with the services Docker depends on, in your local developer machine can be extremely helpful to test integration scenarios and avoid problems when the software is deployed to a remote environment.

This is something that's easy to do using [Docker Desktop](https://www.docker.com/products/docker-desktop), but what if you just need the [Docker Engine](https://docs.docker.com/engine/) and the [Docker CLI](https://docs.docker.com/engine/reference/commandline/cli/), and maybe a plug in to your favorite IDE? Can we do this without Docker Desktop?

In this article, I will show you how to install and configure Docker Engine in a Vagrant VM, as well as how to connect it to your local Docker CLI and to two of the most popular IDEs we have today: [IntelliJ](https://www.jetbrains.com/idea/whatsnew/) and [VS Code](https://code.visualstudio.com/docs/containers/overview).

Note: I will assume you are using a Mac operating system with ZSH as the default shell, but these steps can be easily modified to work on other operating systems.

##### Setting Up Your Computer

The first thing you have to do is to make sure Docker Desktop and Docker CLI are not installed on your Mac, since later you will have to install the Docker CLI on its own. If you need to uninstall these, follow the steps outlined [here](https://docs.docker.com/desktop/mac/install/#uninstall-docker-desktop).

You will also need Homebrew to proceed with the steps below. You can see how to install it [here](https://brew.sh/), or simply run the command below:

```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

Now you need [Vagrant](https://www.vagrantup.com/). Vagrant is a tool that helps us to install and manage virtual machines, and it is in one of these virtual machines, a Ubuntu one, that our Docker Engine will be running.

First, install Docker CLI, VirtualBox, and Vagrant with the commands below.

```
brew install virtualboxbrew install vagrantbrew install dockerbrew install docker-compose
```

This last command will install only Docker CLI, not the Docker Engine or Docker Desktop, so if you try to use Docker now, you will receive the following error message:

```
docker run hello-worlddocker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.See 'docker run --help'.
```

##### Setting Up Vagrant

Now that you have everything that you need on your Mac, it's time to install what goes into Vagrant. First, let's create a VM to host Docker Engine. An important note here is that it will create a Vagrantfile in the current folder, so it might be a good idea to move to your home folder or create a particular folder to host it.

```
cd ~vagrant init ubuntu/focal64
```

The Vagrantfile in the current folder will contain the VM settings. You will probably need to configure some port forwardings in this file, as shown in the example below:

```
config.vm.network "forwarded_port", guest: 3000, host: 3000
```

Now ssh into the VM and install the [Docker Engine](https://docs.docker.com/engine/install/ubuntu/).

```
vagrant upvagrant sshcurl -fsSL https://get.docker.com -o get-docker.shsudo sh get-docker.shsudo apt install docker-compose
```

You will also need to create a user group for Docker and add the Vagrant user.

```
sudo addgroup dockersudo adduser vagrant docker
```

Now exit the VM and reload.

```
exitvagrant reload
```

Docker Engine and Docker CLI should be working inside the VM now. Let's test it.

```
vagrant sshdocker run hello-world
```

Now let's go back to the host and configure it to connect to the Docker Engine that is running inside the VM.

```
exit
```

Add the Vagrant ssh key to the host known keys and configure Docker CLI to use the Vagrant VM.

```
export DOCKER_HOST=ssh://vagrant@127.0.0.1:2222ssh-add --apple-use-keychain ~/.vagrant/machines/default/virtualbox/private_key
```

If you are running an old version of MacOS, chances are that you should replace the param --apple-use-keychain by the param -K.

Now run the commands below to make the two previous commands run when you start a new terminal. Adjust it to use the -K param if necessary.

```
echo -n "\nexport DOCKER_HOST=ssh://vagrant@127.0.0.1:2222\n" >> ~/.zshrcecho -n "\nssh-add --apple-use-keychain ~/.vagrant/machines/default/virtualbox/private_key\n" >> ~/.zshrc
```

And reload your .zshrc to make sure it is working:

```
source ~/.zshrc
```

Now you should be ready to use Docker CLI in the host:

```
docker run hello-world
```

In case you have an error in the output of this command, with the message below, try running the command specified in the message:

```
 ssh -l vagrant -p 2222 -- 127.0.0.1 docker system dial-stdio
```

and it should fix the problem.

docker: error during connect:

```
Post "http://docker.example.com/v1.24/containers/create": command [ssh -l vagrant -p 2222 -- 127.0.0.1 docker system dial-stdio] has exited with exit status 255, please make sure the URL is valid, and Docker 18.09 or later is installed on the remote host: stderr=Host key verification failed.
```

The last step is to test Docker Compose. Go to some project that has a properly configured docker-compose.yml file and spin that up:

```
docker-compose up
```

If you have a problem with credentials when running docker-compose, you need to make sure the credsStore is not set in the file ~/.docker/config.json. If it is, remove it. Just delete the json entry, save the file, and try again.

##### Testing Docker Compose

The last step is to test Docker Compose.

First, you need to make sure the credsStore is not set in the file ~/.docker/config.json. If it is, remove it. Just delete the json entry.

Now Docker Compose should be working fine. Go to some project that has a properly configured docker-compose.yml file and spin that up:

```
docker-compose up
```

##### Configuring IntelliJ

Docker CLI is ready, so now it's time to configure the IDEs.

Let's start by configuring IntelliJ to connect to Docker.

In the Docker settings, add a Docker configuration using SSH with the Host 127.0.0.1, port 2222, and OpenSSH config as authentication type:![](https://lh6.googleusercontent.com/0Xrnpq3XOgLTHXXmAhESD9ItE8rMhOkYrpwKSezLqObKFp0TeW-yHnpvXCycOBmT_s3etYv0RUyctVLcGqgPhdKwe3gwN8zPRCAMfZEhuTcA2UVO9D-YnrEWkUv_0yV0ya2yEqpd)

![](https://lh3.googleusercontent.com/XCQbVvUP0adB4QQsJz1P8aHOtvFipVRRRcRRcQoO45UH73984MqHc5adxlOtxRZmGfWsdsx0wPVAdqiQSB2AFj5iSYEO934dR5YuKJ5dLfbNwoKmXYKBn57JvC-nLkBQbuicgQpf)

Now let's configure IntelliJ to use Docker Compose via SSH.

In the run configuration of the Docker Compose file, set the DOCKER\_HOST env var:![](https://lh3.googleusercontent.com/0o_-qk-l6kWhoZzn19Sj4KMVgzKvtwNZlSu8D8sNrsv66GkqQhduBmSVUZxTjEW-7srILdaPENuoc210b4ek0EN2-jfR1VCVz8zZ90HkqbMv71sYJtIyQFc5fKvOMxsSJo44fr83)

##### Configuring VS Code

If you use VS Code, it is even simpler. With the Docker CLI properly configured, Visual Studio should connect to Docker without problems and the Docker Explorer should show images and containers; you can run Docker commands as usual.![](https://lh5.googleusercontent.com/E2sxM9g5fQzxkYmmX4kAbCzWebIGwgM485sb8X422ohawh-6GymHs1yQUXWnfuH1PhE3VX0Nl65rfmatQSs3EaoK6CqFh7CB6yY_k4EEOgrhYjaE0Qw92HU5bDZuPFMI_cbUsENL)

But you can also configure VS Code to work from the Vagrant VM, using Remote Development:

Install [Remote SSH extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh).

Click on the button "Open a Remote Window," in the lower lefthand corner, select "Connect to Host," and then select "VagrantVM." You are now connected to your remote Vagrant VM. Go to the Terminal and type 'docker ps' to test it.

That's it! You can now use Vagrant to host your Docker backend.

 

##### References

[GitHub Gist, Set up a Vagrant VM for docker-based development](https://gist.github.com/mattbell87/ccacccd220249a4f8917dfc04e3b2a89)

- [docker docs, Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/)

[Linux Handbook, How to Set Up Remote Access to Docker Daemon \[Detailed Guide\]](https://linuxhandbook.com/docker-remote-access/)

---

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png)](http://twitter.com/rmromao) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png)](http://linkedin.com/in/rafaelromao)

### Author

# Rafael Romão

 Rafael Romão is a Software Engineer at Avenue Code. He has studied software development since 1998 and has worked as a software developer since 2002. His main areas of interest are software architecture and design, as well as the development of highly productive teams.

---

### Related Posts

### Asymmetric Cryptography

[READ MORE](https://blog.avenuecode.com/asymmetric-cryptography?hsLang=en-us)

### Improving the developer experience when documenting

[READ MORE](https://blog.avenuecode.com/improving-the-developer-experience?hsLang=en-us)

### How to Run Rust from Python

[READ MORE](https://blog.avenuecode.com/how-to-run-rust-from-python?hsLang=en-us)

### How to Create a Tic-Tac-Toe Board Using React.js

[READ MORE](https://blog.avenuecode.com/how-to-create-a-tic-tac-toe-board-using-react.js?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenuecode/)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![Avenue Code-primary versions_logo white avenue code endorsement 2](https://blog.avenuecode.com/hs-fs/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png?width=1180&name=Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png "Avenue Code-primary versions_logo white avenue code endorsement 2")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Rafael Romão",
    "url" : "https://blog.avenuecode.com/author/rafael-romão"
  },
  "dateModified" : "2022-01-14T16:00:24.716Z",
  "datePublished" : "2021-12-29T17:00:00.000Z",
  "headline" : "Running Docker in a Vagrant VM",
  "image" : [ "https://blog.avenuecode.com/hubfs/ian-taylor-jOqJbvo1P9g-unsplash.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/running-docker-in-a-vagrant-vm",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```