[PYTHON] Step by Step for creating a Dockerfile

The Dockerfile is similar to the procedure manual, and you can write it immediately, but it is quite difficult to check whether the content (= procedure) is correct. After actually making it, I've come to understand the points, so I'll share the procedure.

FROM selection

The Dockerfile can be written by inheriting the existing Dockerfile. The FROM described at the beginning is used for this inheritance. In order to make things easier, it is important to first find an official Dockerfile that is close to the environment you want to build.

In my case, I often create a Python environment, so I often inherit the Python / Miniconda Dockerfile by FROM.

After that, I will write while looking at the sample of Dockerfile.

Build test

When you finish writing, test whether you can build an environment properly with it. The docker build command is used for this purpose.

docker build

The basic usage is as follows.

docker build --force-rm=true -t my_image .

If you build in an environment with a proxy, but you don't have a proxy in a production environment, you can pass environment variables with --build-arg.

docker build --force-rm=true -t my_image --build-arg http_proxy=http://myproxy:8080 --build-arg https_proxy=http://myproxy:8080 .

As mentioned above, it is rare that the build will pass in one shot, so in that case, delete the image that failed to build. If you neglect to do this, it will be very troublesome to grind the disc and erase it later.

Clean up after build

Now it's clean. After that, the build will be executed and cleaned up repeatedly.

run test

After creating the image, try running it and check if it works properly. Use docker run to create a container from the image and run it.

docker run

When actually using it, it will look like the following.

docker run -p 8080:8080 --rm my_image

Clean up after run

Note that docker run creates a container from the image each time it is run (creating a container from the image and running the container with a single command). Therefore, if you hit docker run repeatedly, it often happens that the container overflows. Make sure you do docker rm from docker stop so that the disk is not occupied by the container.

Once you've tested the run, you've created your Dockerfile exactly. Thank you for your hard work!

Recommended Posts

Step by Step for creating a Dockerfile
A memo for creating a python environment by a beginner
Dockerfile for creating a data science environment based on pip3
Commands for creating a new django project
Memo for creating a text formatting tool
Creating a cholera map for John Snow
Creating a development environment for machine learning
A textbook for beginners made by Python beginners
A tool for creating symbolic links on Windows
Procedure for creating a LineBot made with Python
I created a Dockerfile for Django's development environment
Commands for creating a python3 environment with virtualenv
Procedure for creating a Python quarantine environment (venv environment)
Try refactoring step by step
[Day 9] Creating a model
Creating a Home screen
4. Creating a structured program
Creating a scraping tool
Procedure for creating a Line Bot on AWS Lambda
Creating a dataset loader
The story of creating a VIP channel for in-house chatwork
The first step to creating a serverless application with Zappa
(For beginners) Try creating a simple web API with Django
I want to create a Dockerfile for the time being.
Try creating a CRUD function
Made a command for FizzBuzz
Python vba to create a date string for creating a file name