How to Create Custom Docker Image Using Docker Compose

How to Create Custom Docker Image Using Docker Compose
COMMENTS (0)
Tweet

Docker is the container platform provider and management service that helps in addressing every application across the hybrid cloud. Docker enables true independence between applications and infrastructure, as well as developers and IT ops to unlock their potential for better innovation and collaboration. Keywords used for Docker across all platforms are ‘Develop’, ‘Ship’ and ‘Run Anywhere’. The basic idea of it is to enable developers to easily develop applications and further ship them into containers, which can later be deployed wherever required.

In this blog, you will learn how to easily create custom image using Docker compose.

Docker compose is fundamentally used to run multiple containers as a single service. Let’s say, you have an application that requires WordPress and Maria DB both, with the help of Docker compose you can easily create a single file that initiates both the containers as a service without having to start each of them separately. Let’s have a look at the steps involved to do the same:

To start off with, please note that we are targeting Ubuntu 16.04 OS for Docker engine here:

# apt-get update

Before installing Docker engine we need to install some recommended packages, given below:

# apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

After installing prerequisite packages, now let’s install Docker Engine:

# apt-get install docker-engine

The next step is to start Docker service by using the following command:

# docker start

Before proceeding any further, let’s have a look at some instructions that are essential for the creating the Docker file:

  1. Label:

You can assign metadata in the form of key-value pairs to the image using this instruction. It is important to note that each LABEL instruction creates a new layer in the image, therefore it is recommended to use as few LABEL instructions as possible.

  1. Environment: 

This instruction specifies environment variables that are set for the container when it runs.

  1. Ports: 

This type of instruction helps in identifying port mappings between the container and the Docker host. This is just like the -p flag that we pass to Docker run.

  1. Links: 

It specifies services that are to be exposed to other service via this network. In our case, we want the WordPress to be exposed to Maria dB.

Before installing Docker-compose, you first need to install python-pip as a prerequisite:

# apt-get install python-pip

Now, install Docker compose package:

# pip install docker-compose

Now, create directory called WordPress:

# mkdir wordpress

# cd wordpress

Create a Docker file called “Docker-compose.yml”. You need to define three separate containers, which are WordPress, Maria dB and php myadmin.

wordpress:
 image: wordpress
 links:
   - wordpress_db:mysql
 ports:
   - 8080:80

wordpress_db:
 image: mariadb
 environment:
  MY_ROOT_PASSWORD:  M~?zDm51ISnwX'qG
phpadmin:
 image: corbinu/Docker-phpmyadmin
 links:
   - wordpress_db:mysql
 ports:
   - 8181:80
 environment:
  MYSQL_USERNAME: root
  MYSQL_ROOT_PASSWORD:  $<0D~c6]N0Y(z*`5

 

Now, build container from Docker file “Docker-compose.yml”

# docker-compose up -d

This command will pull the three images and would then build three Docker containers.

Once the Docker is built, you can now easily install and access the WordPress using localhost port 8080:

URL: localhost:8080/wp-admin/install.php

docker image

Once the WordPress is installed, you will see its main Dashboard.

custom docker image

You can access PHP MyAdmin using port 8181:
URL: localhost: 8181
Username: root
Password: $<0D~c6]N0Y(z*`5

docker image

 

After logging in, you will be able to see php MyAdmin dashboard and the databases.

And that’s it! Wasn’t it easy?

Please feel free to comment or get in touch with us if you have any questions.

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812