How to create your first docker application?
This tutorial is for How to create your first docker application? we will learn how to create a docker application beginner guide. Docker is free software developed by Docker Inc. Docker use allows users to create independent and isolated environments to launch and deploy its applications. Docker’s work Unlike Docker, a virtual machine will include a complete operating system.
Install Docker (For Ubuntu)
Update your packages with apt:
$ sudo apt update
Now, install docker with apt:
$ sudo apt install docker.io
Finally, verify that Docker is installed:
$ sudo docker --version Output: Docker version 20.10.3, build 48d30b5
Create your project
Create your first Docker application, we create a folder on your computer.
- run.py file
- Dockerfile
Folder architecture:
.
├── Dockerfile
└── run.py
0 directories, 2 files
Edit the Python file
# run.py #!/usr/bin python3 print("Devnote tutorial!")
The first step to take when you create a Dockerfile you have access the DockerHub website.
# Dockerfile # A dockerfile start by importing the base image Like(python,selenium, etc...). # And use 'FROM' keyword to do that. FROM python:latest # Launch our python code, we must import it into our image. # We use 'COPY' keyword to do that. COPY run.py / # We use 'CMD' keyword to do that. # The following command will execute "python ./run.py". CMD [ "python", "./run.py" ]
Create the Docker image
$ docker build -t python-demo . Output: Sending build context to Docker daemon 7.815MB Step 1/3 : FROM python:latest latest: Pulling from library/python 0ecb575e629c: Pull complete 7467d1831b69: Pull complete feab2c490a3c: Pull complete f15a0f46f8c3: Pull complete 937782447ff6: Pull complete e78b7aaaab2c: Pull complete 06c4d8634a1a: Pull complete 42b6aa65d161: Pull complete f7fc0748308d: Pull complete Digest: sha256:ca8bd3c91af8b12c2d042ade99f7c8f578a9f80a0dbbd12ed261eeba96dd632f Status: Downloaded newer image for python:latest ---> 2a93c239d591 Step 2/3 : COPY run.py / ---> 26bb4f5767de Step 3/3 : CMD [ "python", "./run.py" ] ---> Running in c0e9034045a5 Removing intermediate container c0e9034045a5 ---> 17eb42125c64 Successfully built 17eb42125c64 Successfully tagged python-demo:latest
In this case ‘-t'(tag) option allows you to define the name of your image.
Run the Docker image
Now the image is created, and your code is ready to be launched.
$ docker run python-demo Output: Devnote tutorial!
You should normally see Devnote tutorial! displayed in your terminal.
Useful commands
We have prepared a list of commands that may be useful to you on Docker.
List your images.
$ docker image ls
Delete a specific image.
$ docker image rm [image_name]
Delete all existing images.
$ docker image rm $(docker images -a -q)
List all containers (start)
$ docker ps
List all existing containers.
$ docker ps -a
Stop all running containers.
$ docker stop $(docker ps -a -q)
Stop a specific container.
$ docker stop [container_name]
Delete a specific container(stopped)
$ docker rm [container_name]
Delete all containers (stopped)
$ docker rm $(docker ps -a -q)
Display logs
$ docker logs [container_name]