Docker Usage

Recently I discovered Docker, which is an open platform for distrubuted applications for developers and sysadmins. Lets dive right in and start using it!

Images

The Docker engine manages containers. To create a container, we first need an image. Images are hosted on dockerhub.com. To generate an image, we will use a Dockerfile. This file will install programs within our image and do any setup that we need.

In the Dockerfile, specify a FROM which points to your base image. This is what your image will be based upon. You can then specify a RUN command to execute on startup of your container. Keep in mind that if you specify a RUN command while starting the container it will override the RUN specified in the Dockerfile.

ADD will copy files or directories from the host machine into your container.

VOLUME will bind a directory on your host machine to the container. This will allow you to add/remove files from this host volume and it will transale into the container.

To list all of your images:

docker images

To remove all images:

docker rmi $(docker images -q)

This may require stopping containers:

docker stop $(docker ps -a -q)

If you wish to remove these containers:

docker rm $(docker ps -a -q)

Now we can build our image!

docker build -t myTag myImage

This will create an image called myImage tagged as myTag. We are now ready to create a container based off of this image.

Containers

Alright, enough about images. Time to make a container!

We want to run a container based off our image created earlier. This is done with the RUN command.

docker run -it -v /var:/etc myTag /bin/bash

This will connect to the container via interactive console with -i. You can then explore the filesystem and your installation.

-t allows calling by tag and -v sets the volume making /var on the host machine map to /etc within the container.

/bin/bash will puts you into bash. This can also point to a script or run a command. Keep in my this will override the CMD within the Dockerfile.

To detach, use ctrl+p then ctrl+q.

To view all containers:

docker ps -a

To view only running containers:

docker ps

Attaching to a container:

docker attach <id>

You can also run docker in detached mode while will keep the container running in the background. This can be done with:

docker run -t myTag -d

All I/O should be done through network connections or shared volumes because the container is no longer listening to the command line.

Hopefully this will get you a good start on Docker. Look for posts showing docker with node!