Image Management with Docker
- List the available images on our host. Notice when the images are not available on the host, they are by default downloaded from DockerHub.
student@ubuntu:~$ sudo docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 174c8c134b2a 3 weeks ago 77.9MB
nginx latest d453dd892d93 2 months ago 187MB
hello-world latest d2c94e258dcb 8 months ago 13.3kB
- If the image is already existing on the host, then creating a container is very quick. Let’s see how to pull an image from the registry:
student@ubuntu:~$ sudo docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
661ff4d9561e: Pull complete
Digest:
sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f48
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
- By default, the Daemon is configured to fetch the image from the Docker Hub registry and from the user account library, where Docker stores all the publicly available official images and fetches the image with tag as latest. We can override the default behavior by specifying what we want.
student@ubuntu:~$ sudo docker image pull gcr.io/google-samples/hello-app:2.0
2.0: Pulling from google-samples/hello-app
07a64a71e011: Pull complete
fe5ca62666f0: Pull complete
b02a7525f878: Pull complete
1933f300df8c: Pull complete
36cc1f0ec872: Pull complete
Digest:
sha256:7104356ed4e3476a96a23b96f8d7c04dfa7a1881aa97d66a76217f6bc8a370d0
Status: Downloaded newer image for gcr.io/google-samples/hello-app:2.0
gcr.io/google-samples/hello-app:2.0
- Let us build a simple image using Dockerfile. We will use the index.html file we created in an earlier step and copy it to our new image.
student@ubuntu:~$ cat > Dockerfile <<EOF
> FROM nginx
> COPY . /usr/share/nginx/html
> EXPOSE 80/tcp
> CMD ["nginx", "-g daemon off;"]
> EOF
student@ubuntu:~$ sudo docker image build . -t nginx:version1
[+] Building 0.4s (7/7) FINISHED
docker:default
=> [internal] load .dockerignore
0.1s
=> => transferring context: 2B
0.0s
=> [internal] load build definition from Dockerfile
0.1s
=> => transferring dockerfile: 123B
0.0s
=> [internal] load metadata for docker.io/library/nginx:latest
0.0s
=> [internal] load build context
0.1s
=> => transferring context: 5.74kB
0.0s
=> [1/2] FROM docker.io/library/nginx
0.1s
=> [2/2] COPY . /usr/share/nginx/html
0.1s
=> exporting to image
0.0s
=> => exporting layers
0.0s
=> => writing image
sha256:69be93e604b157a2dda142b3444100e5e24013f2c29db16bd25c0c8de84c28b2
0.0s
=> => naming to docker.io/library/nginx:version1
- Create a container using the image we built and access the application.
student@ubuntu:~$ sudo docker container run -dit --name webapp -p 8081:80
nginx:version1
8fd1b47bafda05c96faba7d46486ca90f4a828c221cb5fd689e819592b839c84
student@ubuntu:~$ curl localhost:8081
Welcome to Docker Training!!
- Clean up the unused images on the host by executing the following command:
student@ubuntu:~$ sudo docker image prune -a
WARNING! This will remove all images without at least one container
associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine:latest
untagged:
alpine@sha256:51b67269f354137895d43f3b3d810bfacd3945438e94dc5ac55fdac340352f
48
deleted:
sha256:f8c20f8bbcb684055b4fea470fdd169c86e87786940b3262335b12ec3adef418
deleted:
sha256:5af4f8f59b764c64c6def53f52ada809fe38d528441d08d01c206dfb3fc3b691
untagged: gcr.io/google-samples/hello-app:2.0
<Output truncated>
- There are additional image options; explore the sudo docker image –help command and try
additional options.