DEV Community

Sami Ullah Saleem
Sami Ullah Saleem

Posted on

Running your First Container

  1. Enter the following to see how easy it is to get a container running:

Copy code

1
docker run hello-world

It can be just that easy to run a container! You will see output similar to:

alt

There are two sections to the output: output from Docker as it prepares to run the container and the output from the command running in the container.

Take a look at the Docker output first:

In the first line, Docker is telling you that it couldn't find the image you specified, hello-world, on the Docker Daemon's local host. The latest portion after the colon (:) is a tag. The tag identifies which version of the image to use. By default, it looks for the latest version.
In the next line, it notifies you that it automatically pulled the image. You could manually perform that task using the command docker pull hello-world. The library/hello-world is the repository it's pulling from inside the Docker Hub registry. library is the account name for official Docker images. In general, images will come from repositories identified using the pattern account/repository.
The last three lines confirm the pull completed and the image has been downloaded.
Enter fullscreen mode Exit fullscreen mode

The container output is the output that gets written by the command that runs in the container. Read the output to learn more about how it was produced. You ask, but what was the command? You only specified the image name, hello-world. The image provides a default command in this case which prints the output you see.

  1. Re-run the same command:

Copy code

1
docker run hello-world

Notice this time the Docker output is not included. That is because the specific version of the image was found locally and there is no need to pull the image again.

  1. Try running a more complex container with some options specified:

Copy code

1
docker run --name web-server -d -p 8080:80 nginx:1.12

This runs the nginx web server in a container using the official nginx image. You will see output similar to:

alt

This time you specified the tag 1.12 indicating you want version 1.12 of nginx instead of the default latest version. There are three Pull complete messages this time, indicating the image has three layers. The last line is the id of the running container. The meanings of the command options are:

--name container_name: Label the container container_name. In the command above, the container is labeled web-server. This is much more manageable than the id, 31f2b6715... in the output above.
-d: Detach the container by running it in the background and print its container id. Without this, the shell would be attached to the running container command and you wouldn't have the shell returned to you to enter more commands.
-p host_port:container_port: Publish the container's port number container_port to the host's port number host_port. This connects the host's port 8080 to the container port 80 (http) in the nginx command.
Enter fullscreen mode Exit fullscreen mode

You again used the default command in the image, which runs the web server in this case.

  1. Verify the web server is running and accessible on the host port of 8080:

Copy code

1
curl localhost:8080

This command sends an HTTP GET request (a standard web browser request) to localhost port 8080. You will be returned an HTML document, which is the default nginx web page, verifying the nginx server is running in the container:

alt

  1. To list all running containers, enter:

Copy code

1
docker ps

alt

You will see a table with one row below the table column headings, indicating one running process. The (truncated) container id is the same as the last line of the docker run output. One new thing is that you can see the command that is running in the container in the third column. The friendly name you specified is in the last column.

  1. Enter the following to see a list of all running and stopped containers:

Copy code

1
docker ps -a

alt

This time you can see the two hello-world containers from the start of the Lab Step. They simply wrote a message and then stopped when the command finished, whereas the nginx server is always listening for requests until you stop it. Notice that Docker automatically assigned random friendly names for the hello-world containers, musing_volard, and jovial_snyder in the image above. These names are useful if you need to reference a container that you didn't assign a name to by yourself.

  1. To stop the nginx server, enter:

Copy code

1
docker stop web-server

  1. Verify the server is no longer running by running:

Copy code

1
docker ps

This will not return any rows in the table. You can also verify that the default nginx web page is no longer being served by running curl localhost:8080 again.

  1. To start running the command in the web-server container again, enter:

Copy code

1
docker start web-server

This is different from re-running the original docker run command, which would make a second container running the same command instead of using the stopped container.

  1. To see the container's output messages, enter:

Copy code

1
docker logs web-server

alt

Docker logs messages written to standard output and standard error. In the case of nginx, it writes a line for each request that it receives.

  1. You can run other commands in a running container. For example, to get a bash shell in the container enter:

Copy code

1
docker exec -it web-server /bin/bash

Which will cause your shell prompt to change to something similar to:

alt

This indicates you are at a shell prompt in the container using the root container user. The -it options tell Docker to handle your keyboard events in the container. Enter some commands to inspect the container environment, such as ls and cat /etc/nginx/nginx.conf. When finished, enter exit to return to the VM ssh shell. Your shell prompt should change to confirm you are no longer in the container bash shell.

You were able to connect to a bash shell because the nginx image has a Debian Linux layer which includes bash. Not all images will include bash, but exec can be used to run any supported command in the container.

  1. To list the files in the container's /etc/nginx directory, enter:

Copy code

1
docker exec web-server ls /etc/nginx

This runs the ls command and returns to the ssh shell prompt without using a container shell to execute the command. What commands are supported depends on the layers in the container's image. Up until now you have used two images but don't know how to find more.

  1. Stop the nginx container:

Copy code

1
docker stop web-server

  1. Search for an image that you don't know the exact name of, say an image for Microsoft .NET Core, by entering:

Copy code

1
docker search "Microsoft .NET Core"

This searches Docker Hub for images related to the string provided. In this case, the top results related to .NET Core are returned:

alt

This can be useful for recalling the name of an image but not very useful for images that have multiple configuration options. For that you should use the web version of Docker Hub.

  1. Navigate to the following link. You will be viewing the web page of the .NET repository:

alt

You will find all the supported tags along with helpful documentation for using the images. You will usually find useful documentation and examples for images on Docker Hub. You will notice a Dockerfile beside each group of tags on the dotnet page. A Dockerfile is a set of instructions for creating an image and it is what you will dive into in the next Lab Step.

Top comments (0)