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

Refs and Ref Details in Vue3

The editor also shares with you the corresponding...

The difference between ID and Name attributes of HTML elements

Today I am a little confused about <a href=&quo...

Is it necessary to give alt attribute to img image tag?

Do you add an alt attribute to the img image tag? ...

Analysis of common usage examples of MySQL process functions

This article uses examples to illustrate the comm...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...

A brief understanding of the three uses of standard SQL update statements

1. Environment: MySQL-5.0.41-win32 Windows XP Pro...

The difference and reasons between the MySQL query conditions not in and in

Write a SQL first SELECT DISTINCT from_id FROM co...

Tutorial on using portainer to connect to remote docker

Portainer is a lightweight docker environment man...

A brief analysis of whether using iframe to call a page will cache the page

Recently, I have a project that requires using ifr...

Detailed explanation of the correct use of the count function in MySQL

1. Description In MySQL, when we need to get the ...

Database query optimization: subquery optimization

1. Case Take all employees who are not the head o...