Working with Docker Image

  • Start by creating a Docker Hub account if you don’t already have one. You will use it to log in later in this exercise.

  • How to search images on DockerHub ?

$ docker search alpine
  • How to pull an image from DockerHub ?
$ docker image pull alpine
  • How to list images which are available locally ?
$ docker image ls
  • How to list images digests ?
$ docker image ls --digests
  • How to inspect an image ?
$ docker image inspect alpine
  • How to create an image from a running container ?
$ docker container run -it --name myfirstimage alpine sh
$ echo "My first docker image" > /opt/hello.txt

N.B: Detach from the container using CTRL-p CTRL-q

  • Lets check whats the difference between the previous image and current one.
$ docker container diff myfirstimage

You can see the change in /opt

Now, run the commit command to commit your myfirstimage, associate it with a new image named docker-hub-username/alpine:v1.1, where v1.1 is the image tag and docker-hub-username is your Docker Hub username.

$ docker container commit myfirstimage docker-hub-username/alpine:v1.1

Lets start a container from the new image and verify the existence and contents of /opt

$ docker container run -it docker-hub-username/alpine:v1.1 sh
  • How to login to DockerHub from the command line?
$ docker login

After login is succeeded the username would be reflected in the docker output.

$ docker info 
  • Let’s push the image to DockerHub
$ docker image push docker-hub-username/alpine:v1.1

TASK

Now you know how to build a docker image. Here is a task for you. Run a Simple Nginx Server based on an already existing public Docker Hub image.

  • Create a index.html
<h1> Hello This is my First Container </h1> 
  • Build the image and push it to Docker Hub with your own identifier and tag.
  • Run the newly created container in detached mode.
  • Check if your website is reachable from inside the running container.
  • Check if your website is reachable from the local EC2 Instance.

With what we have learned so far, the container should be up and running, the website should be responding from the inside of the Container, but external access should be failing. We will look into why it is failing to be accessed externally in the next section, Docker Networking