Docker Compose applications are easy to share with your teams. You just need to define your application with Docker Compose once and use that same configuration to run the application on other machines to save your time.
Compose comes with all the necessary CLI commands for managing your application’s life cycle, like e.g. start, stop and rebuild services or view the status of running services. To see the environment variables check out this link but keep in mind that it is not recommended to use them for connecting to linked services. You should rather use the link name.
First you need to install Compose.
# curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`u name -s`-`uname -m` > /usr/local/bin/docker-compose
#chmod +x /usr/local/bin/docker-compose
or as Python package:
# pip install docker-compose
Verify your installation with
# docker-compose --version.
Basically there are just three steps to use Compose and run your entire app with only one command.
- Define the Dockerfile for your app that defines its whole environment.
- Define the services that make up your app, so they can be run in an isolated environment. This will be done in docker-compose.yml.
- Run docker-compose up.
In this file you specify your services. Each service must specify exactly one of image or build, whereas image can be the a remote or local tag or the partial ID and build specifies a path or directory containing a Dockerfile. If the path is a relative path it will be relative to the docker-compose.yml itself.
You do not need to specify options such as CMD or VOLUMES in docker-compose.yml because docker run` respects these options from the Dockerfile. Further options are e.g. to link containers, expose ports or mount paths as volumes.
Let’s take an example of creating webserver with DB, in that for building webserver image will use Dockerfile and for DB will use some image from registry. To specify these services, link everything together and describe which Docker images your docker-compose.yml should look something similar like this:
web: build: . volumes: - '.:/code' ports: - 8883:80 links: - db - cache db: image: postgres:latest command: '-d' ports: - 5432:5432 cache: ports: - 6379:6379
To build the project use docker-compose run. The command
# docker-compose run web project1 testing .
will build an image for the web service with the given Dockerfile. The second part will run inside a container built using this image. The next step is to set up the correct database connection. from postgres Docker image.
Finally, you run the command docker-compose up which will link everything together and that’s it!
Read More: Docker Compose Networking – Explained