We have seen how to pull a Docker image from DockerHub and run in, now we will see how to create our own Docker image, build and run it.
The first step to create a Docker image is to create a Dockerfile.
Dockerfiles is a text file that contains instructions and steps to create a
new Docker image.
A Dockerfile has no extension. If you are using docker on windows use notepad to create a dockerfile while saving select "All type" and save the file name as "Dockerfile".
The first instruction of a Dockerfile is always a FROM
instruction. This tells Docker which Docker image to start from.
We can start with FROM python:3
to build a Python environment
We can start with FROM node:current-slim
to build a Node environment
We can start with FROM ubuntu:18.04
to build a Linux Ubantu environment
In this sample we will start with FROM scratch
The scratch image is Docker’s reserved empty image, which is useful for
building base images
The Dockerfile for our HelloWorld sample image will be as
follows
FROM scratch
ADD hello /
CMD ["/hello"]
The ADD hello / line adds the hello executable to the image, you can built the
hello executable example by following the instructions at https://github.com/docker-library/hello-world/,
or you can download the executable from there. You can also build your own
executable instead and use it with the Docker image.
Once we create and save the file, we can use the docker build command to build
the image using the Dockerfile
In the Docker command prompt navigate to the directory where
the new Dockerfile was created and execute the following command. Make a node
of the (.) at the end of the command,
this is required and sets the build context to the current directory.
$ docker build -t
helloworld .
Once the image is build, list the images and make sure that the new image is
created
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld latest 8befd08417b0 2 minutes ago 13.3kB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
$ docker run helloworld
Hello from Docker!
This message shows that your installation appears to be working correctly.
No comments:
Post a Comment