[Docker] Introduction of basic Docker Instruction

Also, I'm going to leave Docker for a while, so I'd like to write a memorandum as well. This time, I will briefly introduce FROM, RUN, and CMD.

FROM The Dockerfile describes the FROM to determine the base image. When specifying the image of the latest version of ex.ubuntu

FROM ubuntu:latest

Until you get used to it, it's a good idea to specify one that has all the tools you need. Once you get used to it, you should consider adding the tools you want only with the OS.

RUN You can use this command to customize what your server needs to your liking.

ex. Create files test1.txt and test2.txt on the image OS


RUN touch test1.txt
RUN touch test2.txt

It is possible to prepare the environment on the image OS by executing the RUN command with multiple lines, but a layer is created for each RUN. Please note that the image becomes larger as the number of layers increases.

Ingenuity to reduce layers

If you need to use multiple commands to create layers, RUN, COPY, and ADD, use && to connect the commands. If one line becomes long, the Dockerfile will be hard to see, so start a new line with \ (backslash).


RUN apt-get update
RUN apt-get install aaa
RUN apt-get install bbb
RUN apt-get install ccc


RUN apt-get update && apt-get install aaa bbb ccc

↓ As the number of installation packages increases, it becomes difficult to see, so arrange with line breaks.


RUN apt-get update && apt-get install \
aaa \
bbb \

 You need to enter the execution permission y interactively, so if you enter -y which means yes, the installation will run smoothly.

#### **`Dockerfile`**

RUN apt-get update && apt-get install \
aaa \
bbb \

Effective use of cache

When writing to install the tool with apt-get etc., aaa and bbb were installed successfully one by one, so connect them and write ccc and try ... and access to the network every time to get the execution result It will occur.

If you want to install the aaa bbb ccc tool


RUN apt-get install \
aaa \

Since this worked, if you continue to write ccc, the cache will not be used and everything will be fetched from the network.


RUN apt-get install \
aaa \
bbb \

↓ First, write in a different RUN.


RUN apt-get install \
aaa \
RUN apt-get ccc

First of all, I will try to see if it works well by dividing the things to be added later. Hopefully you can eliminate the loss of a series of execution time from description by writing it on one line.

CMD -By writing at the end of the Dockerfile, you can specify the command to be executed by default in the container. CMD ["command", "parameter 1", "parameter 2", "parameter 3"] ex. Start Bash

FROM ...
RUN ...
CMD ["bin/bash"]

The end

If you want to create an environment such as a simple web server with the commands introduced above: FROM, RUN, CMD, you will be able to create it. Docker is convenient, so please try it.

Recommended Posts

[Docker] Introduction of basic Docker Instruction
Introduction of Docker --Part 1--
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
Docker basic summary
Introduction of pay.jp
Introduction of milkode
docker basic command
Docker basic commands
Introduction of Docker Hub and commands Self-learning ①
[Docker] Introduction of basic options (sharing, user, port, CPU / memory) during Docker run
Introduction and basic usage of Simple Calendar gem
[Rails] Introduction of PAY.JP
Basic format of Dockefile
Introduction of user authentication
Introduction to Java for beginners Basic knowledge of Java language ①
Basic methods of Ruby hashes
Overview of Docker and containers
[Rails] Introduction of devise Basics
Docker monitoring-explaining the basics of basics-
Basic knowledge of SQL statements
Basic methods of Ruby arrays
About simple operation of Docker
Understand the basics of docker
I've only heard of Docker ...
Fastest Docker introduction (Windows 10 edition)
Super basic usage of Eclipse
[Ruby] List of basic commands
Summary of basic functions of ImageJ
Docker Compact Manual (1: Basic / Basic Command)
Review of Ruby basic grammar
Introduction of javaFX in Eclipse (April 2020)
Introduction to Linux Container / Docker (Part 1)
Understand the basic mechanism of log4j2.xml
Run LibreOffice Basic macros in Docker
[Rails] Introduction of Rubocop by beginners
[Java] Basic types and instruction notes
Basic knowledge of Ruby on Rails
[Docker Network Chapter 2] Explanation of Docker Networking
[Docker Network Chapter 1] Explanation of Docker Networking
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
Basic usage of java Optional Part 1
Introduction to Linux Container / Docker (Part 2)
Summary of frequently used Docker commands
From introduction to use of ActiveHash
From introduction to usage of byebug
Basic processing flow of java Stream
Docker [Batch deletion of <none> tags]
[Basic knowledge of Java] Scope of variables
Summary about the introduction of Device
Introduction to Docker (1) Frequently used commands
Basic structure of Java source code
[Summary of technical books] Summary of reading "Introduction to Docker / Kubernetes Practical Container Development"