Getting started with the many ways to Docker

This is a followup on how to use Docker after building a Swarm cluster. I think it is important for people to understand the different ways to create containers and choose the best way for their needs.This blog post will explain docker-compose, docker engine, and how to do persistent storage.

[Docker Compose]

Let’s begin with docker-compose. This utility allows a user to create a manifest file of all the containers needed and how they communicate with each other. This example will show you how to create a MySQL container and connect it to a web application called nodechat.

 

Download the sample docker-compose.yml in a new directory. Below is the contents of the file for reference.  Since YAML files are space sensitive and not easy share in a blog post, please do not copy and paste the contents below.

 

docker-compose.yml:

mysql:
image: rusher81572/mysql
restart: always

nodechat:
image: rusher81572/nodechat
restart: always
ports:
– 8080:8080
links:
– mysql:mysql

 

Type the following command to create the containers.

docker-compose up

A lot of output will be displayed on the screen next and may not bring you back to a terminal prompt. It is safe to press ctrl+c when you see the following:

nodechat_1 |
nodechat_1 | listening on *:8080
nodechat_1 | Creating Database…..
nodechat_1 | Creating Table……

Now that the containers have been created, it is time to start them.

docker-compose start

Run the following command to find out which host is running nodechat with:

docker ps

 

Use your web browser to navigate to the host running nodechat on port 8080. Feel free to chat with yourself =)

 

This is how you can stop and remove your running containers built with the compose file:

docker-compose stop

docker-compose rm -f

 

[Docker Engine]

Now let’s run the equivalent Docker engine commands to accomplish the same result as the docker-compose file so you will have a better understanding on how Docker works.

 

Pull the image from the repository:

docker pull rusher81572/mysql
docker pull rusher81572/nodechat

Run the containers in daemon mode (In the background) with -d. The -p argument exposes a port for outside access. The format for -p is outside_port:inside_port. The “name” argument specifies a container name. This will allow us to link the nodechat application to the MySQL container simply by using a name. The”link” argument links the MySQL container to Nodechat using the container name. This will allow connectivity between nodechat and MySQL to store the chat data. The format for “link” is:  container_name:link_name.

docker run -d –name mysql rusher81572/mysql

docker run -d –link mysql:mysql -p 8080:8080 rusher81572/nodechat

(If you have any issues copying and pasting the above commands….There should be two “-” before name and link. For some reason, WordPress changes them to a single minus sign)

Find out what host is running nodechat with “docker ps” and use your web browser to navigate to the host running nodechat on port 8080

 

[Dockerfile’s]

Dockerfile’s contain all of the steps needed to create a container such as adding files, defining volumes, installing software, and setting environment variables. The following steps will explain how to create persistent storage for containers by creating a container to share volumes with other containers.

 

Create a directory called “fileserver” with a file called “Dockerfile” with the following contents:

FROM ubuntu
VOLUME /share
CMD sleep infinity

Build the filesever container. The -t argument specifies the tag for the container which is basically a name for it.

docker build -t fileserver .
mkdir data

Run the container in daemon mode. The -v argument allows you to share a local directory inside the container as a volume. Replace location_to_data_dir with the full path to the data directory created in the previous step.

docker run -d -v location_to_data_dir:/share –name fileserver fileserver

(If you have any issues copying and pasting the above command….There should be two “-” before name)

 

Now we have a container named fileserver that can share volumes with other containers. The files will be store locally in the data directory. To create a client, create a directory called “fileserver-client” with a file called “Dockerfile” with the following contents:

FROM ubuntu
CMD sleep infinity

Build the fileserver-client container image.

docker build -t fileserver-client .

Now let’s run the fileserver-client container in interactive mode to create a test file. Interactive mode runs a container in the foreground so you can see what is happening and even interact with the shell. The argument “volumes-from” will mount all of the volumes from the container specified. Please note that the container will stop and return you to the shell after running the command.

docker run -it –volumes-from fileserver fileserver-client touch /share/foo.txt

(If you have any issues copying and pasting the above command….There should be two “-” before volumes-from)

 

Run another fileserver-client container to see list of files on the fileserver.

docker run -it –volumes-from fileserver fileserver-client ls /share

Check to ensure that the files are being stored locally.

ls location_to_data_dir/data

The file should be displayed in the terminal. Feel free to play around with this more. I hope that you learned something new today.

containersdocker