
This assumes you have installed Docker on Linux and Docker is running.
A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).
Let's start by creating a Dockerfile that will be used to create our image.
touch Dockerfile
Add the following to the Dockerfile. This will download the latest CentOS image and every time a container is created using the image, Hello World will be printed.
FROM centos:latest
CMD echo "Hello World"
Let's create an image named my-hello-world-image using the Dockerfile.
~]$ sudo docker build --tag jeremycanfield/hello-world:latest .
Sending build context to Docker daemon 340.6MB
Step 1/2 : FROM centos:latest
---> 5d0da3dc9764
Step 2/2 : CMD echo "Hello World"
---> Running in 1e40e09593f2
Removing intermediate container 1e40e09593f2
---> 1120808b5d7a
Successfully built 1120808b5d7a
Successfully tagged jeremycanfield/hello-world:latest
The docker images command should now show that jeremy/canfieldhello-world:latest was created.
~]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jeremycanfield/hello-world latest 1120808b5d7a 2 minutes ago 231MB
And the docker run command can be used to create a container from the image, which outputs Hello World.
~]$ sudo docker run --detach --name hello-world jeremycanfield/hello-world:latest
Hello World
And the docker logs command will also show that "Hello World" was appended to the containers log.
~]$ sudo docker logs hello-world
Hello World
Did you find this article helpful?
If so, consider buying me a coffee over at