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.
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.
FROM python: 3
conda install
in environment constructionFROM continuumio / miniconda3
. Specify tags as neededAfter that, I will write while looking at the sample of Dockerfile.
When you finish writing, test whether you can build an environment properly with it. The docker build
command is used for this purpose.
The basic usage is as follows.
docker build --force-rm=true -t my_image .
--force-rm = true
: Make sure to remove the intermediate container created during the build. It's rare for a build to work from the first time, so make sure you don't leave an odd container on failure.-t
: Set the name (tag) of the image to be created.
in the case of the current directory).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.
docker ps -a
to check if there is a container being createddocker rm
. In fact, you can specify the container with only the first 3 characters of the ID.docker ps
only shows running containers, so it's often the case that a lot of containers were hidden behind when I thought there was nothing. Please notedocker images
docker rmi
Now it's clean. After that, the build will be executed and cleaned up repeatedly.
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.
When actually using it, it will look like the following.
docker run -p 8080:8080 --rm my_image
-p
option to associate the container side port with the host side port. This option is often used to check the operation of Web applications.--rm
option, the container will be automatically deleted after stopping. If you neglect this option, you will need to clean up the following:docker ps
docker stop
docker rm
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