HostOnNet Blog

Creating your own custom image with Docker

Looking for Linux Server Admin or WordPress Expert? We can help.

To build your own custom docker image, you need to pull a base image first. Lets start with Ubuntu. You can find more about Ubuntu image at

https://hub.docker.com/_/ubuntu/

Lets pull Ubuntu 16.04 image

docker pull ubuntu:16.04
root@boby:~# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
d54efb8db41d: Pull complete 
f8b845f45a87: Pull complete 
e8db7bf7c39f: Pull complete 
9654c40e9079: Pull complete 
6d9ef359eaaa: Pull complete 
Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535
Status: Downloaded newer image for ubuntu:16.04
root@boby:~# 

Now Ubuntu image is available on your docker installation. Lets verify it with command

docker images

Example

root@boby:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               0ef2e08ed3fa        4 weeks ago         130 MB
root@boby:~# 

Run container with terminal

To install software and create your own image, you need to start it with

docker run -ti ubuntu /bin/bash

Example

root@boby:~# docker run -ti ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535
Status: Downloaded newer image for ubuntu:latest
root@e0039fd6fc1e:/# 

Now you are inside the container, lets install some software.

apt update && apt -y upgrade
apt -y install nginx

You have nginx installed, exit the container

exit

This will stop running container.

root@boby:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@boby:~# 

To see the container, run

docker ps -a

Example

root@boby:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
e0039fd6fc1e        ubuntu              "/bin/bash"         4 minutes ago       Exited (0) 57 seconds ago                       pensive_kalam
root@boby:~# 

Now lets commit the container as our own for later use

docker commit -m "my nginx install" e0039fd6fc1e hostonnet/nginx:1

Example

root@boby:~# docker commit -m "my nginx install" e0039fd6fc1e hostonnet/nginx:1
sha256:98a9ccdf456ba6d08c9dc3ac63079c6896298c37c5b5fe07de6f6c47506d4168
root@boby:~# 

To see the available images, run

docker images

You can see the new image now listed

root@boby:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hostonnet/nginx     1                   98a9ccdf456b        36 seconds ago      254 MB
ubuntu              16.04               0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
root@boby:~# 

Lets run our newly created image.

root@boby:~# docker run -ti hostonnet/nginx:1 /bin/bash
root@2af374559746:/# service nginx status
 * nginx is not running
root@2af374559746:/# 

As you can see, this image have nginx already installed. Lets install PHP with apt install php, then exit the container.

Commit the new container with

root@boby:~# docker commit -m "installed php" 3199ecaef06a hostonnet/nginx:2.0
sha256:57f7c9cea522d278b126fa5d3e79c3016e48ffe389efd9fc11641a1d89d28de7
root@boby:~# 

Run the container, verify php is available

root@boby:~# docker run -ti hostonnet/nginx:2.0 /bin/bash
root@b0d9d40a9818:/# php -v
PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.15-0ubuntu0.16.04.4, Copyright (c) 1999-2017, by Zend Technologies
root@b0d9d40a9818:/# exit
root@boby:~# 

Posted in Virtualization

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.