- Introduction
- CLI Commands
- Listing and info
- Deleting
- Creating and Running basics
- Parameter in CMD / Entrypoint
- Environmental variables and build args
- Dockerization examples
- Available installer for every OS on Docker website, from 2020 Win 10 Home is also supported.
- Dockerhub registration expected to be able to try hub services for images
For better understanding and better syntax, docker image ls
, docker container ls
like syntax is newly created instead of traditional docker images
and docker ps
.
Still, both will work, so I will provide them below.
- images
docker images
ordocker image ls
. - containers
docker ps
ordocker container ls
-a
show containers not running too-l
show latest onlyq
show ID only
You can get information about an image, a container. For example: environmental variables, entrypoint, state
docker inspect 5fe2fe
- image
docker image rm alpine:3.4
- container
docker container rm 6fe5ce
--force
,-f
force removal with SIGKILL
- stop all containers
docker stop $(docker ps -a -q)
- remove all containers
docker rm $(docker ps -a -q)
- remove all images
docker image rm $(docker images)
docker build -t myimage .
create image with Dockerfiledocker pull hello-world
pull image called hello-world from the remote (docker hub)docker create -name my-container hello-world
create your container from hello-world imagedocker start my-container
start the container createddocker stop 4ce5ek
stop the containerdocker restart 4ce5ek
docker run starts the container and if not found, automatically runs docker pull and docker create before.
docker run -d -p 8080:8080 --name container_name image_name
-d
detached modedocker attach
get back to container CLI
-n
name container-p
,--publish
publish port to the host, 3000:5000 means 5000 is the inside port, 3000 is the public-i
interactive mode-t
,-it
start a pseudo-terminal to interact with containerdocker exec 5ec4lf cat /etc/*release*
docker exec 5ec4lf ls
docker exec 5ec4lf curl http://localhost:8080
As example, run sleep command in ubuntu image for 5 millisec
docker run ubuntu sleep 5
docker build - t ubuntu-sleeper
docker run ubuntu-sleeper
FROM ubuntu
CMD sleep 5
it is the same:
FROM ubuntu
CMD ["sleep", "5"]
in CMD [...] executable first, parameter next
docker run ubuntu-sleeper 5
FROM ubuntu
ENTRYPOINT["sleep"]
default parameter if not provided
FROM ubuntu
ENTRYPOINT["sleep"]
CMD[5]
docker run -v /opt/datadir:/var/lib/mysql mzsql volume mapping keep data even if container does not run any more
- ARG - only available during image build
- ENV - available for container
So PRINTME will be printed, but APPLE won't:
...
ARG APPLE
ENV PRINTME=$APPLE
...
...
console.log(process.env.PRINTME)
console.log(process.env.APPLE)
...
docker build --build-arg APPLE="fruit fruit" -t imagename .
parameters to be printed in build output:
...
ARG APPLE
ENV APPLE=$APPLE # it is not necessary, with this line you can use APPLE in runtime
RUN echo "hello world"
RUN echo ${APPLE}
...
parameters to be printed in runtime in a javascript app:
...
console.log('here ${process.env.APPLE}');
...
docker run -p 8080:8080 --env PEAR="vegetable" myimage`
...
console.log(process.env.PEAR);
...
docker login
docker push my-image
|Python|Flask|Ubuntu|Pip|
based on mmumshad's test app
docker run -it ubuntu bash
start a container and a bash CLI insideapt-get update
apt-get install -y python
answer 'yes' to all the installation checksapt-get install python3-pip
pip3 install flask
cat > /opt/app.py
create code file to write it- paste code:
import os
from flask import flask
app=FLask(__name__)
@app.route("/")
def main():
return "Welcome"
@app.route(`/how are you`)
def hello():
return 'I am good'
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
- run:
FLASK_APP=/opt/app.py flask run --host-0.0.0.0
- dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python python3-pip
RUN pip3 install flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
- build
docker build .
- build again with name:
docker build . -t my-simple-webapp
this build process with tagging will be faster, reuses the existing image - run
docker run -p 8080:5000
my-simple-webapp`
Now you can open it in browser on http://localhost:8080
or http://localhost:8080/how are you
.