Image for post
Image for post

Docker: Using Shell or Docker-Compose

Comparison of Docker Commands and Docker-Compose Tool

Docker is a great tool to pull down pre-packaged images of services to run on your system. This is a small tutorial that shows how to quickly run a WordPress application using either shell commands with the docker tool, or using Docker Compose tool uses DSL in a YAML file to do the same thing.

Prerequisites

You will need the following tools for this tutorial:

  • Docker service
  • Docker client
  • Docker-Compose

Docker runs natively on Linux, so installation is rather straight forward process for popular distributions:

After installing and enabling Docker for your current user, get Docker Compose. If you have Python and Pip installed, you get simply with:

pip install docker-compose

On macOS, you will need to run a virtual guest that will host the docker service. For the virtual guest, you can either use Docker Machine, which uses Virtualbox by default, or you can use Docker-for-Mac, which uses HyperKit (based on xhyve). You can actually install both of these tools, and decide, which one to use.

For Docker Machine and all the required components, you can get these with Docker Toolbox:

For Docker-for-Mac, you can get this from here:

You will need to get Docker-Compose for Part II. If you installed Docker Toolbox, this is already included, otherwise you need to install it separately.

  • Docker Compose: brew install docker-compose if Homebrew is installed or install with python pip: pip install docker-compose.

For Windows, , you will need to run a virtual guest that hosts the Linux docker service. You have two options for this. The first option is to use Docker Machine that uses by default Virtualbox, but can target Hyper-V. Docker-for-Windows is the second option, which only uses Hyper-V. You can only use Hyper-V or Virtualbox on Windows, but not both.

For Docker Machine and all the required components, you can get these with Docker Toolbox:

For Docker-for-Windows:

You will need to get Docker Compose for Part II. If you installed Docker Toolbox, this is already included, otherwise you need to install it separately.

Part I: Using the Shell to Automate Docker

This part uses the docker command in the shell to bring up a WordPress application.

After running these commands, you will create a mini-infrastructure that consists of two containers (WordPress and MySQL), a private network so that the containers can communicate to each other, and volume so that database data persists after the life of the container.

In a previous article, I did a step-by-step walkthrough describing each step.

The full script or shell commands below will create the WordPress application and supporting infrastructure.

We can try out the solution by pointing your browser to http://localhost:8080 if you run Docker natively or use Docker-for-Mac.

If you are using Docker Machine, then use docker machine ip to get the IP address, and navigate to that IP address at port 8080.

You can remove these containers through the following:

docker stop db
docker stop wordpress
docker rm db
docker rm wordpress

Part II: Using Docker-Compose to Automate Docker

As an alternative to running long docker commands, we can use a descriptive key-value pairs specified in a YAML file.

Docker Compose does some automation behind the scenes to connect these containers together, such as creating a network so that the containers can communicate to each other.

Below is the full docker compose script.

With Docker Compose installed, and a running Docker environment, we bring up the environment using the following command:

docker-compose up -d

You can remove these containers through the following:

docker-compose stop
docker compose rm

You can bring up each service individually and show the output in the console. This is useful for troubleshooting the service running inside the container. Two do this, bring up two terminal sessions, bring up a service in each terminal.

First terminal, start the database:

docker-compose up db

In the second terminal, start wordpress:

docker-compose up wordpress

If you are using Docker Machine, don’t forget to initialize the environment for each terminal, e.g. eval $(docker-machine env).

We can try out the solution by pointing your browser to http://localhost:8000 if you run Docker natively or use Docker-for-Mac.

If you are using Docker Machine, then use docker machine ip to get the IP address, and navigate to that IP address at port 8000.

Part III: Docker-Compose with Env Vars

This is an small alternative to the previous script. This demonstrates how do you environment variables to alter the behavior of the Docker Compose script.

You can setup default environment variables in a .env file, and optional override them by setting up environment variables before you run docker-compose command.

Note: Careful attention to not confuse this with container environment variables, which we setup as key:value pairs under the environment heading. These are environment variables to docker compose itself.

Similar to Part II, but this time, let’s change the port:

WP_HOST_PORT=9000 docker-compose up -d

We can try out the solution by pointing your browser to http://localhost:9000 if you run Docker natively or use Docker-for-Mac.

If you are using Docker Machine, then use docker machine ip to get the IP address, and navigate to that IP address at port 9000.

Final Thoughts

Docker is a great tool for quickly testing applications and services without the need to spend a lot of time to configure these locally. We did a comparison of both the vanilla docker command and using using Docker Compose to orchestrate containers.

Both methods are used for local development and testing, as well as Continuous Integration build environment. With support for environment variables, you can adjust the behavior of running containers for dev, test, or ci oriented environments.

For production, Docker Compose does not make sense, in case you thought of it, as it is oriented for local interactive usage. More robust automation, such as Ansible or Kubernetes are used for these scenarios.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store