Information:
Because Docker only runs on Linux you will need to install some kind of virtual instance on your local machine. An easy and popular way to do that is to install
Boot2docker and VirtualBox. VirtualBox is a dependency of Boot2docker. You can download, setup and install the latest versions from their websites or if you are using Homebrew, as you mentioned, you can quickly get the working binaries both in one step.
After installing boot2docker, you're ready to use Boot2docker to create a Tomcat Container. You can find a pre-configured tomcat image by searching Docker's community repository, docker hub registry.
Notes:
- Each time you execute the docker run command a new container is created.
- The VM running Docker requires a ssh private/public encryption key handshake to connect to. If you follow my steps below, one will be generated for you.
Steps to Setup Tomcat using the tomcat image:
- Open Terminal and run this command:
brew install boot2docker
- Create a new Boot2Docker VM instance using the init command:
boot2docker init
- Run this command in Terminal to forward local ports to the vm:
for i in {10000..10999}; do VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i”; VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i";done
- Start the boot2docker daemon:
boot2docker start
or boot2docker up
- After starting docker, copy the exports that are displayed from the previous command to your clipboard
- Edit your bash profile file ~/.bash_profile (or if you are using zsh, edit the resource configuration file ~/.zshrc) with a text editor (I prefer using Sublime text):
subl .zshrc
*note: this will permanently save the docker env variables.
- Paste the exports into that file and save
- Execute the source command on the file:
source .zshrc
- Pull the latest tomcat image to create a container and start tomcat:
docker run -it --rm -p 10080:8080 tomcat:8.0
*note: this will forward your local 10080 port to the vm's 8080 port.
- Go to http://localhost:10080, you should see the tomcat start page!
Useful Docker commands:
- $ boot2docker status
- $ docker version
- $ docker ps #shows running containers
- $ docker ps -a # shows all containers
- $ docker exec -it NAME /bin/bash #to start a bash session on the container. -i = interactive, -t = tty
External Resources: