Docker: Create a Custom Image

root's picture

In this tutorial, I will show you how to create a custom docker image and run a container on top of it.

1. Download debian image

00:28:28 root@service:~# docker pull debian
Using default tag: latest
latest: Pulling from library/debian
22dbe790f715: Pull complete 
Digest: sha256:72e996751fe42b2a0c1e6355730dc2751ccda50564fec929f76804a6365ef5ef
Status: Downloaded newer image for debian:latest
00:29:05 root@service:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian              latest              0af60a5c6dd0        10 days ago         101MB

2. Run a container from that image by connecting interactively to it

NOTE: we will exit for a short time to show you how this new "on the fly created" container looks like.
Also, we will show you that after you exit from it, if you want to connect to it, you need to start it first.

00:52:06 root@service:~# docker run -ti debian
root@935bd3b36617:/# exit
exit
00:53:19 root@service:~# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                     PORTS               NAMES
935bd3b36617        debian              "bash"              About a minute ago   Exited (0) 4 seconds ago                       eloquent_heyrovsky
00:56:40 root@service:~# docker start 935bd3b36617
935bd3b36617
00:56:44 root@service:~# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
935bd3b36617        debian              "bash"              4 minutes ago       Up 2 seconds                            eloquent_heyrovsky
00:57:08 root@service:~# docker exec -ti 935bd3b36617 bash
root@935bd3b36617:/#

Now do your stuff (install whatever packages you want there, edit configuration files, etc.)

3. Once you are done, you need to commit the changes into a new image

02:07:02 root@service:~# docker container ls -s | grep 935bd3b36617
935bd3b36617        debian              "bash"                   About an hour ago   Up About an hour                           eloquent_heyrovsky     3.96GB (virtual 4.07GB)
02:07:26 root@service:~# docker commit 935bd3b36617
sha256:dd9bc9f7a2a6b4a6f3dc82853cb07d180f43b2d0cfe8221e5ee25814a57362c8
02:18:17 root@service:~# docker images | grep dd9bc9f7a2a6
<none>              <none>              dd9bc9f7a2a6        37 seconds ago      4.07GB
02:18:36 root@service:~# docker tag dd9bc9f7a2a6 debian-php5-apache2:v0BETA
02:19:07 root@service:~# docker images | grep dd9bc9f7a2a6
debian-php5-apache2   v0BETA              dd9bc9f7a2a6        About a minute ago   4.07GB

4. And now start a container on top of this image

02:22:51 root@service:~# docker run --detach --publish=8002:80 --name=someapachewphptest debian-php5-apache2:v0BETA /usr/sbin/apache2ctl -D FOREGROUND
c35ce144ae3e1a94c0559e74911b14ad796722d1968c3af3e4bedd8b6c85ec0e
02:23:05 root@service:~# docker container ls | grep someapache
c35ce144ae3e        debian-php5-apache2:v0BETA   "/usr/sbin/apache2ct…"   10 seconds ago      Up 7 seconds        0.0.0.0:8002->80/tcp   someapachewphptest

DONE!

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content