0% found this document useful (0 votes)
4 views

6 - Running Your First Docker Container

This document provides a tutorial on running Docker containers on Linux for AWS, including how to pull images from Docker Hub and execute commands. It covers the process of running a simple 'hello-world' container, setting up a web server using nginx, and managing container states. The document also explains how to search for images and access documentation on Docker Hub.

Uploaded by

asifzea7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

6 - Running Your First Docker Container

This document provides a tutorial on running Docker containers on Linux for AWS, including how to pull images from Docker Hub and execute commands. It covers the process of running a simple 'hello-world' container, setting up a web server using nginx, and managing container states. The document also explains how to search for images and access documentation on Docker Hub.

Uploaded by

asifzea7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Menu Browse Library Search in our library...

Training Library / Getting Started with Docker on Linux for AWS


1h 41m 10s left
Running Your First Docker Container

Open Cloud Environment


100%
Setup completed Introduction
Average setup time: 3m 49s
As you learned in the previous Lab Step, containers run a command on top
of an image. There are many available images to choose from. Images are
Credentials collected in repositories. A repository can have many versions of an image.
Tags are used to manage versions. You will use images from repositories
Account ID
inside the default Docker Registry, Docker Hub. Docker Hub hosts official
753705587437 images as well as community-contributed images. Docker Hub offers free
and paid accounts. You get an unlimited amount of public repositories and
Username one free private repository with the free account.
student In this Lab Step, you will run your first container. You will learn how to
search Docker Hub, pull images, and work with containers running on top of
Password the images. You will get practice with many common commands when
working with Docker.
Ca1_VRjAKmFO

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.

2. 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.

3. 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:

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

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:

5. To list all running containers, enter:


Copy code

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 stop web-server

8. 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.

9. 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.

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


Copy code

1 docker logs web-server

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

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

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.

12. 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.

13. Stop the nginx container:


Copy code

1 docker stop web-server

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

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:

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.

Did you like this End


Back Next Step
step? Lab

ABOUT US COMMUNITY HELP


About QA Join Discord Channel Help Center

Legal & Privacy Sitemap System Status Manage your cookies


Copyright © 2024 QA. All rights reserved.

You might also like