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
[email protected]:~# 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 [email protected]:~#
Now Ubuntu image is available on your docker installation. Lets verify it with command
docker images
Example
[email protected]:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 16.04 0ef2e08ed3fa 4 weeks ago 130 MB [email protected]:~#
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
[email protected]:~# 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 [email protected]:/#
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.
[email protected]:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [email protected]:~#
To see the container, run
docker ps -a
Example
[email protected]:~# 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 [email protected]:~#
Now lets commit the container as our own for later use
docker commit -m "my nginx install" e0039fd6fc1e hostonnet/nginx:1
Example
[email protected]:~# docker commit -m "my nginx install" e0039fd6fc1e hostonnet/nginx:1 sha256:98a9ccdf456ba6d08c9dc3ac63079c6896298c37c5b5fe07de6f6c47506d4168 [email protected]:~#
To see the available images, run
docker images
You can see the new image now listed
[email protected]:~# 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 [email protected]:~#
Lets run our newly created image.
[email protected]:~# docker run -ti hostonnet/nginx:1 /bin/bash [email protected]:/# service nginx status * nginx is not running [email protected]:/#
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
[email protected]:~# docker commit -m "installed php" 3199ecaef06a hostonnet/nginx:2.0 sha256:57f7c9cea522d278b126fa5d3e79c3016e48ffe389efd9fc11641a1d89d28de7 [email protected]:~#
Run the container, verify php is available
[email protected]:~# docker run -ti hostonnet/nginx:2.0 /bin/bash [email protected]:/# 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 [email protected]:/# exit [email protected]:~#