Introduction
This is an article showing how to build a docker image using a Dockerfile file. But in this article, it will just show a minimum requirement for building the docker image. By executing a certain command using a Dockerfile, it will generate docker image. Building the image is possible in any kind of operating system as long as the requirement is available.
Build Docker Image from a Dockerfile
So, how is the process for building a docker image using docker command with the definition of a Dockerfile file ?. Basically, there are several requirements which is very important to do that. The following are those requirements to be able to do that :
-
Check whether docker tool is avaiable or not. It is very easy to do it. Just execute ‘docker’ in the command line. If there is no docker tool available in the operating system, just install it first. There are so many articles in this site discussing about how to install docker. The first one is ‘How to Install Docker CE in Linux CentOS 7’ in this link. Furthermore, there is an article with the title of ‘How to Install Docker in Linux CentOS 8’ in this link. Another one is an article with the title of ‘How to Install Docker in Linux Ubuntu 18.04 Bionic Beaver’ in this link. The last one is ‘Install Docker in CentOS 7 via command line’ in this link. Otherwise, just search by googling it.
-
Test it again by executing the command ‘docker’ as follows :
Microsoft Windows [Version 10.0.18363.628] (c) 2019 Microsoft Corporation. All rights reserved. C:\Users\Administrator>docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "C:\\Users\\Administrator\\.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "C:\\Users\\Administrator\\.docker\\ca.pem") --tlscert string Path to TLS certificate file (default "C:\\Users\\Administrator\\.docker\\cert.pem") --tlskey string Path to TLS key file (default "C:\\Users\\Administrator\\.docker\\key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: builder Manage builds buildx* Build with BuildKit (Docker Inc., v0.6.3) compose* Docker Compose (Docker Inc., v2.1.1) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins scan* Docker Scan (Docker Inc., 0.9.0) secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. To get more help with docker, check out our guides at https://docs.docker.com/go/guides/ C:\Users\Administrator>
-
Next, since docker is available just create the file with the name of ‘Dockerfile’ with the following content :
# pull image FROM centos
-
Finally, execute the following command to build the image :
C:\Users\Administrator>docker build . [+] Building 0.6s (5/5) FINISHED => [internal] load build definition from Dockerfile 0.7s => => transferring dockerfile: 32B 0.1s => [internal] load .dockerignore 0.6s => => transferring context: 2B 0.1s => [internal] load metadata for hub.docker.local:5000/centos:latest 0.0s => CACHED [1/1] FROM hub.docker.local:5000/centos 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:22773018c04267669821153cd42ef21101902b10a8a16f340fbef85a77821d03 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them C:\Users\Administrator>
- Finally, the following is the command to check the image built from the previous process :
C:\Users\Administrator>docker image list --all REPOSITORY TAG IMAGE ID CREATED SIZE registry latest b8604a3fe854 3 weeks ago 26.2MB bash latest bacfc038bc2c 3 weeks ago 12.9MB <none> <none> 22773018c042 2 months ago 231MB centos latest 5d0da3dc9764 2 months ago 231MB hub.docker.local:5000/centos latest 5d0da3dc9764 2 months ago 231MB C:\Users\Administrator>
In the above output, the one without any name in the repository and tag column is the new created image.
One thought on “How to Build Docker Image from a Dockerfile”