Two ways to build Docker images

Two ways to build Docker images

When the image downloaded from the Docker image repository does not meet our needs, we can change the image in the following two ways.

  • Update an image from an existing image
  • Building an image from scratch

Update the image from an existing image:

Before updating the image, we need to create a container using the image.

insert image description here

Enter the container:

docker run -t -i db2b37ec6181 /bin/bash

Use the apt-get update command in the running container to update. After completing the operation, enter the exit command to exit the container.
At this time, the container with ID 0aab061e6f5a is the container that has been changed according to our needs. We can commit the container copy through the command docker commit.

insert image description here

[root@localhost .ssh]# docker commit -m="has update" -a="zyn" 0aab061e6f5a mysql:v2
sha256:3dda266fd05963e816f22e3dec2584589977e040f7202e0421b0151290e4f54b

Description of the parameters of the above command:

-m: Submit description information
-a: Specify the image author
0aab061e6f5a: container ID
mysql:v2: specifies the target image name to be created

We can use the docker images command to view our new image mysql:v2:

insert image description here

Build an image from scratch:

Use the docker build command to create a new image from scratch. To do this, we need to create a Dockerfile file, which contains a set of instructions to tell Docker how to build our image.

For example, we build a docker image from a jar package:

First create the Dockerfile file:

FROM java:8
MAINTAINER zyn
COPY jenkins.war /usr/local/jenkins.war
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/usr/local/jenkins.war","--httpPort=8080"]

Description of the parameters of the above command:

java:8 is the base image just downloaded
MAINTAINER is the author
COPY is to copy the local jar to the image
EXPOSE declares the open interface of the mirror
ENTRYPOINT is the command parameter specified when docker run, similar to RUN and CMD commands

Each instruction creates a new layer on the image. The prefix of each instruction must be uppercase.

Then put the jar in the same directory as the Dockerfile:

insert image description here

Finally, we use the Dockerfile file to build an image using the docker build command:

Note: The dot after Dockerfile means that the current directory is used as the context directory. When creating an image, the files in the context directory will be copied to the image, that is, the jar package will be copied to the image.

[root@localhost zyn]# docker build -t my_jenkins:1.0 -f Dockerfile .
Sending build context to Docker daemon 67.29MB
Step 1/5: FROM java:8
8: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:8
 ---> d23bdf5b1b1b
Step 2/5: MAINTAINER zyn
 ---> Running in d8027d9002f4
Removing intermediate container d8027d9002f4
 ---> cdd9362868cb
Step 3/5 : COPY jenkins.war /usr/local/jenkins.war
 ---> 3a276d766222
Step 4/5: EXPOSE 8080
 ---> Running in 99cf28fb33a8
Removing intermediate container 99cf28fb33a8
 ---> de89b785c80d
Step 5/5 : ENTRYPOINT ["java", "-jar", "/usr/local/jenkins.war","--httpPort=8080"]
 ---> Running in c3a7e16eaa11
Removing intermediate container c3a7e16eaa11
 ---> 5107b1256f01
Successfully built 5107b1256f01
Successfully tagged my_jenkins:1.0

insert image description here

Start the above docker image:

[root@localhost zyn]# docker run -itd --name jenkins -p 18080:8080 my_jenkins:1.0
5e185bb8e4866b8018f0b3bb7a4845360d3d4efc5bd2509d84fe118929fe52b3

insert image description here

This concludes this article about two ways to build images with Docker. For more information about building images with Docker, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A detailed introduction to the Dockerfile image building file and related commands in Docker
  • Detailed explanation of using Dockerfile to build MySQL image and implement data initialization and permission setting
  • How to build a docker base image from scratch
  • Jenkins builds Docker image example
  • Implementation of Docker multi-stage image building
  • How to use Dockerfile to build images

<<:  Perfect solution to the problem of webpack packaging css background image path

>>:  JS Easy to understand Function and Constructor

Recommend

CentOS7.5 installation of MySQL8.0.19 tutorial detailed instructions

1. Introduction This article does not have screen...

Using HTML to implement a voting website cheating scheme that restricts IP

This is a cheating scheme for voting websites wit...

More than 100 lines of code to implement react drag hooks

Preface The source code is only more than 100 lin...

A complete guide to the Docker command line (18 things you have to know)

Preface A Docker image consists of a Dockerfile a...

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

Solve the docker.socket permission problem of vscode docker plugin

Solution: Kill all .vscode related processes in t...

Javascript scope and closure details

Table of contents 1. Scope 2. Scope Chain 3. Lexi...

Detailed tutorial on deploying Jenkins based on docker

0. When I made this document, it was around Decem...

How to convert a column of comma-separated values ​​into columns in MySQL

Preface Sometimes you come across business tables...

MySQL performance optimization index pushdown

Index condition pushdown (ICP) is introduced in M...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...

How does Vue solve the cross-domain problem of axios request front end

Table of contents Preface 1. Why do cross-domain ...

mysql-8.0.16 winx64 latest installation tutorial with pictures and text

I just started learning about databases recently....

Vue.js handles Icon icons through components

Icon icon processing solution The goal of this re...