Skip to content

Instantly share code, notes, and snippets.

@sarink
Last active January 28, 2017 21:34
Show Gist options
  • Save sarink/bb9632e9fb06f614aaf2e715bc8b92ae to your computer and use it in GitHub Desktop.
Save sarink/bb9632e9fb06f614aaf2e715bc8b92ae to your computer and use it in GitHub Desktop.
docker-readme

Docker for <project_name>

Overview/Things to Know

Machine

Our machine name is "<company_name>"

A docker machine is the underlying virtual machine that your container runs on. You shouldn't really ever have to worry about it.

Image

Our image name is "<company_name>/<project_name>"

A docker image is a stand-alone file that you use to build containers

Container

Our container name is "<project_name>"

A docker container is an instance of an image

Destroying

If you mess up, or want to experiment, there is a docker-destroy-all.sh script that has some useful commands in it. Beware, if you just execute it, it will destroy all images and all containers.

Commands

docker ps - Shows a list of all your running containers

docker ps -a - Shows a list of all your containers

docker images - Shows a list of all images you've downloaded

docker run <options> <image_name> - Create/name a new container and attach to it.
You'll almost always want to use the -v (mount volume), --name (name), -p (map ports), -t (tty), and -i (interactive) options. See docker run --help for a list and explanation of the options.

docker exec <options> <container_name> <command> - Execute a command inside a container (it must have been created and named with run first). If you want to run a shell, you can just use bash as the command. Again, you'll most likely want to use the -t and -i options. If you supply -d you can run the command in the background. See docker exec --help for more details.

docker start <container_name> - Starts a container by its name (it must have been created and named with run first)

docker attach <container_name> - Attaches you to a running container (it must have been started first. This command should be almost instant, if it looks like it's hanging, it's probably just your terminal being weird. Hit enter a few times, because you're most likely already attached)

docker stop <container_name> - Halts your container (start and then attach to get back into it)

docker-machine ip <machine_name> - Returns the IP address of the docker machine (the name of the machine we're using for this setup guide is <company_name>)


OSX instructions

Install docker

Follow the instructions on the docker install page

Set up a new docker machine called "<company_name>"

docker-machine create --driver virtualbox --virtualbox-memory 1024 <company_name>
docker-machine env <company_name>

Now, docker-machine ls should show your new "<company_name>" machine as running

Run the following command (you may find it useful to just add it to your .bash_profile):

eval "$(docker-machine env <company_name>)"

Build the Image

Navigate to the root of the repository (<repo_folder_name>) directory on your host

cd /path/to/the/repo

Run the following command to build the docker image:

docker build -t <company_name>/<project_name> .

Create a container from our <company_name>/<project_name> image

Name the container <project_name>, mount the current directory (which should still be <repo_folder_name>) as /src on the container, forward port 3000 to 3000:

docker run -it --name=<project_name> -v "$(pwd)":/src -p 3000:3000 <company_name>/<project_name>

Now you should be inside of the container, and you should see a /src directory that has the whole project.

If you run docker ps (from your host OS, of course, currently you're inside the container) you should see your "<project_name>" container running

Set up the app

Navigate into the /src directory and run:

npm install

Now you should be able to navigate to http://localhost:3000 and see the app running!

The Test

From your favorite IDE in OSX, go modify a JSX file, and observe the react-hot-loader working in the browser. The files should immediately sync from your host to the container.


Windows instructions

Install docker

Follow the instructions on the docker install page

Set up a new docker machine called "<company_name>"

docker-machine create --driver virtualbox --virtualbox-memory 1024 <company_name>

Enable symlinks in VirtualBox

One of the npm dependencies in the project tries to add a symlink when running building npm packages.

Navigate to the <repo_folder_name> directory on your host and run:

C:/Program\ Files/Oracle/VirtualBox/VBoxManage setextradata <company_name> VBoxInternal2/SharedFoldersEnableSymlinksCreate/$(pwd) 1

You may need to restart VirtualBox and run as administrator for settings to take effect. It might also be necessary to run Docker Quickstart Terminal as administrator as well.

For more help see here

Setup port forwarding for viewing Karma tests in the browser

C:/Program\ Files/Oracle/VirtualBox/VBoxManage controlvm "<company_name>" natpf1 "tcp-port9876,tcp,,9876,,9876";

Set environment variables so docker commands refer to our <company_name> machine

After you have the machine created, run

eval "$(docker-machine env <company_name>)"

see these instructions for more info.

Now, docker-machine ls should show your new "<company_name>" machine as ACTIVE

Build the Image

Run the following command:

docker build -t <company_name>/<project_name>

Now, docker images should show the "<company_name>/<project_name>" image in the list

Create a container from the "<company_name>/<project_name>" image

We need to share the <repo_folder_name> directory between Windows and the Docker image so that we can edit code. By default, boot2docker knows about the User directory. Anything outside of it will not mount.

See here if you need your <repo_folder_name> project to reside outside the User directory. (I have been unsuccessful at getting this to work)

Assuming you are in <repo_folder_name> and that <repo_folder_name> is a subdirectory of the Windows User directory, run:

docker run -it --name=<project_name> -v /$(pwd):/src -p 3000:3000 <company_name>/<project_name>

You should now be inside your freshly created docker container.

ls

should yield the contents of <repo_folder_name>

Set up the app

In the /src directory run:

npm install

Now you should be able to navigate to http://localhost:3000 and see the app running!

The Test

From your favorite IDE in Windows, go modify a JSX file, and observe the react-hot-loader working in the browser. The files should immediately sync from your host to the container.

Some commands to remember once you've set up docker but have restarted your computer

list the docker machines, start <company_name>, set <company_name> as the machine that docker will run commands against

docker-machine ls
docker-machine start <company_name>
eval "$(docker-machine env <company_name>)"

start the <project_name> container on the <company_name> machine, attach the console to the running container. now we're "in"

docker start <project_name>
docker attach <project_name>

Ubuntu (running in a VM on Windows) instructions

Install docker

SSH into your Ubuntu VM, and follow the instructions on the docker install page

Build the Image

On your VM, navigate to the root of the <repo_folder_name> repository

Run the following command:

docker build -t verizon/iptv .

Now, docker images should show the "<company_name>/<project_name>" image in the list

Create a container from our "<company_name>/<project_name>" image

Name the container <project_name>, mount the current directory (which should still be <repo_folder_name>) as /src on the container, forward port 3000:

docker run -it --name=<project_name> -v "$(pwd)":/src -p 3000:3000 <company_name>/<project_name>

Now you should be inside of the container, and you should see a /src directory that has the whole project.

If you run docker ps (from your VM, of course, currently you're inside the container) you should see your "<project_name>" container running

Set up the app

From the container, navigate into the /src directory and run:

npm install

Next, go into your VirtualBox settings and forward port 3000 from your Windows OS to port 3000 on your Ubuntu VM

Now, if everything worked correctly, you should be able to navigate to http://localhost:3000 on your Windows OS and see the app running!

The Test

From inside your Ubuntu VM, go modify a JSX file, and observe the react-hot-loader working in the browser. The files should immediately sync to the container.


Ubuntu (native) instructions

Install docker

Follow the instructions on the docker install page

Build the Image

Navigate to the <repo_folder_name> directory on your host

Run the following command:

docker build -t <company_name>/<project_name> .

Now, docker images should show the "<company_name>/<project_name>" image in the list

Create a container from our <company_name>/<project_name> image

Name the container <project_name>, mount the current directory (which should still be <repo_folder_name>) as /src on the container, forward port 3000 to 3000:

docker run -it --name=<project_name> -v "$(pwd)":/src -p 3000:3000 <company_name>/<project_name>

Now you should be inside of the container, and you should see a /src directory that has the whole project.

If you run docker ps (from your host OS, of course, currently you're inside the container) you should see your "<project_name>" container running

Set up the app

Navigate into the /src directory and run:

npm install

Now you should be able to navigate to http://localhost:3000 and see the app running!

The Test

From your favorite IDE on your Ubuntu host OS, go modify a JSX file, and observe the react-hot-loader working in the browser. The files should immediately sync from your host to the container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment