100% found this document useful (1 vote)
175 views7 pages

Docker Cheat-Sheet

This document provides instructions for interacting with Docker containers and images. It includes commands for creating, running, stopping, removing, and committing containers. It also includes commands for searching, pulling, tagging, and pushing images to private Docker registries. The key steps are: 1. Create an Ubuntu container, install Java, and create/run a simple "Hello World" Java program. Commit the changes to a new image. 2. Tag the image with a username/repository, and push it to a private Docker Hub registry. 3. Pull the image from the private registry and run the Java program. 4. Search for an Apache image, pull it, and run it with port publishing.

Uploaded by

Naitik Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
175 views7 pages

Docker Cheat-Sheet

This document provides instructions for interacting with Docker containers and images. It includes commands for creating, running, stopping, removing, and committing containers. It also includes commands for searching, pulling, tagging, and pushing images to private Docker registries. The key steps are: 1. Create an Ubuntu container, install Java, and create/run a simple "Hello World" Java program. Commit the changes to a new image. 2. Tag the image with a username/repository, and push it to a private Docker Hub registry. 3. Pull the image from the private registry and run the Java program. 4. Search for an Apache image, pull it, and run it with port publishing.

Uploaded by

Naitik Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DOCKER CHEAT SHEET

Container Interaction Commands


How to create a Container
Command to create a Container This Command is used to create a docker create -t –name=ubicon
Container ubuntu
Show all the container This command shows all the docker ps -a
containers that are created
Command to run a Container This Command is used to run a docker run -it --ubicon (container_
container name)
Command to start a Container This Command is used to start a docker start ubicon (container_name)
Container
Command to kill a Container This Command is used to kill the docker kill ubicon (container_name)
Container
Command to stop a container This Command is used to stop the docker stop ubicon (container_name)
container
Command to restart a container This Command is used to restart the docker restart ubicon
container (container_name)
Command to pause the container This Command is used to pause the docker pause ubicon
container (container_name)
Command to unpause the container This Command is used to unpause the docker unpause ubicon
container (container_name)
Command to remove the container This Command is used to remove the docker rm ubicon or docker rm -f
(when container is stopped) container ubicon (forcefully remove the
container)
Command to remove the image of This Command is used to remove the docker rmi image_name or ID
the container image of the container

Question: Create an Ubuntu container, install Java, and run hello world program
Make an image of this setup
Push this image into a private Docker Hub registry
Run the image from the private registry by pulling it back

JDK installation and Java Program


Create an Ubuntu Container This command is used to Docker run -it –name=ubicon ubuntu
create container
Update the ubuntu and then install This command is used to apt-get update
java (OpenJdk) inside the container install jdk apt-get install -y default-jdk
Create and run simple java program By using this command echo “ public class HelloWorld{public static void
create and run simple java main(String args[]){System.out.println(“Hello
program World”)}” > HelloWorld.java
Compile the java program and run it This command is used to Javac HelloWorld.java
using the command complie and run java Java HelloWorld
program
Come out of the shell and Commit By using this command docker commit ubicon newimage
the changes in the container to a new commit the changes in the
image container to a new image
Tag image with docker hub username This command used top docker tag newimage
and repository name tag the image your_dockerhub_username/newimage
Push the tag image to your private By using this command docker push
docker hub repository push your tag image your_dockerhub_username/newimage
Pull the image from your private By using this command docker pull
docker hub repository pull the image your_dockerhub_username/newimage
Run a container using the pulled ------- docker run -it
image your_dockerhub_username/newimage java
HelloWorld
Result ----------- Hello World

1. Search an Apache image


2. Pull appropriate Apache image
3. Check whether the image pull or not
4. Run the Apache and check port as it is not exposed outside
5. Rerun Apache with -p option to expose the port outside
6. Check and Browse in any browser
7. Run once again by the customizing port
8. Try to change the content of the index.html
Search and Pulling Images
First let’s see the available container This command is used to check docker ps -a
available container
Search an Apache image This command is used to docker search apache
search an Apache image
Now pull the appropriate apache image. This command is used to pull docker pull httpd
(httpd) the apache(httpd) image
Check whether the pull or not This command is used to check docker images
image pulled successfully or
not
Now run the server and check port This command is used to check docker port mywebserver
port
It’s not showing anything that’s do one ------------------------------- docker stop mywebserver
thing stop this container and remove docker rm mywebserver
Now, again create and run a httpd This command is used to run docker run -d --name=mywebserver -p
container but now with -p option by the container with port 8080:80 httpd
customizing the port. (-p means publish)
Now check the port by running it in HTTP request to the IP curl 0.0.0.0:8080
browser or using the curl command specified
we need to open the interactive shell by This command is used to docker exec -it mywebserver bash
using the command change the html file content
Now update, upgrade the server and This command is used to apt-get update
install any editor inside it update, install the editor apt-get upgrade
apt-get install vim
The html file is present inside the This command is used to cd htdocs
“htdocs” folder change directory to “htdocs”
Edit the file This command is used to edit vim index.html
the file
Save the file In order to insert text, press “I” and then
edit the content. Save the file and exit
using the “:wq”

Now check the bowser You can see the Result

Set Tag Your image


Login Registry
Push Image to your registry
Pull image to your registry
Tag your image
Pull the image This command is used to pull docker pull ubuntu
the image
Then check images ------------------------------------- docker images
Create a container --------------------------------------- docker run -it -d ubuntu
Commit the images ------------------------------------------ docker commit d8d142a2d254
(container id)piklu1903/fake2-
ubuntu:4
Push the image ----------------------------------------- docker push piklu1903/fake2-
ubuntu:4 (own image)
To set a tag for your Docker This command is used to tag docker tag
image your image SOURCE_IMAGE[:TAG]
TARGET_IMAGE[:TAG]
Example docker tag myapp:latest
myapp:v1.0

Data Management in Docker through Volumes and Bind mounts


1) Run a container with docker volume
2) Inspect whether the volume is attached with the container or not
3) Create a file inside the volume
4) Stop container, remove the container and then remove the volume

Using the “- -mount” flag


Firstly, creating and running This command is used to docker run -d --name=newcon --mount
container, while also create a container and also source=newvol, target=/newfol
mounting a volume named mounting a volume nginx:latest
“newvol” into the container at here: newcon-> container name
the path “/newfol” for data
sharing or persistence
Inspect the container Using this command we are docker inpect conatiner newcon
retrieving the detailed
information about the
container and also checking
whether the volume is
mount or not
Now in order to create a start an interactive shell docker exec -it newcon bash
file inside the volume session within the running
docker container
After creating the file, exit Only type “exit”
from the interactive mode
Then next check the file Using this command we are sudo ls
checking file is present or /var/lib/docker/volumes/newvol/_data
not in the respective folder
now stop the container, This command is used to docker stop newcon
remove the container and stop the container and docker rm newcon
then remove the volume remove the container and docker volume rm newvol
then remove the volume

Volumes - Using the “- v”


flag
Firstly, creating and running docker run -d --name=newcon2 -v
container, while also newvol2:/newfol2 nginx:latest
mounting a volume named
“newvol2” into the container
at the path “/newfol2” for
data
Inspect the container Using this command we are docker inspect newcon2
retrieving the detailed
information about the
container and also checking
whether the volume is
mount or not
Now in order to create a start an interactive shell docker exec -it newcon2 bash
file inside the volume session within the running
docker container
After creating the file, exit Only type “exit”
from the interactive mode
Then next check the file Using this command we are sudo ls
checking file is present or /var/lib/docker/volumes/newvol2/_data
not in the respective folder
now stop the container, This command is used to docker stop newcon
remove the container and stop the container and docker rm newcon
then remove the volume remove the container and docker volume rm newvol
then remove the volume

Bind Mount - Using the “- -


mount” flag
we are simply creating a docker run -d -it --name=testcontainer
running the container and --mount type=bind,
attaching a volume to it source="(pwd)"/target,target=/newexp
using the “- -mount” flag nginx:latest
Inspect the container Using this command we are docker inspect testcontainer
retrieving the detailed
information about the
container and also checking
whether the volume is
mount or not

Bind Mount - Using the “-v”


flag
Here we are creating and docker run -d -it –name=piklu -v
running a container “$(pwd)”/none:/newdir
nginx:latest
Next inspect the container and docker inspect piklu
check mount section

Docker Networking

Docker Networking
Check available network docker network ls
create a custom network of This command used to create a docker network create --
type bridge bridge network driver=bridge my_network
Inspect the bridge network This command is used to docker network inspect bridge
retrieve the information of the
bridge network driver
we are creating a new docker run -dt --name=nettry2
container of ubuntu image ubuntu
without specifying any
network details
check the network details docker inspect network
container_name
Connect the network docker network connect
network_name nettry2
disconnect a container from This command is used to docker network disconnect bridge
any network disconnect the network container_name
Docker network with a docker network create
custom subnet network_name --
subnet=10.11.0.0/16
Run a container and connect it docker run -it --
to a custom network with a name=container_name --
net=network_name --ip=10.11.0.10
static IP address using the
ubuntu
ubuntu image
try to create a container of docker run -dt httpd (apache)
apache httpd image without
specifying its name.

Docker Compose

Try Docker Compose


Create a directory for the mkdir composetest
project: cd composetest
Create a file called app.py in import time
your project directory
import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis',
port=6379)

def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except
redis.exceptions.ConnectionError
as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)

@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have
been seen {} times.\
n'.format(count)
Create another file called redis
requirements.txt in your flask
project directory
Create a docker file FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc
musl-dev linux-headers
COPY requirements.txt
requirements.txt
RUN pip install -r
requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Create a file called services:
compose.yaml in your project web:
directory build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
Build and run your app with docker compose up
compose
Go to browser Enter http://localhost:8000/
Refresh the page Output:
Hello World! I have been seen 2
times.
Switch to another terminal docker image ls
window, Listing images at this
point should return redis and
web
Stop the application, from docker compose down
within your project directory
in the second terminal,
If you want to run your docker compose ps
services in the background, docker compose up -d
you can pass the -d flag (for
"detached" mode)

You might also like