[Basic] How to write a Dockerfile Self-learning ②

What is a Dockerfile

A blueprint for creating a Docker image. Creating from a Dockerfile makes it easier to understand the contents of the container. Create a Dockerfile and create an image with the following command.

$ docker build -t <name> <directory>

How to write a Dockerfile

The Dockerfile basically consists of three instructions ・ FROM ・ RUN ・ CMD

Start writing from FROM.

FROM ubuntu:latest
RUN touch test
CMD ["ls"]

In this example, create an OS image called ubuntu in FROM and use the Linux touch command. In most cases, the OS is specified for FROM, or a Docker image created by another person is specified.

The work specified by RUN is piled up as a layer. However, if you use RUN a lot, the number of layers will increase and the Docker image will become heavier, so be careful.

CMD is described at the end of the Dockerfile. The command specified in CMD is executed when creating a container. Here the ls command is executed.

About the difference between RUN and CMD

In short, RUN creates a layer. CMD does not create a layer. In other words, CMD is not reflected in Docker image.

If you want to save it in Dockerimage, describe it in RUN, and the content you want to execute will be the image described in CMD.

To reduce the number of layers ...

There are three commands to create a layer. ・ RUN ・ COPY ・ ADD

In actual business, RUN is mainly used and the main thing is to manage packages. The command to manage the package is as follows.

RUN apt-get update -Get new package list
RUN apt-get install <package> -<package>Install

By connecting these with &&, the layer can be made lighter in one line.

RUN apt-get update && apt-get install test

This time is over.

Recommended Posts

[Basic] How to write a Dockerfile Self-learning ②
How to write dockerfile
How to write a ternary operator
[Introduction to Java] How to write a Java program
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
How to write Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
Write a dockerfile to start jupyter-lab on ubuntu
How to write test code with Basic authentication
How to write good code
How to write a unit test for Spring Boot 2
Bit Tetris (how to write)
java: How to write a generic type list [Note]
How to write java comments
How to introduce Basic authentication
How to leave a comment
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write a date comparison search in Rails
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
To write a user-oriented program (1)
[Ruby] How to write blocks
How to write Rails routing
How to write a core mod in Minecraft Forge 1.15.2
How to insert a video
How to create a method
How to write a migration from Rails datetime type to date type
How to create and launch a Dockerfile for Payara Micro
How to add columns to a table
[Rails] How to write in Japanese
How to sign a Minecraft MOD
How to make a JDBC driver
Baseball ball count (how to write)
[Java] How to create a folder
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Swift] How to send a notification
How to write Java variable declaration
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to make a Java array
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
[Xcode] How to add a README.md file
How to execute a contract using web3j
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
I want to write a nice build.gradle
How to insert a video in Rails