Steps

Docker tutorial

Step 1 - Run your first container

Docker downloaded an image of the container hello-world, started the container, ran a single command ("hello") and terminated the container. Lets try to understand what happenned.

When the Docker engine received the command, it checked locally if the container image hello-world existed. Since it did not exist, it internally triggerred an action to pull the image. For reference, you can manually trigger image pulls using docker pull hello-world. It then ran the image with the default command (specified when the image is build). This started a new container and ran the default command. Once the command completed, it exited the container.

You can change the status of your container by running

docker ps

You will NOT see any container because your container has existed. To list exited and running containers, run docker ps -a.

You can also check which images are present on your local system by running:

docker images

Now let us dissect this image. The container image hello-world is hosted at DockerHub hello-world page. This image has the following Dockerfile:

Lets start at the bottom and work our way up this file. The CMD is specifying that when this container is run, without a command specified, it runs the command /hello. How did /hello get there? It was copied in using the COPY command. The FROM command specifies that this container was built using scratch which is a special image for building brand new images. Note: Most image you will work with will NOT be built from scratch but build from a layer such as ubuntu or coreos.

If you question where the fille hello was copied from, it was copied from the host where the image was being built. Hello must be in the folder from where docker build was called at the time the image was built. You will soon learn how to build your own images.

What happens when you run the container with a non-default command? docker run hello-world ls

This command fails because ls is a not a known command inside this container. The image hello-world has one and only one file, hello and hence it does not support running any other commands.

Step 2 - Run a more interesting container

We can also run images more interesting than hello-world which multiple files. One great examples is the busybox image. It is a great place to learn Docker as it contains the binaries of most common Linux command such as ls, echo, etc. Lets first pull the busybox image

docker pull busybox

Now run a few commands to understand the image.

docker run busybox pwd

This command will tell you that the commands you are are using / as the default working directory. This is another parameter we can set using the Dockerfile at the time of building the image.

Lets see the contents of this working directory:
docker run busybox ls

You can ls other folder in the image, for example,
docker run busybox ls /bin

Now, try this:
docker run busybox whoami
`

The commands in busybox are run as root (the root inside the container) by default.

You can also work with a container interatively and get on a prompt to look around. It can be done as follows:

docker run -it busybox sh

You can learn more about your container here, e.g., run ifconfig to see what IP has Docker assigned to the container.

Step 3 - Container lifecycle and ports

Host a simple webpage

First, lets revise a few key concepts.

The lifetime of a container is equal to the life time of the master process. If the master process is deamon such as nginx, the container runs for a longer period of time.

We can use the docker run with -d to run the container in a daemon mode

We can use forward a port of the container using docker run with -p <container port>:<host port>

For this step, let us nginx

Pull the nginx conainer:
docker pull nginx

Run the nginx container as follows:
docker run -p 80:80 --name=mynginx nginx

You will see that the screen hangs and you do not get any output. At this time, you can try to open another terminal window.

To avoid typing in the nginx command when running the container you can add a default command to the container image using the directive CMD to the end of the Dockerfile. Try it out by adding the line CMD ["nginx", "-g", "daemon off;"] to the end of the Dockerfile.

Now figure out the steps to change the text displayed from Hello World to "I have got this"

Stop and remove your container.

Step 5 - Run a full application

While running a single container is useful, it is far more useful to be able to run multiple containers in tandem such that an entire application can be run. Docker makes this feature available through docker-compose.

Help

Katacoda offerings an Interactive Learning Environment for Developers. This course uses a command line and a pre-configured sandboxed environment for you to use. Below are useful commands when working with the environment.

cd <directory>

Change directory

ls

List directory

echo 'contents' > <file>

Write contents to a file

cat <file>

Output contents of file

Vim

In the case of certain exercises you will be required to edit files or text. The best approach is with Vim. Vim has two different modes, one for entering commands (Command Mode) and the other for entering text (Insert Mode). You need to switch between these two modes based on what you want to do. The basic commands are: