0% found this document useful (0 votes)
4 views

docker

The document provides a comprehensive guide on using Docker commands for managing containers, images, and networks. It includes instructions for running, stopping, and removing containers, as well as pulling images and setting environment variables. Additionally, it covers creating Docker networks and deploying applications with specific configurations using Docker Compose.

Uploaded by

saiarungcp10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

docker

The document provides a comprehensive guide on using Docker commands for managing containers, images, and networks. It includes instructions for running, stopping, and removing containers, as well as pulling images and setting environment variables. Additionally, it covers creating Docker networks and deploying applications with specific configurations using Docker Compose.

Uploaded by

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

version of docker

docker --version

How many containers are running on this host?


docker ps

How many images are available on this host?


docker images -q | wc -l

Run a container using the redis image


docker run redis

To stop the present container


docker stop silly_sammet

list all containers, including those that are stopped


docker ps -a

What is the image used to run the nginx-1 container?


docker ps --filter "name=nginx-1" --format "{{.Image}}"

What is the name of the container created using the ubuntu image?
docker ps -a --filter "ancestor=ubuntu" --format "{{.Names}}"

What is the ID of the container that uses the alpine image and is not running?
docker ps -a --filter "ancestor=alpine" --filter "status=exited" --format "{{.ID}}"

What is the status of the container that uses the alpine image
docker ps -a --filter "ancestor=alpine" --format "{{.Status}}"

How many containers are RUNNING on this host now?


docker ps -q | wc -l

How many containers are PRESENT on the host now?


docker ps -a -q | wc -l

Delete all containers from the Docker Host.


Both Running and Not Running ones. Remember you may have to stop containers before
deleting them.
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)

How can remove the ubuntu image from Docker host?


docker rmi ubuntu

How can pull the nginx:1.14-alpine image from Docker Hub


docker pull nginx:1.14-alpine

Run a container with the nginx:1.14-alpine image and name it webapp


docker run -d --name webapp nginx:1.14-alpine

Delete all images on the host


docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)

------------------------------------------
How many ports are published on this container
docker port silly_sammet
Which of the below ports are published on Host
80/tcp -> 0.0.0.0:8080
443/tcp -> 0.0.0.0:8443
Port 80 in the container is mapped to port 8080 on the host.
Port 443 in the container is mapped to port 8443 on the host.

Run an instance of kodekloud/simple-webapp with a tag blue and map port 8080 on the
container to 38282 on the host.
docker run -d -p 38282:8080 kodekloud/simple-webapp:blue

---------------------------------
How many images
docker images

What is the size of the ubuntu image?


docker images | grep ubuntu

We just pulled a new image. What is the tag on the newly pulled NGINX image?
docker images | grep nginx

We just downloaded the code of an application. What is the base image used in the
Dockerfile?
Inspect the Dockerfile in the webapp-color directory.
cd webapp-color
cat Dockerfile

When a container is created using the image built with this Dockerfile, what is the
command used to RUN the application inside it.
Inspect the Dockerfile in the webapp-color directory.
python app.py

Build a docker image using the Dockerfile and name it webapp-color. No tag to be
specified.
cd webapp-color
docker build -t webapp-color .

Run an instance of the image webapp-color and publish port 8080 on the container to
8282 on the host.
docker run -d -p 8282:8080 --name webapp-color-container webapp-color
docker run: Runs a new container.
-d: Runs the container in detached mode (in the background).
-p 8282:8080: Maps port 8282 on the host to port 8080 on the container.
--name webapp-color-container: Names the container as webapp-color-container.
webapp-color: Specifies the image to use for the container.

What is the base Operating System used by the python:3.6 image?


If required, run an instance of the image to figure it out.
FROM python:3.6-alpine
RUN pip install flask
COPY . /opt/
EXPOSE 8080
WORKDIR /opt
ENTRYPOINT ["python", "app.py"]

Delete the Image by ID or Name:


docker rmi IMAGE_ID
rename the image by name
docker tag OLD_IMAGE_NAME:TAG NEW_IMAGE_NAME:TAG
-------------------------------------------------------------------------------
Inspect the environment variables set on the running container and identify the
value set to the APP_COLOR variable.
docker exec CONTAINER_ID env | grep APP_COLOR

Run a container named blue-app using image kodekloud/simple-webapp and set the
environment variable APP_COLOR to blue. Make the application available on port
38282 on the host. The application listens on port 8080.

docker run -p 38282:8080 --name blue-app -e APP_COLOR=blue kodekloud/simple-webapp

Deploy a mysql database using the mysql image and name it mysql-db.
Set the database password to use db_pass123. Lookup the mysql image on Docker Hub
and identify the correct environment variable to use for setting the root password.
docker pull mysql
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=db_pass123 mysql
-----------------------------------------------------------------------------------
----

What is the ENTRYPOINT configured on the mysql image?


docker inspect mysql | grep -i entrypoint

What is the CMD configured on the wordpress image?


docker pull wordpress
docker inspect wordpress | grep -i cmd

What is the command run at startup when the ubuntu image is run?
docker inspect ubuntu | grep -i cmd

Run an instance of the ubuntu image to run the sleep 1000 command at startup.
Run it in detached mode.
docker run -d ubuntu sleep 1000

----------------------------------------------------------------------------------
create a simple container called redis using image redis:alpine
docker run -d --name redis redis:alpine

1. Stop all running containers docker stop $(docker ps -q)


2. Remove all containers docker rm $(docker ps -a -q)
docker stop $(docker ps -q) && docker rm $(docker ps -a -q)
3. Remove docker network docker network rm clicknet

Next, create a simple container called clickcounter with the image kodekloud/click-
counter, link it to the redis container that we created in the previous task and
then expose it on the host port 8085
The clickcounter app run on port 5000.if you are unsure, check the hints section
for the exact commands.

docker run -d --name clickcounter --link redis:redis -p 8085:5000 kodekloud/click-


counter

Create a docker-compose.yml file under the directory /root/clickcounter. Once done,


run docker-compose up.
The compose file should have the exact specification as follows -redis service
specification - Image name should be redis:alpine. clickcounter service
specification - Image name should be kodekloud/click-counter, app is run on port
5000 and expose it on the host port 8085 in the compose file.

cd /root/clickcounter
nano docker-compose.yml

version: '3.8'
services:
redis:
image: redis:alpine
clickcounter:
image: kodekloud/click-counter
ports:
- "8085:5000"
depends_on:
- redis

docker-compose up -d

-----------------------------------------------------------------------------------
-----------
Let practice deploying a registry server on our own.
Run a registry server with name equals to my-registry using registry:2 image with
host port set to 5000, and restart policy set to always.
Note: Registry server is exposed on port 5000 in the image.
Here we are hosting our own registry using the open source Docker Registry.

docker run -d --name my-registry -p 5000:5000 --restart always registry:2

Now its time to push some images to our registry server. Let's push two images for
now .i.e. nginx:latest and httpd:latest.
Note: Don't forget to pull them first.
To check the list of images pushed , use curl -X GET localhost:5000/v2/_catalog

docker pull nginx:latest


docker pull httpd:latest
docker tag nginx:latest localhost:5000/nginx:latest
docker tag httpd:latest localhost:5000/httpd:latest
docker push localhost:5000/nginx:latest
docker push localhost:5000/httpd:latest
curl -X GET http://localhost:5000/v2/_catalog

docker image prune -a

Now we can pull images from our registry-server as well. Use docker pull [server-
addr/image-name] to pull the images that we pushed earlier.

In our case we can use: docker pull localhost:5000/nginx

-----------------------------------------------------------------------------------
-
Explore the current setup and identify the number of networks that exist on this
system.
docker network ls

We just ran a container named alpine-1. Identify the network it is attached to.
docker inspect alpine-1 --format '{{json .NetworkSettings.Networks}}'

What is the subnet configured on bridge network?


docker network inspect bridge

Run a container named alpine-2 using the alpine image and attach it to the none
network.
docker run --name alpine-2 --network none -d alpine sleep 1000

Create a new network named wp-mysql-network using the bridge driver. Allocate
subnet 182.18.0.0/24. Configure Gateway 182.18.0.1
docker network create --driver bridge --subnet 182.18.0.0/24 --gateway 182.18.0.1
wp-mysql-network

Deploy a mysql database using the mysql:5.6 image and name it mysql-db. Attach it
to the newly created network wp-mysql-network
Set the database password to use db_pass123. The environment variable to set is
MYSQL_ROOT_PASSWORD.

docker run -d --name mysql-db --network wp-mysql-network -e


MYSQL_ROOT_PASSWORD=db_pass123 mysql:5.6

Deploy a web application named webapp using the kodekloud/simple-webapp-mysql


image. Expose the port to 38080 on the host.
The application makes use of two environment variable:
1: DB_Host with the value mysql-db.
2: DB_Password with the value db_pass123.
Make sure to attach it to the newly created network called wp-mysql-network.
Also make sure to link the MySQL and the webapp container.

docker run -d --name webapp --network wp-mysql-network -p 38080:8080 -e


DB_Host=mysql-db -e DB_Password=db_pass123 --link mysql-db:mysql-db
kodekloud/simple-webapp-mysql

You might also like