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 :

sudo touch Dockerfile

then edit the file

sudo nano Dockerfile

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

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:

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:

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:

 "Hello I did it!" 

printed on the screen.

If you want to see your image just use

 sudo docker images

and you will be shown that your image was made

 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.

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

© bigsk1. Some rights reserved.

AI | Tech | HomeLab | Crypto | Docker and more 🚀