docker logo

Coming To Grips With Docker
Published: 02-Sep-2018

I've used Docker for a few years now in various capacities, but I have never created a docker-compose file. The main reason being that I find the Docker documentation to be largely impenetrable.

What Is Docker?

Most simply, docker is a virtual machine that creates virtual servers on your laptop, or your cloud server. Or something in between.

With Docker, you can download images. From images, you can serve containers based on those images.

There are hundreds of images to choose from. Some of them run your favorite databases and web servers. Many of them may be configured to run stuff you've never heard of or thought to seek out.

Here's a quick guide to getting the best out of Docker.

The things you need to know:

  • you need to install Docker;
  • you also need to install an image of whatever it is you want to serve.

Let's start with CouchDB (just because).

I'm going to leave the how to with you, because this isn't too hard to work out from the documentation.

But, here are some hints:

https://docs.docker.com/install/
https://hub.docker.com/_/couchdb/

Next, we will build a re-useable CouchDB server which we can build and use over and over again in our development environment.

So, let's say our development environment is:

~/projects/test/

The "test" folder is our development environment located inside our projects folder. Let's now create a "database" folder within our development environment, so that we have:

~/projects/test/database

Within this database folder, we need to create two text files:

docker-compose.yml

Dockerfile

Use your favorite code editor (eg. VS Code or Atom)

docker-compose.yml

Enter the following inside this file:

version: "2"



services:

couchdb:

build: .

ports:

\- "5984:5984"

volumes:

\- ./local_couch_files:/opt/couchdb/data

environment:

\- COUCHDB_USER=admin

\- COUCHDB_PASSWORD=password



Version: I think this has to do with the version of docker-compose yml you are using.

Services: we're building a simple service - couchdb with nothing else connected.

Ports: here we list the default ports. The reason for two ports is that the Docker container that loads has an 'internal' port, and also a tunnel to an 'external' port (which your code and/or browser may connect to).

Volumes: this is really important. Creating a local volume means that our data can persist between sessions, giving us some predictability with regards to our test/development data.

Environment: should be fairly easy to comprehend what is going on here. Note that this is a development environment, and security may not be your primary concern.

Dockerfile

Enter the following inside this file:

FROM couchdb



 

Terminal - Starting Your Container

You will need to use the command prompt (the terminal) to do the next bit.

First, "cd" into your project directory:

~/projects/test/database

Next, run this command:

sudo docker-compose up

This will create a new container running CouchDB which will be running on http://localhost:5984. It will also place any data into the following directory:

~/projects/test/database/local_couch_files

That's right, the CouchDB data - instead of being stored inside the Docker container - is being stored locally.

Terminal - Stopping Your Container

Open a new terminal window (not the one running the Docker container). Next, "cd" into your project directory:

~/projects/test/database

Run this command:

sudo docker-compose down

What this does is drop and destroy the container. But what it does not do is destroy the local data!

This means you can create and destroy a server environment at will, but persist the data between sessions.

Which is awesome!

Note that this is not a particularly sophisticated Docker environment. It is possible to launch and connect more than one container together, or to run them as separate virtual servers. Docker is amazingly versatile and light-weight. But it's documentation is not that easy to digest (for my needs anyway).

docker logo

Coming To Grips With Docker
Published: 02-Sep-2018

I've used Docker for a few years now in various capacities, but I have never created a docker-compose file. The main reason being that I find the Docker documentation to be largely impenetrable.

What Is Docker?

Most simply, docker is a virtual machine that creates virtual servers on your laptop, or your cloud server. Or something in between.

With Docker, you can download images. From images, you can serve containers based on those images.

There are hundreds of images to choose from. Some of them run your favorite databases and web servers. Many of them may be configured to run stuff you've never heard of or thought to seek out.

Here's a quick guide to getting the best out of Docker.

The things you need to know:

  • you need to install Docker;
  • you also need to install an image of whatever it is you want to serve.

Let's start with CouchDB (just because).

I'm going to leave the how to with you, because this isn't too hard to work out from the documentation.

But, here are some hints:

https://docs.docker.com/install/
https://hub.docker.com/_/couchdb/

Next, we will build a re-useable CouchDB server which we can build and use over and over again in our development environment.

So, let's say our development environment is:

~/projects/test/

The "test" folder is our development environment located inside our projects folder. Let's now create a "database" folder within our development environment, so that we have:

~/projects/test/database

Within this database folder, we need to create two text files:

docker-compose.yml

Dockerfile

Use your favorite code editor (eg. VS Code or Atom)

docker-compose.yml

Enter the following inside this file:

version: "2"



services:

couchdb:

build: .

ports:

\- "5984:5984"

volumes:

\- ./local_couch_files:/opt/couchdb/data

environment:

\- COUCHDB_USER=admin

\- COUCHDB_PASSWORD=password



Version: I think this has to do with the version of docker-compose yml you are using.

Services: we're building a simple service - couchdb with nothing else connected.

Ports: here we list the default ports. The reason for two ports is that the Docker container that loads has an 'internal' port, and also a tunnel to an 'external' port (which your code and/or browser may connect to).

Volumes: this is really important. Creating a local volume means that our data can persist between sessions, giving us some predictability with regards to our test/development data.

Environment: should be fairly easy to comprehend what is going on here. Note that this is a development environment, and security may not be your primary concern.

Dockerfile

Enter the following inside this file:

FROM couchdb



 

Terminal - Starting Your Container

You will need to use the command prompt (the terminal) to do the next bit.

First, "cd" into your project directory:

~/projects/test/database

Next, run this command:

sudo docker-compose up

This will create a new container running CouchDB which will be running on http://localhost:5984. It will also place any data into the following directory:

~/projects/test/database/local_couch_files

That's right, the CouchDB data - instead of being stored inside the Docker container - is being stored locally.

Terminal - Stopping Your Container

Open a new terminal window (not the one running the Docker container). Next, "cd" into your project directory:

~/projects/test/database

Run this command:

sudo docker-compose down

What this does is drop and destroy the container. But what it does not do is destroy the local data!

This means you can create and destroy a server environment at will, but persist the data between sessions.

Which is awesome!

Note that this is not a particularly sophisticated Docker environment. It is possible to launch and connect more than one container together, or to run them as separate virtual servers. Docker is amazingly versatile and light-weight. But it's documentation is not that easy to digest (for my needs anyway).