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

html page!--[if IE]...![endif]--Detailed introduction to usage

Copy code The code is as follows: <!--[if IE]&...

Ubuntu 18.04 disable/enable touchpad via command

In Ubuntu, you often encounter the situation wher...

Docker binding fixed IP/cross-host container mutual access operation

Preface Previously, static IPs assigned using pip...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Linux kernel device driver kernel debugging technical notes collation

/****************** * Kernel debugging technology...

The qualities and abilities a web designer should have

Web design is an emerging marginal industry that c...

MySQL fuzzy query usage (regular, wildcard, built-in function)

Table of contents 1. MySQL wildcard fuzzy query (...

Two query methods when the MySQL query field type is json

The table structure is as follows: id varchar(32)...

In-depth analysis of MySQL indexes

Preface We know that index selection is the work ...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

Solution to the automatic termination of docker run container

Today I encountered a problem when I used Dockerf...

Tutorial on Installing Nginx-RTMP Streaming Server on Ubuntu 14

1. RTMP RTMP streaming protocol is a real-time au...

Detailed explanation of the functions and usage of MySQL common storage engines

This article uses examples to illustrate the func...