Steps to build a Docker image using Dockerfile

Steps to build a Docker image using Dockerfile

Dockerfile is a text file that contains instructions. Each instruction builds a layer, so the content of each instruction describes how the layer should be built.

Dockerfile supports the Shell-like command line-ending method of adding "\" at the end of the line and the comment format of "#" at the beginning of the line.

Note when using Dockerfile to build a Docker image:

(1) Try to select a basic system image that meets your needs but is smaller;

(2) Clean up temporary files such as compiled files and installation package cache;

(3) When installing each software, specify the exact version number and avoid introducing unnecessary dependencies;

(4). Add a .dockerignore file or use a clean working directory.

Common instructions for Dockerfile:

(1).FROM: used to specify the base image to be built, which is usually the first instruction in the Dockerfile;

(2) LABEL: used to add labels to help organize images, record license information, assist in automated builds, etc. Labels are key-value pairs stored as strings;

(3). RUN: used to execute commands in the image, which will create a new image layer. Each RUN instruction creates a new image layer, and always combines apt-get update and apt-get install into one RUN;

The RUN instruction has two formats:

A. Shell format: RUN <command>, just like the command entered directly in the command line;

B.exec format: RUN ["executable file", "parameter 1", "parameter 2"];

(4) COPY: supports simple copying of local files into the container. The COPY instruction is usually used to assign application code to the image.

(5).EXPOSE: used to record the network ports used by the application;

(6).ENTRYPOINT: used to specify the program that runs by default after the image is started in container mode;

(7).ENV: Updates the PATH environment variable for programs installed in the container.

The following Dockerfile is used to compile and execute directly in the container https://github.com/fengbingchun/Messy_Test:

FROM ubuntu:16.04
LABEL maintainer="FengBingchun [email protected]" \ 
   version="1.0" \
   description="dockerfile test"
RUN dep_items='git cmake g++-5' \
  && apt-get update \
  && apt-get install -y $dep_items \ 
  && ln -s /usr/bin/g++-5 /usr/bin/g++ \
  && rm -rf /var/lib/apt/lists/*

Build the image. After executing the following command, an image named fengbingchun/ubuntu:16.04 will be successfully generated:

docker build -t fengbingchun/ubuntu:16.04 .

By mounting the host directory, create a new container test and execute the following command:

docker run -it -P --name test --mount type=bind,source=e:\GitCode\docker,target=/home/fengbingchun fengbingchun/ubuntu:16.04 /bin/bash

Then, in the container, cd to the /home/fengbingchun directory, clone Messy_Test and execute the following command:

git clone https://github.com/fengbingchun/Messy_Test

Then cd to the cd Messy_Test/prj/linux_cmake_CppBaseTest directory and execute the following commands in sequence

./build.sh
./build/CppBaseTest

The execution result is shown in the figure below, indicating that the image built by Dockerfile can compile and execute Messy_Test normally:

Save the image fengbingchun/ubuntu:16.04 to a tarball and execute the following command:

docker save -o ubuntu_16.04.tar fengbingchun/ubuntu:16.04

Copy ubuntu_16.04.tar to the Ubuntu system, load an image from the tarball, and execute the following command:

docker load -i ubuntu_16.04.tar

Then perform similar operations on Windows, compile and execute Messy_Test in the newly created container test, and execute the following commands in sequence:

docker run -it -P --name test --mount type=bind,source=/home/xxxx/Disk/GitHub/docker,target=/home/fengbingchun fengbingchun/ubuntu:16.04 /bin/bash
cd /home/fengbingchun/
git clone https://github.com/fengbingchun/Messy_Test
cd Messy_Test/prj/linux_cmake_CppBaseTest/
./build.sh
./build/CppBaseTest

The execution result is shown in the figure below: It shows that after the image generated on Windows is packaged, it can be used normally after loading on Ubuntu.

This concludes this article about the steps to build a Docker image using Dockerfile. For more information about building a Docker image using Dockerfile, 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:
  • Dockerfile file writing and image building command analysis
  • Implementation of building custom images with Dockerfile
  • Build a Docker image using Dockerfile
  • How to build a tomcat image based on Dockerfile
  • How to use Dockerfile to build images in Docker
  • Example of using Dockerfile to build an nginx image
  • How to use Dockerfile to build images
  • Sample code for building a docker image using the dockerfile instruction

<<:  Creating a Secondary Menu Using JavaScript

>>:  Simple web page code used in NetEase blog

Recommend

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

In-depth analysis of HTML semantics and its related front-end frameworks

About semantics Semantics is the study of the rel...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

An example of implementing a simple finger click animation with CSS3 Animation

This article mainly introduces an example of impl...

React Hooks Common Use Scenarios (Summary)

Table of contents 1. State Hook 1. Basic usage 2....

Summary of common Linux distribution mirror source configuration

I have been researching Linux recently and tried ...

Common operation commands of MySQL in Linux system

Serve: # chkconfig --list List all system service...

HTML 5.1 learning: 14 new features and application examples

Preface As we all know, HTML5 belongs to the Worl...

Solve the docker.socket permission problem of vscode docker plugin

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

Detailed explanation of the solution to forget the password in MySQL 5.7

ENV: [root@centos7 ~]# uname -r 3.10.0-514.el7.x8...

CSS implementation code for drawing triangles (border method)

1. Implement a simple triangle Using the border i...

Simple usage of MySQL temporary tables

MySQL temporary tables are very useful when we ne...

Detailed explanation of Linux kernel macro Container_Of

Table of contents 1. How are structures stored in...

Using CSS3 to implement font color gradient

When using Animation.css, I found that the font o...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...