Remove containers

We need to get the container ID or name from docker ps and then run this command:

docker rm <container_id_or_name>

Running a Web server

Background web server container

The hello-world container was short-lived and executed in foreground. Docker may also be used to run background services like a Web server.

docker run -d -p 9191:80 --name web_server nginx

If we access port 9191 of our host machine after executing the previous command we'll see the Nginx web server welcome page.

Let's break that command into parts:

-d runs the container in background.

-p 9191:80 exposes internal port 80 of the container in port 9191 of the host machine.

--name web_server specifies the name of the container as web_server.

nginx runs a container using the nginx image with the default command.

Internal access to the container

We now want to access an interactive shell in our running web server container.

By using the docker exec command we may run a new command in a running container:

docker exec -it web_server bash

Note that all commands executed with docker exec are tied to the container's primary process and will be killed if the container stops.

Let's get some new static content to host in our web server by running the following commands inside the container:

apt-get update && apt-get install -y wget

cd /usr/share/nginx/html/

wget -O image.png http://lorempixel.com/300/200/animals/

We've downloaded a new image named image.png to our web server root directory. We can view that image by going to:

http://<hostname>:9191/image.png

Building a custom Web server image

Write a Dockerfile

Up until this point we've created a web server container and run some commands in an interactive shell inside it. What happens if we need to replicate or regenerate the container? Do we need to manually run all those commands again?

One of the main principles of Docker is that containers must be ephemeral, which means that the regeneration cost must be minimal. Having to run a bunch of commands each time we create a container goes against this.

The answer is writing a Dockerfile. This file is basically a recipe, a set of instructions that will be run in order to create a custom Docker image.

The following set of instructions represent a Dockerfile to encapsulate the actions of the previous step.

Debugging Scenarios

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: