Navigate to the Maven project directory on your workstation and launch the Maven container.
To run a Maven project by using the Maven Docker image directly, passing a Maven command to docker run.
docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install
To launch the Maven Docker container and open a Bash terminal in the container for an interactive session.
docker run -it --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 bash
Docker is an emerging technology to launch applications on any guest operating system using any host operating system. The same host can run multiple Docker containers of any type, so one physical or virtual server can serve as a complete mini cloud environment. Most of the time Linux (including macOS) is used as the host operating system.
In this example, we will launch a PostgreSQL Docker container on macOS, and access the PostgreSQL database server from a Windows VirtualBox virtual machine.
Thanks to Robert Lestak for the Docker scripts.
Create a network between the guest VM and the host in Oracle VirtualBox
A VirtualBox virtual machine can share the networking with the host with the NAT setting, or it can be an independent computer on the network with the Bridged setting.
To access the host directly from the guest VM we can set up a host-only network, that only the host and the guest can access. This way we can launch a container on the host and access it from the guest via the host-only mini-network.
To set up the private host-only network
- Open the VirtualBox application and shut down all virtual machines
- In the File menu select Host Network Manager…
- On the Host Network Manager window click the Create button
- Click the Enable checkbox and the Close button
Connect the virtual machine to the host-only network
The VirtualBox virtual machines can have four virtual network adapters. Always use the first adapter to connect to the host-only network.
- Select the virtual machine in the list and click the Settings icon
- On the Network tab click the arrow next to the Attached to drop-down
- Select the Host-only adapter
- The host-only network is automatically selected
- Select the second adapter, click the Enable Network Adapter checkbox, and select NAT
- Click the OK button to close the window.
Find the IP address of the host machine
- Open a terminal on the host machine.
- On Mac
- enter the following command. Use the IP address range of the host-only network above.
ifconfig | grep 192.168.56
inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255
- Based on the output above, the IP address of the host machine is 192.168.56.1
Launch a PostgreSQL Docker container
- Create a docker-compose.yml file. Containers are ephemeral, when we terminate them, all data stored inside the container is lost. We want to keep the database files on our local hard drive, so this configuration will map the /var/lib/postgresql/data directory of the Docker container to the pgdata subdirectory of the current working directory. The 5432 is the default port of the PostgreSQL database server, we will expose it on port 5433 of the host.
- Open a terminal window in the same directory and launch the Docker container
docker-compose up -d
Connect to the host machine from the guest machine
Test the connection
- Open a terminal on the guest machine
- Test the connection to the host machine
Configure an ASP.NET Core 2.1 application to connect to the Docker PostgreSQL database server
- Change the connection string, and specify the exposed port on the host machine: