Let's try: Docker-compose

Let's try: Docker-compose

We once have talked about docker (link below) and https://hub.docker.com, where we can find millions of images we can start from.

Try Docker
Docker is one of elementary tools of all developers, I guess. We can manage to build an environment with less resources than Virtual Machine and need more command line skill to deal with.

But that is just an image where we can get and build a single container. How about we want to create one ourselves and then a cluster of many different containers? The answer is Dockerfile and Docker-compose.

Dockerfile vs Docker-compose

Dockerfile is a file named "Dockerfile" where we can start creating a base image to put our programs or scripts inside to do one task.
For example, we can create a Dockerfile to build an image then a single container for a simple "Hello World" webpage using NodeJS as a base image.

While Docker-compose is a file named "docker-compose" as well but with extension "yaml" to be "docker-compose.yaml". This is for creating an environment of one or many containers.
For example, we want a simple website using NodeJS that can show a table of goods inventory where is storing in PostgreSQL database. This means we will have 1 container of NodeJS and 1 container of PostgreSQL.

Sample Docker-compose file

This blog we can start from a simple one. Creating 2 Debian containers. One with curl package installed will be created after another without it.

So we start from here.

1. create a dockerfile

We expect a Debian image with curl. It is a custom image so we need to create it ourselves as below. Simple.

We defined this image to install curl at line #3 there.

At this step, we now are able to create a container using the command.

docker build . -t <image_name>:<image_version>
docker run -it <image_name>:<image_version>

2. create a docker-compose.yaml

Now we can start writing a docker-compose.yaml. In case you are new, YAML is a file popular at defining configuration with similarity as JSON but no brackets to frustrating our eyes and a bunch of features such as anchoring a node to use later.

We can design its behaviors. How many containers? How they connect? Where are shared volumes? Etc. they are built on our wants.

For this example, we put only 2 containers; "debian1" & "debian2". You can see it is stdin_open and tty to allow the container can run interactively.

For more information about the configurations, can take your time follow the link below.

Compose specification
Compose file reference

3. up

When the configuration files are ready, next is to start up the machine. Make sure that we put both in same directory and Docker app is online.

docker-compose up -d

-d flag means detached mode or run in background.

The terminal will run and build images then containers like this.

and there are 2 containers as planned.

4. down

When everything is done, run this command to stop the container.

docker-compose down

or this command to wash away all resources including mounted volumes if any.

docker-compose down --volumes --rmi all

Here is my github of this sample dockerfile and docker-compose.yaml

GitHub - bluebirz/sample-docker-compose
Contribute to bluebirz/sample-docker-compose development by creating an account on GitHub.
Show Comments