Docker image creation how to modify upload image and other details

  • 2020-05-30 21:38:25
  • OfStack

Docker creates, modifies, and uploads images

There are many ways to create an image from a wok. Users can get an existing image from Docker Hub and update it, or they can create one from the local file system.

1. Create a mirror

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.

2. Modify the existing image

1. Start the container with the image you downloaded.


$ docker run -t -i training/sinatra /bin/bash
root@0b2616b0e5a8:/#

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

2. 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
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

Among them, -m to specify the submitted specification information, like 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
REPOSITORY     TAG   IMAGE ID    CREATED    VIRTUAL SIZE
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
root@78e82f680994:/#

3. Create a mirror using Dockerfile

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 <newbee@docker.com>
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 instructions that start with RUN are 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 <newbee@docker.com>
 ---> 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:1.9.3.4) ...
Setting up ruby1.9.1 (1.9.3.484-2ubuntu1) ...
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 a mirror image cannot exceed 127 levels

You can also copy local files to the image using the ADD command. Open ports 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
EXPOSE 80
# 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
root@8196968dac35:/#

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


root@0b2616b0e5a8:/# gem install json
0

4. 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
1

Then look at the newly imported image.


root@0b2616b0e5a8:/# gem install json
2

5. Upload a mirror

Users can upload their created images to the repository for sharing through the docker push command. For example, after a user has registered on Docker Hub, they can push their image 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: