/How to use Docker Compose files without the docker-compose command

How to use Docker Compose files without the docker-compose command

Find out how you can use docker-compose.yml files without the docker-compose command.

How to use Docker Compose files without the docker-compose command
Find out how you can use docker-compose.yml files without the docker-compose command.

Containers have become an integral tool in business technology. But for those new to containers, it can be a bit confusing. This is especially true when you realize how many different methods there are to deploying such apps and services onto your network. 

Do you use Docker, Docker Compose, or Kubernetes? What if you’ve opted to go the more flexible Docker Compose route, but don’t want to install the docker-compose command? Or maybe you have a number of files built for Docker Compose, but want to run them with Docker?

SEE: 10 things companies are keeping in their own data centers (TechRepublic download)

You can. Thanks to the docker stack command it is possible to make use of those docker-compose.yml files, to deploy a full stack of containers to your already-running Docker Swarm cluster, with only Docker installed. That means you have a choice between using a Dockerfile or docker-compose.yml file for the deployment of your container stacks.

But how do you do this? Let me show you.

What you’ll need

The only things you’ll need to make this work are:

  • A working installation of Docker
  • A deployed docker swarm cluster
  • A docker-compose.yml file to use

To find out how to deploy a Docker Swarm, read
How to create a Docker swarm

A quick caveat

Before you dive in, there’s one caveat you should know about first. Using the docker stack command will fail if your docker-compose.yml file depends on a Dockerfile to build an image. Such a docker-compose.yml section might look like:

     build: .
     - "8080:80"

The above will fail. Instead, you’d have to declare this section like so:

     image: nginx
     - "8080:80"

Our sample YML file

For the purposes of this demonstration, we’ll reuse the file from the “How to build a Docker Compose file” article (minus the build section). So our docker-compose.yml file will look like this:

Version: '3'
    image: nginx
     - "8080:80"
    image: mysql
    - "3336:3306"
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_USER=user
    - MYSQL_PASSWORD=password
    - MYSQL_DATABASE=demodb

You can change any of the MYSQL variables to suit your needs. We’ll save that file in a directory called ~/dockerbuild. 

Deploy with docker stack

That docker-compose.yml file could be deployed using the docker-compose up command. However, we’re going to do the same thing with the docker stack command. We’re going to name our stack nginxdb, so the command to deploy would be:

docker stack deploy -c docker-compose.yml nginxdb

The breakdown of the command is:

  • docker stack is the main command.
  • deploy indicates we are deploying the stack.
  • -c indicates that what follows is the configuration file to be used.
  • docker-compose.yml is the configuration file.
  • nginxdb is the name of the stack to be deployed.

The command should report back the following:

Creating service nginx_web
Creating service nginx_db

If we issue the command docker ps -a we can see that our two containers have been deployed (Figure A).

Figure A


One thing to know is that, with docker-compose, you have easy access to bringing down those containers with:

docker-compose down

Since we’re in Docker Swarm mode, to stop a container, you must scale it down to 0. To do this, you must know the name of the container, using docker ps -a and then scale it down with the command:

docker service scale NAME=0

Where NAME is the name of the running container(s) to be stopped.

And that’s the gist of using a docker-compose.yml file, without having to add docker-compose into the mix. This is just another tool in your container toolbox. Give docker stack a go and see if you prefer it to the docker-compose take on deploying containers.

Also see


Image: Docker

Original Source