Skip to content

Instantly share code, notes, and snippets.

@shivampip
Last active February 26, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivampip/a763aceda5f7760fa2f4dc4893f9a535 to your computer and use it in GitHub Desktop.
Save shivampip/a763aceda5f7760fa2f4dc4893f9a535 to your computer and use it in GitHub Desktop.
Docker Cheatsheet

Welcome to Docker

  • 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 and container-id randomly by docker
  • we don't need to type full container-id. just type the minimum chars to distiguish from other running containers.

Run

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.

List Containers

docker ps
  • List all running docker containers
docker ps -a
  • List all containers, running and exited

Stop a container

docker stop <container-id/container-name>

Delete a container

docker rm <container-name>

List images

docker images

Delete images

docker rmi <image-name>

Download an image

docker pull <image-name>
  • It will donwload the image but not run it.

Run a command after running container

docker run <image-name> <commands>
  • Example run ubuntu and sleep for 20 sec
docker run ubuntu sleep 20

Execute commmand on running container

docker exec <container-name> <command>
  • Example
docker exec cold_aryabhata cat /etc/hosts

Run container in background (detach)

docker run -d <image-name>

Bring background container in foreground

docker attach <container-name/container-id>

Run specific version of image

docker run <image-name>:<tag>
  • Example
docker run mongodb:4.0
  • By default, it runs the latest version

Run container in interactive mode

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>

PORT mapping

  • 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.

Volume mapping

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.

Inspect container

docker inspect <container-name/container-id>

See container logs

docker logs <container-name/container-id>

Environment Variable- way to pass variable data to container

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.

Create Docker Image

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>

Push image to Docker Hub

docker login
docker push shivampip/myflask

CMD

  • CMD command runs plain
CMD sleep 10

or

CMD ["sleep", "10"]

ENTRYPOINT is where we can pass variable params

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

Restrict container CPU useas

docker run --cpus=.5 ubuntu
  • .5 means 50% CPU

Restrict container memory useas

docker run --memory=400m ubuntu
  • 400m means 400 Mb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment