Introduction¶
Let’s being our journey learning Docker with a basic example which uses a Debian image and simply prints a message to the console and then exits.
Initial setup¶
Create a directory and a Dockerfile
inside it.
Something like this should do:
$ mkdir -pv ~/docker-examples/hello-v1
$ cd !$
$ 1> ./Dockerfile :
$ tree -CFa .
./
└── Dockerfile
Dockerfile¶
Let’s start with of the most basic Dockerfile
possible: an example that simply echoes a message and then exits.
Dockerfile
FROM debian:latest
CMD ["echo", "Hello Debian on Docker 💯"]
Build the image¶
Then build the image:
$ docker build --tag hello-v1:latest .
Check the image was built:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-v1 latest ef2c51bceec9 7 days ago 117MB
The ID, CREATED and SIZE columns will differ on your machine.
Run the image as a container¶
We are finally able to run the image:
$ docker run --rm hello-v1:latest
Hello Debian on Docker 💯
If we want to update the message, it is necessary to build the image again so the next time it runs, it will print the new message.
Important considerations¶
After the message is echoed to STDOUT, the container stops running.
The process exits with the same exit status of the command run inside the container.
That means, for example, that if we change the command to run an ls
command on a file that does not exist, that ls
will return an exit status other than 0, and that same status is reported back to the host system.
Dockerfile
FROM debian:latest
CMD ["ls", "./does-not-exist.txt"]
$ docker build --tag hello-v1:latest .
$ docker run --rm hello-v1:latest
ls: cannot access './does-not-exist.txt': No such file or directory
$ echo $?
2