How to write dockerfile

First post on the Advent Calendar! This article is Day 24 of the Phone Appli Advent Calendar. Thank you!

Why Docker?

――It was "Docker" that was frustrating several times ... ――Because I couldn't understand terms such as "image" and "container" ... ――I often see it in books and sites ...

In short, it's a "memorial" article to reduce frustration with docker during work or other studies. Yes····

Let's try it now ~~~~

Prerequisites

tool Explanation
docker Installation complete
VScode Because there are plug-ins

What is Docker in the first place?

--A platform that allows you to create and execute container-type virtual environments

What is a container?

--It is one of the virtual environments. (Using a technology called "namespace" implemented in the Linux kernel) --On the host OS (on your PC's OS), you can create a virtual environment called a container and operate the libraries required for application development as one. --Multiple applications can be run on one host OS (hence, it is said to have a high affinity with microservices).

Implementation

This goal

--In the container, you can start "vim" with the specified file (/sample/test.md).

I don't have a strong commitment to vim, but by doing something in the container, You can see the difference from the host (your PC) ...

Describe in Dockerfile

FROM ubuntu:latest 
RUN apt-get update && apt-get install -y \
    vim
RUN mkdir sample
WORKDIR /sample
RUN touch test.md
WORKDIR /
CMD ["ls"]

You can do this to go to the specified directory inside the container and start vim

Description of contents

FROM ubuntu:latest

FROM basically specifies the OS (this time, ubuntu, which is said to be friendly to beginners derived from Linux) is specified.

--I don't want to make it too big as it is the basis of docker image

RUN apt-get update && apt-get install -y \
    vim
RUN mkdir sample
WORKDIR /sample
RUN touch test.md
WORKDIR /

RUN is ** execution command **

--Run update and install with the apt-get command for ubuntu

WORKDIR is ** directory move **

――You have moved twice ... (There must be a better way ...)

CMD ["ls"]

CMD is the first command to be executed when the container is started. Used when you want to specify the port number

--This time, execute the ls command to display a list of directories.

Execute the command at the terminal command prompt

docker build -t "The name of the image you want to give" .  //->Image is created
docker images //-> "sample"Image is created
docker run image name//->Create and start a container, and the ls command is executed inside the container. The container will be closed soon, but ...
docker ps -a //-> `COMMAND`In the column`ls`There should be a history of commanding ...

By the way, dockerfile creation-> image creation-> container creation / start-> I executed the command in the container,

--I can't stop in the container and hit the command myself ** (I can't reach the goal) **

Solved by adding -it and bash to the options

docker run -it image name bash//-> 「-It is possible to hit the command on ubuntuOS in the container with "it image name bash"

-it can be separated by -i and -t (you can execute it with either one, but standard input is not possible or the display is not clean). bash is a shell command that launches inside a container

After that, hit the following commands in order to reach the goal **

cd sample
vim test.md

Thank you for your hard work!

Originally, Docker is often used for web development environment construction and data analysis infrastructure such as python and kotlin. So, you may not be able to create an image to use with this implementation alone: ​​fearful :, It may be less frustrating to see "in a container ..." or "create an image ..." that appears in reference books and sites. I hope this article will increase the number of engineers who are less frustrated with docker.

See you soon····

Recommended Posts

How to write dockerfile
How to write Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
[Basic] How to write a Dockerfile Self-learning ②
How to write and explain Dockerfile, docker-compose
How to write good code
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to deploy
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
How to develop OpenSPIFe
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
How to call AmazonSQSAsync
How to use Map
[Rails] How to write when making a subquery
How to use rbenv
Java Development Basics ~ How to Write Programs * Exercise 1 ~
Write a dockerfile to start jupyter-lab on ubuntu
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to write an if statement to improve readability-java
How to use map
How to use collection_select
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
How to install docker-machine