6 - Running Your First Docker Container
6 - Running Your First Docker Container
Region
Instructions
US West 2
1. Enter the following to see how easy it is to get a container running:
PEM PPK Copy code
Download Download
1 docker run hello-world
Bridge Connection
100% Completed It can be just that easy to run a container! You will see output similar to:
Lab steps
1 Logging In to the Amazon
Web Services Console
2 Connecting to the Virtual There are two sections to the output: output from Docker as it prepares to
Machine using EC2 run the container and the output from the command running in the
Instance Connect container.
3 Installing Docker on
Amazon Linux Take a look at the Docker output first:
4 Using Docker without Root 1. In the first line, Docker is telling you that it couldn't find the image you
Permission on Linux specified, hello-world, on the Docker Daemon's local host.
The latest portion after the colon (:) is a tag. The tag identifies which
5
version of the image to use. By default, it looks for the latest version.
Getting Docker Help from 2. In the next line, it notifies you that it automatically pulled the image.
You could manually perform that task using the command docker
the Command Line pull hello-world. The library/hello-world is the repository it's
Menu Browsepulling
Library from inside the Docker Hub registry. library is the account
6 Running Your First Docker name for official Docker images. In general, images will come from
Container repositories identified using the pattern account/repository.
3. The last three lines confirm the pull completed and the image has
been downloaded.
7 Creating Your First Docker
Image
The container output is the output that gets written by the command that
Need help? Contact our support team 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.
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.
This runs the nginx web server in a container using the official nginx image.
You will see output similar to:
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.
You again used the default command in the image, which runs the web
server in this case.
Menu Browse Library
4. Verify the web server is running and accessible on the host port of 8080:
Copy code
1 curl localhost:8080
1 docker ps
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.
6. Enter the following to see a list of all running and stopped containers:
Copy code
1 docker ps -a
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.
Menu Browse Library
7. To stop the nginx server, enter:
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.
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.
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.
11. You can run other commands in a running container. For example, to get
a bash shell in the container enter:
Copy code
Which will cause your shell prompt to change to something similar to:
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
Menu DebianLibrary
Browse Linux layer which includes bash. Not all images will include bash, but
exec can be used to run any supported command in the container.
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.
14. Search for an image that you don't know the exact name of, say an
image for Microsoft .NET Core, by entering:
Copy code
This searches Docker Hub for images related to the string provided. In this
case, the top results related to .NET Core are returned:
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.
15. Navigate to the following link. You will be viewing the web page of
the .NET repository:
Menu Browse Library
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.
Summary
In this Lab Step, you explored the commands related to working with
containers. You also ran a few containers. You learned the steps Docker
takes to run a container and how to find Docker Hub images to create
containers from.