Image creation and modification for Docker tutorial

  • 2020-05-30 21:42:37
  • OfStack

Create a mirror image

There are many ways to create an image. You can either get an existing image from Docker Hub and update it, or you can create one using your local file system.

Modify the existing image

Start the container with the image you downloaded.

$ sudo docker run -t -i training/sinatra /bin/bash

Note: remember the ID of the container, which you will use later.

Add json and gem applications to the container.

root@0b2616b0e5a8:/# gem install json

When finished, we used exit to exit, now that our container has been changed, we used the docker commit command to submit the updated copy.

$ sudo docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2

Among them, -m is used to specify the submitted specification information, similar to the version control tool we used 1; -a can specify updated user information; Then ID, which is used to create the mirrored container; Finally, specify the repository name and tag information for the target image. The ID information for this image is returned when it is successfully created.

Use docker images to view the newly created image.

$ sudo docker images
training/sinatra  latest 5bc342fa0b91  10 hours ago 446.7 MB
ouruser/sinatra   v2   3c59e02ddd1a  10 hours ago 446.7 MB
ouruser/sinatra   latest 5db5f8471261  10 hours ago 446.7 MB

After that, you can use the new image to start the container

$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash

Use Dockerfile to create the image

Extending an image using docker commit is easy, but not easy to share with a team. We can use docker build to create a new image. To do this, you first need to create an Dockerfile with instructions on how to create an image.

Create 1 directory and 1 Dockerfile

$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile

Each instruction in Dockerfile creates a layer of the image, for example:

# This is a comment
FROM ubuntu:14.04
MAINTAINER Docker Newbee <>
RUN apt-get -qq update
RUN apt-get -qqy install ruby ruby-dev
RUN gem install sinatra

The basic syntax for Dockerfile is

Comment with # The FROM directive tells Docker which image to use as the base Then there's the maintainer's message The command that starts with RUN will be run during creation, such as installing a software package, where apt-get is used to install some software

After writing Dockerfile, you can use docker build to generate the image.

$ sudo docker build -t="ouruser/sinatra:v2" .
Uploading context 2.56 kB
Uploading context
Step 0 : FROM ubuntu:14.04
 ---> 99ec81b80c55
Step 1 : MAINTAINER Newbee <>
 ---> Running in 7c5664a8a0c1
 ---> 2fa8ca4e2a13
Removing intermediate container 7c5664a8a0c1
Step 2 : RUN apt-get -qq update
 ---> Running in b07cc3fb4256
 ---> 50d21070ec0c
Removing intermediate container b07cc3fb4256
Step 3 : RUN apt-get -qqy install ruby ruby-dev
 ---> Running in a5b038dd127e
Selecting previously unselected package libasan0:amd64.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack .../libasan0_4.8.2-19ubuntu1_amd64.deb ...
Setting up ruby (1: ...
Setting up ruby1.9.1 ( ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
 ---> 2acb20f17878
Removing intermediate container a5b038dd127e
Step 4 : RUN gem install sinatra
 ---> Running in 5e9d0065c1f7
. . .
Successfully installed rack-protection-1.5.3
Successfully installed sinatra-1.4.5
4 gems installed
 ---> 324104cde6ad
Removing intermediate container 5e9d0065c1f7
Successfully built 324104cde6ad

Where the -t tag adds tag, specifying the user information for the new image. ". "is the path (current directory) where Dockerfile resides, or can be replaced with a specific Dockerfile path.

You can see the build process performing the operation. The first thing it has to do is upload this Dockerfile content, because everything has to be done according to Dockerfile. The instructions in Dockfile are then executed one by one. Each step creates a new container, executes the instructions in the container, and commits the changes (as shown in docker commit 1). When all the instructions have been executed, the final mirror id is returned. All intermediate steps that produce containers are removed and cleaned.

* note that one mirror cannot exceed 127 levels

You can also copy local files to the image using the ADD command. Open the port to the outside with the EXPOSE command; Use the CMD command to describe programs that run after the container is started. For example,

# put my local web site in myApp folder to /var/www
ADD myApp /var/www
# expose httpd port
# the command to run
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

You can now start a container with the newly created image.

$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash

You can also modify the labels on the image using the docker tag command.

root@0b2616b0e5a8:/# gem install json

Import from the local file system

To import an image from a local file system, you can create it using openvz (a pioneer in container virtualization) templates: openvz's template download address is templates.

For example, after downloading an image of ubuntu-14.04, import it using the following command:

root@0b2616b0e5a8:/# gem install json

Then look at the newly imported image.

root@0b2616b0e5a8:/# gem install json

Upload the image

Users can upload their images to the repository for sharing through the docker push command. For example, after registering on Docker Hub, users can push their images to the repository.

$ sudo docker push ouruser/sinatra
The push refers to a repository [ouruser/sinatra] (len: 1)
Sending image list
Pushing repository ouruser/sinatra (3 tags)

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: