Home  > Resources  > Blog

Getting Started with Docker

January 1, 1970 by Karandeep Kaur

This tutorial is adpated from Web Age course Architecting Microservices with Kubernetes, Docker and Continuous Integration.

What is Docker?

Docker is an open IT automation platform widely used by DevOps, and in this tutorial, we will review the main Docker commands. In this tutorial, you will install Docker and use it’s basic commands. You will also create a custom image by creating a Dockerfile.

Part 1 – Setting the Stage

1. Open a new Terminal window by clicking Applications > Terminal.

2. Switch the logged-in user to root:

sudo -i

When prompted for the logged-in user’s password, enter your password.

3. Enter the following command:


You should see that you are root now.


Note: Now that you are root on your Lab server, beware of system-wrecking consequences of issuing a wrong command.

4.  Create a folder ‘Works’  in your home directory in your and enter the following command:

cd /home/<your user name>/Works

You should see that your system prompt has changed to ‘Works’ folder

5. Get directory listing:


Download SimpleGreeting-1.0-SNAPSHOT.jar http://www.webagesolutionsfiles.com/Karandeep/SimpleGreeting-1.0-SNAPSHOT.jar file. You will use this file later in this lab. It will be deployed in a custom Docker image and then you will create a container based on that image.

Part 2 – Learning the Docker Command-line

Get quick information about Docker by running it without any arguments.

1. Run the following command:

docker | less

__2. Navigate through the scrollable output using your arrow keys and review Docker’s commands.

__3. Enter q to exit.

The commands list is shown below for you reference.

  attach      Attach local standard input, output, and error streams to a
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's 
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit

__4. You can get command-specific help by using the –help flag added to the commands invocation line, e.g. to list containers created by Docker (the command is called ps), use the following command:

docker ps --help

More information on Docker’s command-line tools can be obtained at https://docs.docker.com/reference/commandline/cli/

Part 3 – Run the “Hello World!” Command on Docker

Let’s check out what OS images are currently installed on your Lab Server.

__1. Enter the following command:

docker images

Notice there are a few images in the VM. You will use them in various labs.

One of the images is ubuntu:12.04.

__2. Enter the following command:

docker run ubuntu echo 'Yo Docker!'

Notice it displays Yo Docker! message

So, what happened?

docker run executes the command that follows after it on a container provisioned on-the-fly for this occasion.

When the Docker command completes, you get back the command prompt, the container gets stopped and Docker creates an entry for that session in the transaction history table for your reference.

Part 4 – List and Delete Container

In this lab part, we will need a second terminal also running a shell as root.

__1. Open a new terminal, expand it width wise (horizontally) to capture the output of the docker ps command we are going to run into it. Also make sure it does not completely overlap the original terminal window.

We will be referring to this new terminal as T2; the original terminal will be referred to as T1.

__2. In the new terminal (T2) become root (use the sudo -i command).

__3. Change to the ~/Works directory:

cd /home/wasadmin/Works

__4. Enter the following command:

docker ps

You should see an empty container table listing only the captions part of the table.


__5. To see a list of all containers, whether active or inactive, run the following command:

docker ps -a

__6. Switch to the original terminal window (T1).

__7. Enter the following command and replace the container id with the one from T2:

docker rm <container id>

Note: You may only need to type the first two or three characters of the container id and then press the Tab key – Docker will go ahead and auto-complete it.

The container id shown to the user is, actually, the first 12 bytes of a 64 hexadecimal character long hash used by Docker to create unique ids for images and containers.

The docker ps command only shows the running containers; if you repeat the docker ps command now after you have killed the container process, it will show the empty table.

In order to view all the containers created by docker with stopped ones stashed in the history table, use the -a flag with this command.

__8. Switch to the T2 terminal.

__9. Enter the following command:

docker ps

Now it should show an empty table.

Part 5 – Working with Containers

__1. Switch to the T1 terminal.

__2. Enter the following command:

docker run -it --hostname basic_host ubuntu /bin/bash

This command will create a container from the ubuntu OS image, it will give the instance of the container the hostname of basic_host, launch the bash program on it and will make the container instance available for interactive mode (i) over allocated pseudo TTY (t).

After running the above command, you should be dropped at the prompt of the basic_host container.


As you can see, you are root there (symbolized by the ‘# prompt sign).

__3. Enter the following command to stop the container and exit out to the host OS:


You should be placed back in the root’s Works folder.

__4. Enter the following command to see the containers:

docker ps -a

You should see the status as Exited.

__5. Enter the following command to restart the container:

docker start <container id>

__6. Connect to the container:

docker exec -it <container id> /bin/bash

__7. Enter the following command:


You should be logged off and placed back in the root’s Works folder.

__8. Enter the following command:

docker stop <container id>

__9. Enter the following command:

docker ps -a

You should see that the container is listed in the transaction history.

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS                    NAMES
aed47e954acd        ubuntu              "/bin/bash"         3 minutes ago      Exited (0) 4 minutes ago        amazing_panini   

Part 6 – Create a Custom Image

Now that we have ourselves a container (which is currently in the exited / stopped status), let’s create a custom image based on it.

__1. Enter the following command providing your container id (aed47e954acd, in our case):

docker commit <container id>  my_server:v1.0

You should get back the OS image id generated by Docker.

__2. Enter the following command:

docker images

You should see the new image listed on top of the available images in our local image repository.

__3. Enter the following command:

docker run -it my_server:v1.0 /bin/bash

This command will start a new container from the custom image we created in our local image repository.

__4. Enter the following command at the container prompt:


You will be dropped back at the Lab Server’s prompt.

Part 7 – Workspace Clean-Up

__1. Enter the following command:

docker ps -a

This command will show all the containers and not only the running ones.

It is always a good idea to clean-up after yourself, so we will remove all the containers we created so far.

__2. Enter the following command for every listed container id:

NOTE: Do NOT delete any image or container other than the ones you have created in this lab. Leave OpenShift and other images / containers as is.

docker rm <container id>    

__3. Verify there are no docker containers:

docker ps -a

__4. Remove the my_server:v1.0 image we created and persisted in our local image repository:

docker rmi my_server:v1.0

__5. Verify your image has gone:

docker images

Part 8 – Create a Dockerfile for Building a Custom Image

In this part you will download Ubuntu docker image and create a Dockerfile which will build a custom Ubuntu based image. It will automatically update the APT repository, install JDK, and copy the SimpleGreeting.jar file from host machine to the docker image. You will also build a custom image by using the Dockerfile.

__1. In the terminal, type following command to create Dockerfile:

gedit Dockerfile

__2. Type in following code:

# lets use OpenJDK docker image
FROM openjdk
# deploy the jar file to the container
COPY SimpleGreeting-1.0-SNAPSHOT.jar /root/SimpleGreeting-1.0-SNAPSHOT.jar

Note: The Dockerfile creates a new image based on OpenJDK and copies host’s /home/wasadmin/Works/SimpleGreeting-1.0-SNAPSHOT.jar to the image under the root directory.

__3. Click Save button.

__4. Close gedit. You will see some errors on the Terminal, ignore them.

__5. Type ls and verify your new file is there.

__6. Run following command to build a custom image:

docker build -t dev-openjdk:v1.0 .

This command builds / updates a custom image named dev-openjdk:v1.0. Don’t forget to add the period at the end of docker build command.

Notice, Docker creates the custom image with JDK installed in it and the jar file deployed in the image. The first time you build the job, it will be slow since the image will get built for the first time. Subsequent runs will be faster since image will just get updated, not rebuilt from scratch.

Part 9 – Verify the Custom Image

In this part you will create a container based on the custom image you created in the previous part. You will also connect to the container, verify the jar file exists, and execute the jar file.

__1. In the terminal, run following command to verify the custom image exists:

docker images

Notice dev-openjdk:v1.0 is there

__2. Create a container based on the above image and connect to it:

docker run --name dev --hostname dev -it dev-openjdk:v1.0 /bin/bash 

Note: You are naming the container dev, hostname is also dev, and it’s based on your custom image dev-openjdk:v1.0

__3. Switch to the root directory in the container:

cd /root

__4. Get the directory list:


Notice SimpleGreeting*.jar file is there.

__5. Execute the jar file:

java -cp SimpleGreeting-1.0-SNAPSHOT.jar com.simple.Greeting

Notice it displays the following message:

__6. Exit out from the container to the command prompt:


__7. Get active docker container list:

docker ps

Notice there’s no active container.

__8. Get list of all docker containers:

docker ps -a

Notice there’s one inactive container named dev.

Part 10 – Working with a Docker Volume

In this part, you will manage a Docker volume. You will create a volume, mount it in a container, store content in it, and delete the volume.

__1. Create a volume:

docker volume create hello

__2. Create a container and mount it in the container:

docker run -v hello:/world -it ubuntu /bin/bash

__3. View mount point in container: (it will show the /world directory)

ls / -al

__4. Create a file in the volume:

echo "hello world" > /world/test.txt

__5. Exit out of the container:


__6. Destroy the container: (Note: Make a note of the ID of your container and substitute it below)

docker ps -a
docker stop <id>
docker rm <id>

__7. Create another container and attach the volume as a mount point:

docker run -v hello:/world -it ubuntu /bin/bash

__8. View mount point in container: (it will show the /world directory)

ls /world

__9. View contents of the file:

cat /world/test.txt

__10. Exit out of the container:


__11. View all volumes:

docker volume ls

Part 11 – Cleanup Docker

In this part you will clean up docker by removing containers and images.

__1. In the terminal, run following to get list of all containers:

docker ps -a

__2. In case, if there are any containers, stop (if running) and remove them by running following commands. Don’t remove the ones you haven’t created yourself in this lab:

docker stop <container>
docker rm <container>

__3. Get list of all docker containers. It should be empty:

docker ps -a

__4. Delete the volume:

docker volume rm hello

__5. Get docker image list:

docker images

__6. Remove all images that you created by executing following command:

docker rmi <REPOSITORY:TAG> 

e.g. docker rmi dev-openjdk:v1.0

__7. Make sure your image has been deleted:

docker images

__8. In each Terminal type exit and then close the Terminal window.

Part 12 – Review

In this lab, we reviewed the main Docker command-line operations.

Follow Us

Blog Categories