- We downlaod the docker image.
- Underlying host where docker is installed is called Docker Host
- when we run the image, a container is created from that image.
- that container runs.
- we can run multiple container of same images.
- every running container is assigned a
container-name
andcontainer-id
randomly by docker - we don't need to type full
container-id
. just type the minimum chars to distiguish from other running containers.
docker run ubuntu
- To run container with specific name
docker run --name koolbantu ubuntu
- run the image
- if image is not downloaded, it'll download it.
docker ps
- List all running docker containers
docker ps -a
- List all containers, running and exited
docker stop <container-id/container-name>
docker rm <container-name>
docker images
docker rmi <image-name>
docker pull <image-name>
- It will donwload the image but not run it.
docker run <image-name> <commands>
- Example run ubuntu and sleep for 20 sec
docker run ubuntu sleep 20
docker exec <container-name> <command>
- Example
docker exec cold_aryabhata cat /etc/hosts
docker run -d <image-name>
docker attach <container-name/container-id>
docker run <image-name>:<tag>
- Example
docker run mongodb:4.0
- By default, it runs the latest version
docker run -i <image-name>
- Now we can interect with it.
- but still terminal input is missing (Enter your name:)
- So for showing terminal output, use
-t
docker run -it <image-name>
- Every docker container is assigned an IP.
- It is internal IP and only accessible without the docker host
- Outside docker host, this is not accessiable.
- So we map docker host's port to container's port
docker run -p <host-PORT>:<docker-PORT> <image-name>
- Example
docker run -p 1100:5000 mywebapp
- it will map host machine port 1100 to docker port 5000
- means, request coming to host machine port 1100 will be redirected to docker image's port 5000.
docker run -v <host-dir>:<docker-dir> <image-name>
- Example
docker run -v /opt/mydata:/var/lib/mysql mysql
- It will map host machine's
opt/mydata
folder to docker container's/var/lib/mysql
folder - Means whatever data will come to docker's
/var/lib/mysql
folder, will be directed to host's/opt/mydata
folder.
docker inspect <container-name/container-id>
docker logs <container-name/container-id>
docker run -e HI_COLOR=blue mywebapp
docker run -e HI_COLOR=green mywebapp
- Inside the container, this env variable can be used
- For instance, for python
color= os.environ.get('HI_COLOR')
- use
docker inspect <container-id|container-name>
command to get env-variables of running container.
To create docker image all you need is a Dockerfile
- Create a file named
Dockerfile
(no extension) - copy this content (you will learn later what is this)
FROM ubuntu
RUN apt update
RUN apt install -y python python-pip
RUN pip install Flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
- now, in the same dir, run this command
docker build . -t shivampip/myflask
.
means source files are in current dir-t
means give image a tag (name)- only
myflask
is also fine but in that case can't be pushed to docker hub. - to be able to push to docker hub, use
<docker-username>/<image-name>
docker login
docker push shivampip/myflask
CMD
command runs plain
CMD sleep 10
or
CMD ["sleep", "10"]
ENTRYPOINT ["sleep"]
CMD ["5"]
- Now while running
docker run mysleeper 20
this will be like
docker run mysleeper sleep 20
- if value not provided, default is
5
docker run --cpus=.5 ubuntu
.5
means 50% CPU
docker run --memory=400m ubuntu
400m
means 400 Mb