Need an expert to help you with
" How to install and use Docker Compose on Ubuntu 14.04" ?
" How to install and use Docker Compose on Ubuntu 14.04" ?
How to install and use Docker Compose on Ubuntu 14.04
IntroductionDocker is an amazing tool, however, in order to take full advantage of its potential it would be better if every component of your application ran in its own container. For more advanced applications, those which have a lot of components, telling all the containers to start up and shut down at the same time could quickly become inconvenient. The community of Docker created an excellent solution which quickly became popular; this solution is called Fig and allows you to use a single YAML file to orchestrate with your Docker containers and configurations. This solution was so popular that, eventually, the Docker team made their own version based on the Fig source which is named Docker Compose. This basically deals with the orchestration procedure of the Docker containers like starting up, shutting down, as well as setting up intra-container linking and volumes. At the end of this tutorial you should have Docker and Docker Compose installed, along with a basic understanding of how Docker Compose works.
Docker and Docker Compose ConceptsWhen using Docker Compose, it will need a combination of several different Docker concepts in one. Before beginning, we will take a minute to review the different concepts involved. If you are familiar with the docker concepts such as volume, links, and port forwarding then you will want to skip over to the next part of the tutorial.
Docker ImagesEvery Docker container is basically a local instance of a Docker image. You could think of a Docker image as a complete Linux Installation. Normally a minimal installation includes the bare minimum of packages which are required to run the image. Those images use the kernel of the host system, however, they are running within a Docker container and are only capable of seeing their own file system. It is very possible to run a distribution like CentOS on an Ubuntu host, this works both ways. Many Docker images are given from the Docker Hub which is managed by the Docker team. Many popular open source projects require an equivalent image uploaded to the Docker Registry which you can use to deploy the software. Once possible it is important to obtain ‘official’ images which means they are guaranteed by the Docker team to follow Docker best practices.
PrerequisitesFor this tutorial you are required to have the following:
- Ubuntu 14.04 VPS
- A non-root user with sudo privileges, Initial Server Setup with Ubuntu 14.04explains how to set this up.
wget -qO- https://get.docker.com/ | shThis command above will download and run a small installation script made by the Docker team. If the user is not configured correctly with Docker it can become a pain; make sure to add the user to the docker group using the following command.
sudo usermod -aG docker $(whoami)Log out of your server before logging in again to enable your new groups. Step 2 — Installing Docker Compose Now that you have Docker installed, you can go ahead and install Docker Compose. First, install ‘python-pip’ as a prerequisite which you will need.
sudo apt-get -y install python-pipNow you may install Docker Compose with the following.
sudo pip install docker-composeStep 3 — Running a Container with Docker Compose In the public Docker registry, Docker Hub, it has a basic ‘Hello World’ image. Since you now have Docker Compose installed, you can try it out with this basic example. Begin by creating a directory for your YAML file.
mkdir hello-worldAfterwards, change into the directory.
cd hello-worldNow open the ‘YAML’ file with your favorite text editor. We will be using nano.
nano docker-compose.ymlPlace the following contents into the file before saving and exiting the text editor.
docker-compose.yml my-test: image: hello-worldAbove, you will need to know that the first line is used as part of the container name. The second line makes sure what image to use to create the container. The image should be downloaded from the official Docker Hub repository. As you are still in the ‘~/hello-world’ directory, use the following command to make the container.
docker-compose up This output should show up: Output of docker-compose up Creating helloworld_my-test_1... Attaching to helloworld_my-test_1 my-test_1 | my-test_1 | Hello from Docker. my-test_1 | This message shows that your installation appears to be working correctly. my-test_1 |This output explains what Docker is doing:
- The Docker client has contacted the Docker daemon.
- The Docker daemon pulled the ‘hello-world’ image from the Docker Hub.
- The Docker daemon made a new container using that image as it runs the executable which provided the output you are currently reading.
- The Docker daemon directed that output to the Docker client, then it was sent to your terminal.
docker-compose up –ddocker-compose should now fork to the background. To reveal your group of Docker containers, even if it is stopped or already running, use the command shown below. As an example, we will show you in the following that the ‘helloworld_my-test_1’ container has stopped.
Output of `docker-compose ps` Name Command State Ports ----------------------------------------------- helloworld_my-test_1 /hello Exit 0If it is a running container, it should show the ‘Up’ state.
Output of `docker-compose ps` Name Command State Ports --------------------------------------------------------------- nginx_nginx_1 nginx -g daemon off; Up 443/tcp, 80/tcpTo end all running Docker containers for an application group, use the below command within the same directory as the ‘docker-compose.yml’ file which was used to start the Docker group.
docker-compose stopIn certain cases, Docker containers are going to store their old information in an internal volume. If you would like to start from scratch, you may use the ‘rm’ command to completely remove all the containers which made up your container group.
docker-compose rmIf you attempt any of those commands from a directory different than the directory which has a Docker container and a ‘.yml’ file then it will not show you your containers like the below output.
Output from wrong directory Can't find a suitable configuration file in this directory or any parent. Are you in the right directory? Supported filenames: docker-compose.yml, docker-compose.yaml, fig.yml, fig.yaml
ConclusionThis should cover all of the basic concepts of Docker Compose and how you can get it installed and running.
Updated on 28 Nov 2017
Add a comment