Post

Docker build a simple image

How to build a simple docker image in seconds


The first thing you need is docker installed


Creating the Dockerfile

To build a simple “Hello I did it!” Docker image, you will need to create a Dockerfile that defines the instructions for building the image.

The Docker file needs to be just that a file named Dockerfile , all one word and capitol D, in linux it would be :


1
sudo touch Dockerfile


then edit the file

1
sudo nano Dockerfile


Paste the contents in the file and save it. CTL+X then Enter


1
2
3
FROM alpine
CMD ["echo", "Hello I did it!"]


This Dockerfile uses the alpine image as a base, and runs the command echo “Hello I did it!” when the container is started.


Building the Image


Once you have created the Dockerfile, you can build the image using the docker build command. Here is an example command that you can use to build the image:


1
2
docker build -t my-hello-image .


This command tells Docker to build an image using the Dockerfile in the current directory (indicated by the . at the end) and tag the image with the name my-hello-image.


Running the Image


Once the image is built, you can run it using the docker run command. Here is an example command that you can use to run the image:


1
2
docker run my-hello-image


This command tells Docker to run the my-hello-image image and output the message “Hello I did it!”

And that’s it, you can now use the command docker run my-hello-image to run your newly created image and see the message:


1
 "Hello I did it!" 

printed on the screen.


If you want to see your image just use


1
 sudo docker images


and you will be shown that your image was made


1
2
 REPOSITORY             TAG       IMAGE ID       CREATED          SIZE
my-hello-image         latest    26cdf940f9e6   50 minutes ago   7.04MB

To remove your image


Just use the command rmi and –force , you can also remove the alpine image that was made when building the image the same way.


1
sudo docker rmi <IMAGE-ID> --force
This post is licensed under CC BY 4.0 by the author.