Neetz World
March 8, 2023

Docker with basic examples

Posted on March 8, 2023  •  5 minutes  • 893 words

What is Docker ?

Docker :


Components of Docker

docker components

Docker Daemon:

Docker Client:

Docker Registry:

Docker Objects:

Images:

Containers:


Two ways to create a container:

Note: Install the docker : https://docs.docker.com/get-docker/ before you go further.

Let’s say we want to create a container with python installed.

1. With pre-configured image from Docker-Registry:

docker image from registry

☺ docker pull python

Using default tag: latest
latest: Pulling from library/python
Digest: sha256:d3c16df33787f3d03b2e096037f6deb3c1c5fc92c57994a7d6f2de018de01a6b
Status: Image is up to date for python:latest
docker.io/library/python:latestdocker run -dt python
☺ docker images

REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
python       latest    8cad8eec9ec5   4 days ago   875MB
☺  docker run -dt python

0311adddf2e1b83d2a1e8f4b1bf7d1e189b61f22fab62ff74f32ab61ef8c6752
☺  docker ps

CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS         PORTS     NAMES
0311adddf2e1   python    "python3"   6 seconds ago   Up 5 seconds
print("hello world")

docker with python run

☺  docker stop 9cbff115d0d9
9cbff115d0d9

☺  docker rm 9cbff115d0d9
9cbff115d0d9

☺  docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
☺  docker rmi python
Untagged: python:latest
Untagged: python@sha256:d3c16df33787f3d03b2e096037f6deb3c1c5fc92c57994a7d6f2de018de01a6b
Deleted: sha256:8cad8eec9ec53ea519f50fce862660e731b8a3b4ff5bda2af059d12495f5df08


☺  docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

2. With Dockerfile :

docker file

# Pull the latest python docker image layer in as a base.
FROM python:latest

#Copy this file to the container
COPY ./main.py .

#Run the python file
CMD ["python", "main.py"]
☺  docker build -t docker-first .


[+] Building 0.1s (5/5) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                             0.0s
 => => transferring dockerfile: 129B                                                                                                                             0.0s
 => [internal] load .dockerignore                                                                                                                                0.0s
 => => transferring context: 2B                                                                                                                                  0.0s
 => [internal] load metadata for docker.io/library/python:latest                                                                                                 0.0s
 => CACHED [1/1] FROM docker.io/library/python:latest                                                                                                            0.0s
 => exporting to image                                                                                                                                           0.0s
 => => exporting layers                                                                                                                                          0.0s
 => => writing image sha256:f6690f886d3e9a06263105e9d638936ba1ecef702eefba0522ed3fc3a0ad383e                                                                     0.0s
 => => naming to docker.io/library/docker-first
☺  docker images
REPOSITORY     TAG       IMAGE ID       CREATED      SIZE
docker-first   latest    f6690f886d3e   4 days ago   875MB
☺  docker run docker-first
hello world
☺  docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
☺  docker ps -a
CONTAINER ID   IMAGE          COMMAND            CREATED         STATUS                     PORTS     NAMES
d1820c86eecd   docker-first   "python main.py"   5 minutes ago   Exited (0) 5 minutes ago             ecstatic_engelbart
Follow me

To have some coding fun and learning new things!