Docker – Working with Images

In this post, let’s play with images through a couple of tutorials.

  • [Tutorial 1] Create a Docker image (Ubuntu 20.04 + Python3) with a “hello-world” application written in Python.
  • [Tutorial 2] Create an nginx image and connect the host port to the nginx port in the container

[Tutorial 1] Dockerfile

1. Build your own Image using Dockerfile

  • Create a Dockerfile
$ mkdir docker-demo
$ cd docker-demo
$ vim dockerfile
FROM ubuntu:20.04
RUN apt-get update
RUN apt-get install -y python3
  • Build a Docker image
$ docker image build .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu:20.04
...
Step 2/3 : RUN apt-get update
...
Step 3/3 : RUN apt-get install -y python3
...
Successfully built 14ac49121f70
$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
<none>       <none>    14ac49121f70   2 minutes ago   150MB
ubuntu       20.04     a0ce5a295b63   4 weeks ago     72.8MB

Notice that you have 2 images: the final image + the base image.


2. Run the Container and Create a python script

$ docker container run -it --name my-container 14ac49121f70
root@14352f7101b5:/# cat /etc/issue
Ubuntu 20.04.5 LTS \n \l

root@14352f7101b5:/# echo "print('Hello World')" > hello-world.py

root@14352f7101b5:/# cat hello-world.py 
print('Hello World')

root@14352f7101b5:/# chmod +x hello-world.py

root@14352f7101b5:/# python3 hello-world.py 
Hello World

root@14352f7101b5:/# exit

3. Clean up Containers and Images

$ docker container ls -a
CONTAINER ID   IMAGE          
14352f7101b5   14ac49121f70 
$ docker container rm 14352f7101b5

$ docker container ls -a
$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    14ac49121f70   36 minutes ago   150MB
ubuntu       20.04     a0ce5a295b63   4 weeks ago      72.8MB
$ docker image rm a0ce5a295b63
Error response from daemon: conflict: unable to delete a0ce5a295b63 (cannot be forced) - image has dependent child images
$ docker image rm 14ac49121f70
$ docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       20.04     a0ce5a295b63   4 weeks ago   72.8MB
$ docker image rm a0ce5a295b63

$ docker image ls

[Tutorial 2] Port Mapping

1. Create and Run nginx Image

  • Run the nginx image in the detached mode
$ docker container run -d --name nginx1 nginx

$ docker container ls
CONTAINER ID   IMAGE     PORTS     NAMES
ba244193417e   nginx     80/tcp    nginx1
  • Find the IP address of the container
$ docker container inspect ba244193417e | grep IPAddress
"IPAddress": "172.17.0.2",
  • Connect to the nginx homepage in the container
$ curl 172.17.0.2:80

$ curl 172.17.0.2

2. Port Mapping

But from the outside of the host, you cannot connect to the nginx container directly.

The easiest solution is to connect the host port and the container port.

  • Let’s stop the container and remove it.
$ docker container stop ba244193417e
$ docker container rm ba244193417e

$ docker container ls -a

  • Run a new nginx container with the -p <host port>:<container port> option.
$ docker container run -d -p 8080:80 --name nginx2 nginx

$ docker container ls
CONTAINER ID   IMAGE     PORTS  
f12c55bc46de   nginx     0.0.0.0:8080->80/tcp, :::8080->80/tcp  
$ docker container inspect f12c55bc46de | grep IPAddress
"IPAddress": "172.17.0.2",
$ docker container port f12c55bc46de
80/tcp -> 0.0.0.0:8080
80/tcp -> :::8080
  • You can connect to the nginx through host now.
$ curl 172.17.0.2

$ curl localhost:8080

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s