Build Applications with Docker Compose Examples

Docker Compose is an orchestration tool for Docker that allows you to define a set of containers and their interdependencies in the form of a YAML file. You can then use Docker Compose to bring up part or the whole of your application stack, as well as track application output, etc. Build applications with docker compose and use it on your continues delivery.

If you have started working with Docker and are building container images for your application services, you most likely have noticed that after a while you may end up writing long `docker run` commands. These commands while very intuitive can become cumbersome to write, especially if you are developing a multi-container application and spinning up containers quickly.

docker compose example

Docker Compose also allows you to manage your application as a single entity rather than dealing with individual containers.

Install Docker Compose

You can install Docker Compose on macOS, Windows and 64-bit Linux OS.

Uset this command to install on your linux OS, download Docker Compose, replacing $dockerComposeVersion with the specific versions.

# curl -L https://github.com/docker/compose/releases/download/$dockerComposeVersion/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

Example to download Docker Compose in linux with docker 1.13

# curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker-compose --version
docker-compose version 1.13.0, build 1719ceb

 

Install using pip

If you install using pip, use this command but make sure python system packages that conflict with docker-compose dependencies.

# pip install docker-compose

Install inside a container

Simple bash script wrapper will install it.

# curl -L --fail https://github.com/docker/compose/releases/download/1.13.0/run.sh > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

Docker compose sample deployment

After you installed docker compose successfully, create a YAML file which will contain the docker image and environment details.

Create a docker-compose.yml file with your WordPress blog and MySQL container with persistence data using volume mount.

version: '2'

services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress_pa33

wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress_pa33
volumes:
db_data:

 

That’s it. Now, run this command from your project directory.

# docker-compose up -d

Once pull all images and start containers, wait for few seconds to initialize the database, etc and open http://MACHINE_IP:8000 in a browser.

Docker Compose example commands

You can start the containers with the up command in daemon mode (by adding -d as a param) or by using the start command:

# docker-compose start

Stopping containers

# docker-compose stop

Remove containers

To stop and remove all the containers use the down command

# docker-compose down

or the rm command if the containers are stopped already.

# docker-compose rm --all

 

 

 

Docker commands with example

Use these Docker commands with an example for your reference.

Container – Docker Containers are what docker is built on. They encapsulate an application and all of its libraries and dependencies, so it can be run anywhere Docker is installed.

Image – A Docker Image is a file that is essentially a snapshot of a container. You can create a container by running a Docker Image.

Layer – a set of read-only files to provision the system. Think of a layer as a read only snapshot of the filesystem.

Registry / Hub is the central place where all publicly published images live. You can search it, upload your images there and when you pull a docker image, it comes the repository/hub.  You can also create private docker registry in your own cloud.

Docker machine is a VM within which you can run Docker containers. On Linux you can run docker containers natively, but on OSX and Windows you need a layer of abstraction. A docker machine will spin a very lightweight virtual machine that integrates with the docker command line utilities really well.

Docker compose is a utility to run multiple containers as a system of containers. It will take care of making them aware of each other and ensure they’re properly connected to each other. This means you can run your application in one container and your database in a different container, and your analytics application in a different container, and so on. This is the ultimate isolation and it means that your applications are independent and are run in development in a very similar way to how the system might work in production.

docker commands with example

 

Docker Cheat Sheet examples

Create and start container, run command

docker run -it --name <container_name> <image> -d

Start, stop and restart the container

docker [start|stop|restart] <container_name> or <container-id>

Use docker attach to attach to a running container using the container’s ID or name, either to view its ongoing output or to control it interactively.

docker attach <container_name>

Kill all running containers

docker kill $(docker ps -q)

Delete dangling images the ones that are not tagged properly and are hanging around.

docker rmi $(docker images -q -f dangling=true)

Remove all stopped containers, this will actually try to remove all the containers, but will fail to do so with the running ones, so only stopped containers will be gone after that.

docker rm $(docker ps -a -q)

Interacting with a container

Run a command in the container

docker exec -it <container_name> command.sh

Show the container logs (use -f option)

docker logs <option> <container_name>

Create an image from a running container

docker commit -m "commit message" -a "author" <container_name> username/image_name:tag

Copy files from the container to the host

docker cp <container_name>:/home/foo.txt <path>

Copy files to the container from host.

docker cp foo.txt <container_name>:/home

or if you want copy all large files, use below command

tar -cv * | docker exec -i <container_name> tar x -C <destination-folder-inside-container>

Mount the directory in host to a Container.

docker run -v /opt/test:/home/test <image> -d

Docker port mapping example
80 – host_port
8080 – container_port

docker run -d -p 80:8080 --name test_container <image>

Docker port mapping to specific IP

docker run -d -p 192.168.1.34:80:8080 --name test_container <image>

Set an environment variable in docker container

docker run -d -p 192.168.1.34:80:8080 -e VIRTUAL_HOST=domain.com --name test_container <image>

List the running containers. With -a option, it shows running and stopped Containers.

docker ps

Show container information like IP address.

docker inspect <container_name>

See the top process in container

docker top <container_name>

Docker Images command line

Create an image with Dockerfile.

echo -e "FROM centos:7\nRUN yum install -y openssh-server\nRUN systemctl enable sshd.service\nCMD ["/bin/bash"]" > Dockerfile
docker build -t <image> .

Login to the docker image

docker run -it <image> bash

Login to the docker container

docker exec -it <container_name> bash

Pull docker images (example is for the default centos image, you can specify the custom image name)

docker pull centos

Push docker images

Login docker hub ( docker login )

docker push gopkris2000/example

Delete a docker image

docker rmi gopkris2000/example

List all the images ( use -a to list all )

docker images

To show docker image information

docker inspect gopkris2000/example

To show command history of an image

docker history gopkris2000/example

Remove all untagged /none images

docker rmi $(docker images -a|awk '/none/ { print $3 }')

Save your docker images as tar

docker save gopkris2000/example:latest | gzip -c > gopkris2000-example.tgz

Load your docker images from tar file.

docker load < gopkris2000-example.tgz
 

Enable VNC Console Access in VMware ESXi

The ability to connect to a virtual machine using a VNC client has been available since the early days of VMware GSX but is not officially supported by VMware. Use the following steps to enable VNC console Access in VMware ESXi host.

ESXi host firewall configuration

ESXi host firewall configuration

SSH to the ESXi host

chmod 644 /etc/vmware/firewall/service.xml
chmod +t /etc/vmware/firewall/service.xml
vi /etc/vmware/firewall/service.xml

Create a new service block before the end of ConfigRoot ( </ConfigRoot> ) tag and make sure service id.

<service id='new unique id within this file'>
<id>VNC</id>
<rule id='0000'>
<direction>inbound</direction>
<protocol>tcp</protocol>
<porttype>dst</porttype>
<port>
<begin>5900</begin>
<end>6199</end>
</port>
</rule>
</service>

We need to open the VNC ports on the ESXi firewall. Add the ESXi Firewall rules and Verify that Ports.

On the ESXi host, execute the following commands

esxcli network firewall refresh
esxcli network firewall ruleset set --ruleset-id VNC --enabled true

Verify that the firewall rules were applied and the ports are open by executing the following commands

esxcli network firewall ruleset list
# You should see a rule labelled VNC in the output

esxcli network firewall ruleset rule list
# You should see the details of VNC rule i.e. port range, protocol, direction, etc.

 

Enable VNC for Existing Virtual Machines

 

To enable VNC console for existing VMs, power off the VM and use one of the following:

Using vSphere Web Client

Click on “edit settings”-> Select the “VM Options” tab->Expand the “Advanced” section-> click on “Edit 
configuration” and add the settings.

Directly on ESXi Host

The required .vmx configuration can also be applied to virtual machines running on ESXi.

Edit the Virtual Machine *.vmx file directly with the lines mentioned here.
RemoteDisplay.vnc.enabled = "TRUE"
RemoteDisplay.vnc.port =

Notes:

Be sure to choose a port number within the range you specified in the VNC.xml custom firewall rule, also make sure your .vmx configuration port is a conflict with existing one. Try this to verify.

grep "vnc.port" */*/*/*/*.vmx

Also remoteDisplay.vnc.password = “password” seems to be optional.