Docker creates images in two ways

  • 2020-05-30 21:21:19
  • OfStack

Docker creates the image

Learning about Docker recently, it is good to come across two methods of creating objects by Docker on the Internet. Note them here, which may help you.

We all know that Docker is a container that runs on an image, so how do you create an image? There are two ways to create an image, 1 is to use the docker commit command, 2 is to use the docker build command and Dockerfile file. By creating an image we mean building on an existing base image such as ubuntu, rather than creating a new image from zero.

The following two methods are introduced briefly.

The first is created using docker commit.

First we can run a container:


    sudo docker run -i-t ubuntu /bin/bash

Then we can modify in this container, such as installing some software or setting up some environment, etc. :


    apt-get install gcc


Finally we commit these changes and create the image:


   sudo dockercommit 4aa578389 buffoon/gcc

Here 4aa578389 is the ID of the modified container, buffoon/gcc is the target mirror repository and the mirror name. We can also use one of the parameters in commit to expand more information. Such as:


    sudo dockercommit -m " A new image with gcc "  -a " buffoon "  4aa578389 buf/gcc:mm

The -m option above specifies the submission information for the image, the -a option indicates the author information, and the last :mm is the label.

We can see if the newly created image was successful by sudo dockerimages buffoon/gcc. To see more information about the image, sudo docker inspect buf/gcc:mm.

The second is created using the docker build command and Dockerfile files.

First we create an empty directory as the build environment, which is the build context in Docker:


    mkdir gcc
    cd gcc

Then we create an Dockerfile file in this directory:


    vim Dockerfile

Edit in file:


#Version: 0.0.1 
FROM ubuntu:latest 
MAINTAINERbuffoon xxx@gmail.com 
RUN apt-getinstall gcc 

In this file, FROM must be specified as the first directive, the base image; The MAINTAINER directive specifies some information about the author; RUN specifies the commands to be executed at the mirror runtime.

We can then go into the build context and execute the create:


   cd gcc
    sudo dockerbuild -t= " buffoon/gcc:v1 "  .  // Pay attention to the last 1 A dot 

-t specifies the warehouse image label, with the last dot indicating that the Dockerfile file is found from the current path.

Dockerfile instruction has a lot of, in here not 11 introduced, specific instructions, please see: https: / / docs docker. com/engine/reference builder /

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


Related articles: