I assume you have Docker installed in your system by now.
- Verify docker is installed by running
docker --version
Run Docker hello-world
image
Run the docker hello-world
image by following the steps below.
- Run the hello-world image by running
docker -D run hello-world
-D
runs the docker command in debug mode, so you can see what exactly happening behind the scene. - As you observed, docker first checks if there is an image in the given name “hello-world”. As it does not find in the host machine, it pulls from the docker-hub to host (local machine) and then it runs the image. The runnable instance of the image is called a container.
- The container echos some message on the terminal and exits.
- Now, try to delete the image by
docker rmi hello-world
, you will notice that the image cannot be removed as it has a dependent container. - So, remove the container from machine to clean up by running
docker rm docker_id
- Remove the image by running
docker rmi hello-world
- Now the
hello-world
image is also cleaned up from your system.
Build a custom image from Dockerfile
Download the code example
Steps to build a custom image from a Dockerfile
- Download the code from the above link and unzip the archive.
- Navigate inside the hello-docker directory,
cd awesome-docker-examples-master/hello-docker/
Spend some time in looking into theDockerfile
, this is the file that instructs Docker what/how to build the expected image. - Now let us build the image from Dockerfile,
docker -D build -t hello-docker:latest .
-D
runs the docker build command in debug mode so you know what is happening.
-t
to specify the Name and optionally a tagname:tag
.
is to specify the path where Dockerfile exists.
docker build
– To build the docker image from Dockerfile - Run
docker build --help
to know all the commands available for building an image. - Now that image hello-docker is built, type
docker images
to see the list of images stored in your host. - A runnable instance of an image is called container, so to run docker container
docker run -i --name hello-docker-container hello-docker
-i
starts the container in an interactive mode, very useful for advanced options
--name hello-docker-container
assigns the container this name. By default, docker tries to assign a unique name to every runnable container, for learning purpose I tried to show you how to name a given container.
Usedocker run --help
to know all the options available for running a container. - To see all the runnable containers existing in your system, run
docker container ls --all
- Cleaning up the container and images. You can refer the hello-world example above to make use of the
docker rm
anddocker rmi
to clean up the unnecessary containers and images from your host.
Watch the screencast below
A closer look at Dockerfile
As explained above, Dockerfile has the instructions or commands that explains to the docker how to and what to build. We will look into this little more in details as this is the most important of Docker tutorial.
FROM: Explains the base image that is used to build the custom hello-docker image. This is like inheritance/reusability.
LABEL: This is to set some labels/meta information about your image. Like version, author, email, license, and etc.
CMD: Command to run in the hello-docker image
There are several other commands as below.
FROM, LABEL, COPY, RUN, EXPOSE, ENV, ADD, COPY, ENTRYPOINT, VOLUME, USER, WORKDIR, ARG, ONBUILD, STOPSIGNAL, HEALTHCHECK, SHELL. All these commands need to be written in capital letter.
In the next tutorial, we will spend some more time in understanding each of these commands. Let me know your questions in the comment below.