Create a image

In the Humpback system, although we can use some public network images, but there are always something unsatisfactory. Such as long time when pulling images or container configuration issue. So we need to customize the image, and submitted to the private image registry. There are two ways to create a image.

  • You can create a new image base on an existing container.

  • Create a new image with Dockfile.

Create a new image base an existing container

This way is that adjust an exists running Container and modify the configuration or environment args, then create a new image base on it.

First we start a Container from the Image which need to be updated. Use the official base mirror alpine: 3.5 as a example.

  1. $ docker run -t -i --name=alpine alpine:3.5 /bin/ash

And then we can modify the container, for example instal a postgresql-dev environment for the container:

  1. / # apk add --no-cache postgresql-dev
  2. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
  3. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
  4. (1/9) Installing libressl2.4-libtls (2.4.4-r0)
  5. (2/9) Installing pkgconf (1.0.2-r0)
  6. (3/9) Installing libressl-dev (2.4.4-r0)
  7. (4/9) Installing db (5.3.28-r0)
  8. (5/9) Installing libsasl (2.1.26-r8)
  9. (6/9) Installing libldap (2.4.44-r3)
  10. (7/9) Installing libpq (9.6.2-r0)
  11. (8/9) Installing postgresql-libs (9.6.2-r0)
  12. (9/9) Installing postgresql-dev (9.6.2-r0)
  13. Executing busybox-1.25.1-r0.trigger
  14. OK: 29 MiB in 20 packages

Exit this container and then use the docker commit command to crate a new image base on the current alpine container.

  1. / # exit
  2. $ docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. b1ac4a82c2dd alpine:3.5 "/bin/ash" "5 minutes ago" Exit (0) 5 seconds ago alpine

Execute the docker commit command to create new image:

  1. $ docker commit -m "add postgresql" -a "bobliu" b1ac4a82c2dd 192.168.1.10:5000/postgresql:9.6.2
  2. sha256:b1ac4a82c2dd17e9c50b6bff0e42290496df9b6187117dfda9daec98015f6ae1
  3. $ docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. alpine 3.5 1bb3a95866d7 15 minutes ago 3.987MB
  6. 192.168.1.10:5000/postgresql 9.6.2 b1ac4a82c2dd 45 seconds ago 27.59MB

The docker commit command has the following options:

  1. docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

The main options (OPTIONS) are as below:

  • -a, —author - {string}, The author
  • -c, —change - {list}, Use the Dockerfile directive to create a image (default [])
  • -m, —message - {string}, Submit a note message
  • -p, —pause - {string}, Suspended container when submitting (default true)

The example192.168.1.10: 5000 is the private registry address.

  1. 192.168.1.10:5000/postgresql:9.6.2

Finally, push image to the private registry 192.168.1.10: 5000

  1. $ docker push 192.168.1.10:5000/postgresql:9.6.2
  2. The push refers to a repository [192.168.1.10:5000/postgresql]
  3. c1bfc2be7117: Pushed
  4. 23b9c7b43573: Pushed
  5. 9.6.2: digest: sha256:afcf3f596e42837ded618f4694e6ff9928034ce20e860b75125992c3dc1ba501 size: 739

Create a new image with Dockfile

Dockerfile builds the Docker image based on the DSL (Domain Specific Language) language. After the Dockerfile is written, you can use the docker build command to build a new image.

First create a folder, named postgresql, this directory is our build environment, in the Docker, this environment will be called content or build content. When building a image, the Docker passes the files and directories in the build environment to the daemons so that the daemon accesses any code, files, or other data that the user wants to store in the image.

Create a Dockerfile file in the directory:

  1. $ mkdir postgresql
  2. $ cd postgresql
  3. $ touch Dockerfile

Then edit the Dockerfile file, again, with the official alpine: 3.5 as the base image.

  1. # This is a comment
  2. FROM alpine:3.5
  3. MAINTAINER bobliu <bobliu@example.com>
  4. RUN apk add --no-cache postgresql-dev

How to use Dockerfile commands, please refer to the official:Dockerfile Reference

How to write Dockerfile, please refer to the official:Best practices for writing Dockerfiles

Then use the docker build command to build postgresql image:

  1. $ docker build -t 192.168.1.10:5000/postgresql:9.6 ./postgresql
  2. Sending build context to Docker daemon 2.048 kB
  3. Step 1 : FROM alpine:3.5
  4. 3.5: Pulling from library/alpine
  5. 031b4db7df57: Pull complete
  6. Digest: sha256:4d1622c09e7e7128132f825351d61f0e66651512672820a51ba537f0fd673ffb
  7. Status: Downloaded newer image for alpine:3.5
  8. ---> 031b4db7df57
  9. Step 2 : MAINTAINER bobliu <bobliu@example.com>
  10. ---> Running in cd85fee20da5
  11. ---> 51c8ce3d6bb8
  12. Removing intermediate container cd85fee20da5
  13. Step 3 : RUN apk add --no-cache postgresql-dev
  14. ---> Running in 2948f616151f
  15. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
  16. fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
  17. (1/9) Installing libressl2.4-libtls (2.4.4-r0)
  18. (2/9) Installing pkgconf (1.0.2-r0)
  19. (3/9) Installing libressl-dev (2.4.4-r0)
  20. (4/9) Installing db (5.3.28-r0)
  21. (5/9) Installing libsasl (2.1.26-r8)
  22. (6/9) Installing libldap (2.4.44-r3)
  23. (7/9) Installing libpq (9.6.2-r0)
  24. (8/9) Installing postgresql-libs (9.6.2-r0)
  25. (9/9) Installing postgresql-dev (9.6.2-r0)
  26. Executing busybox-1.25.1-r0.trigger
  27. OK: 29 MiB in 20 packages
  28. ---> c3e27ed7f727
  29. Removing intermediate container 2948f616151f
  30. Successfully built c3e27ed7f727

Then you can push the image to the private registry 192.168.1.10: 5000

  1. $ docker push 192.168.1.10:5000/postgresql:9.6
  2. The push refers to a repository [192.168.1.10:5000/postgresql]
  3. 031b4db7df57: Pushed
  4. 51c8ce3d6bb8: Pushed
  5. 9.6: digest: sha256:c3e27ed7f727d7b0248894ddfa6da54d913abc79e49467ff7e311c1dcd23ffd0 size: 739